configure logging to file
This commit is contained in:
parent
2541a561b1
commit
c509106aec
1 changed files with 50 additions and 0 deletions
50
main.go
50
main.go
|
|
@ -12,6 +12,8 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/op/go-logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
var isNode bool
|
var isNode bool
|
||||||
|
|
@ -23,6 +25,12 @@ var justNodeId bool
|
||||||
var daemonize bool
|
var daemonize bool
|
||||||
var pidPath string
|
var pidPath string
|
||||||
|
|
||||||
|
type LogConfig struct {
|
||||||
|
XMLName xml.Name `xml:"log"`
|
||||||
|
Level string `xml:"level,attr"`
|
||||||
|
LogPath string `xml:",innerxml"`
|
||||||
|
}
|
||||||
|
|
||||||
type NodeConfig struct {
|
type NodeConfig struct {
|
||||||
XMLName xml.Name `xml:"node"`
|
XMLName xml.Name `xml:"node"`
|
||||||
KeyStore string `xml:"keystore"`
|
KeyStore string `xml:"keystore"`
|
||||||
|
|
@ -36,6 +44,42 @@ type NodeConfig struct {
|
||||||
} `xml:"neighbor"`
|
} `xml:"neighbor"`
|
||||||
|
|
||||||
RPCEndpoints []string `xml:"rpc"`
|
RPCEndpoints []string `xml:"rpc"`
|
||||||
|
|
||||||
|
Log LogConfig `xml:"log"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupLogging(log LogConfig) {
|
||||||
|
var level logging.Level
|
||||||
|
switch log.Level {
|
||||||
|
case "critical":
|
||||||
|
level = logging.CRITICAL
|
||||||
|
case "error":
|
||||||
|
level = logging.ERROR
|
||||||
|
case "warning":
|
||||||
|
level = logging.WARNING
|
||||||
|
case "notice":
|
||||||
|
level = logging.NOTICE
|
||||||
|
case "info":
|
||||||
|
level = logging.INFO
|
||||||
|
case "debug":
|
||||||
|
level = logging.DEBUG
|
||||||
|
default:
|
||||||
|
level = logging.INFO
|
||||||
|
}
|
||||||
|
|
||||||
|
logging.SetLevel(level, "")
|
||||||
|
|
||||||
|
// if log tag has file path, then it open file in append mode,
|
||||||
|
// otherwise it will use default stdout logger
|
||||||
|
if log.LogPath != "" {
|
||||||
|
logFile, err := os.OpenFile(log.LogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.FileMode(0600))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to open log file: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
logging.SetBackend(logging.NewLogBackend(logFile, "", 0))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func bootstrapNode() {
|
func bootstrapNode() {
|
||||||
|
|
@ -84,6 +128,9 @@ func nodeMain() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setup logging
|
||||||
|
setupLogging(config.Log)
|
||||||
|
|
||||||
// check presense of keystore and id in config
|
// check presense of keystore and id in config
|
||||||
if config.KeyStore == "" {
|
if config.KeyStore == "" {
|
||||||
fmt.Fprintln(os.Stderr, "no keystore path specified!")
|
fmt.Fprintln(os.Stderr, "no keystore path specified!")
|
||||||
|
|
@ -170,6 +217,9 @@ func nodeMain() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// start node
|
||||||
|
node.Start()
|
||||||
|
|
||||||
// TODO: daemonize
|
// TODO: daemonize
|
||||||
|
|
||||||
// register go channel to receive unix signals,
|
// register go channel to receive unix signals,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue