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:
parent
60d8ba5e62
commit
70d153bb60
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue