add log handler for Stdout and file (#751)

* add log handler for Stdout

* enhance logger based on node config

* write logs to stderr

* remove unused `return` after call to Fatal
This commit is contained in:
Andrea Franz 2018-03-23 14:26:28 +01:00 committed by GitHub
parent 60d8ba5e62
commit 70d153bb60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"flag" "flag"
"fmt" "fmt"
stdlog "log"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -73,14 +74,39 @@ var (
// All general log messages in this package should be routed through this logger. // All general log messages in this package should be routed through this logger.
var logger = log.New("package", "status-go/cmd/statusd") var logger = log.New("package", "status-go/cmd/statusd")
func enhanceLogger(logger *log.Logger, config *params.NodeConfig) error {
var (
handler log.Handler
err error
)
if config.LogFile != "" {
handler, err = log.FileHandler(config.LogFile, log.LogfmtFormat())
if err != nil {
return err
}
} else {
handler = log.StreamHandler(os.Stderr, log.TerminalFormat(true))
}
level, err := log.LvlFromString(strings.ToLower(config.LogLevel))
if err != nil {
return err
}
filteredHandler := log.LvlFilterHandler(level, handler)
log.Root().SetHandler(filteredHandler)
return nil
}
func main() { func main() {
flag.Usage = printUsage flag.Usage = printUsage
flag.Parse() flag.Parse()
config, err := makeNodeConfig() config, err := makeNodeConfig()
if err != nil { if err != nil {
logger.Error("Making config failed", "error", err) stdlog.Fatalf("Making config failed %s", err)
return
} }
if *version { if *version {
@ -88,6 +114,10 @@ func main() {
return return
} }
if err := enhanceLogger(&logger, config); err != nil {
stdlog.Fatalf("Error initializing logger: %s", err)
}
backend := api.NewStatusBackend() backend := api.NewStatusBackend()
err = backend.StartNode(config) err = backend.StartNode(config)
if err != nil { if err != nil {