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"
|
"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 {
|
||||||
|
|
Loading…
Reference in New Issue