From 70d153bb60c932cd968186322b2c93037a792f5e Mon Sep 17 00:00:00 2001 From: Andrea Franz Date: Fri, 23 Mar 2018 14:26:28 +0100 Subject: [PATCH] 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 --- cmd/statusd/main.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/cmd/statusd/main.go b/cmd/statusd/main.go index 517673458..f1fcae9dc 100644 --- a/cmd/statusd/main.go +++ b/cmd/statusd/main.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + stdlog "log" "net/http" "os" "os/signal" @@ -73,14 +74,39 @@ var ( // All general log messages in this package should be routed through this logger. 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() { flag.Usage = printUsage flag.Parse() config, err := makeNodeConfig() if err != nil { - logger.Error("Making config failed", "error", err) - return + stdlog.Fatalf("Making config failed %s", err) } if *version { @@ -88,6 +114,10 @@ func main() { return } + if err := enhanceLogger(&logger, config); err != nil { + stdlog.Fatalf("Error initializing logger: %s", err) + } + backend := api.NewStatusBackend() err = backend.StartNode(config) if err != nil {