From 5bce7e793f835f5c14dcbe63d75ee992f7d81f04 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 5 Mar 2019 11:03:06 +0200 Subject: [PATCH] Redirect log output to mobile system loggers --- logutils/override.go | 29 +++++++++++++++++++---------- logutils/stdhandler.go | 17 +++++++++++++++++ params/config.go | 3 +++ 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 logutils/stdhandler.go diff --git a/logutils/override.go b/logutils/override.go index 70881937e..dd5283066 100644 --- a/logutils/override.go +++ b/logutils/override.go @@ -8,8 +8,19 @@ import ( "github.com/status-im/status-go/params" ) +// OverrideWithStdLogger overwrites ethereum's root logger with a logger from golang std lib. +func OverrideWithStdLogger(config *params.NodeConfig) error { + return enableRootLog(config.LogLevel, NewStdHandler(log.LogfmtFormat())) +} + // OverrideRootLogWithConfig derives all configuration from params.NodeConfig and configures logger using it. func OverrideRootLogWithConfig(config *params.NodeConfig, colors bool) error { + if !config.LogEnabled { + return nil + } + if config.LogMobileSystem { + return OverrideWithStdLogger(config) + } return OverrideRootLog(config.LogEnabled, config.LogLevel, FileOptions{ Filename: config.LogFile, MaxSize: config.LogMaxSize, @@ -26,18 +37,8 @@ func OverrideRootLog(enabled bool, levelStr string, fileOpts FileOptions, termin disableRootLog() return nil } - - return enableRootLog(levelStr, fileOpts, terminal) -} - -func disableRootLog() { - log.Root().SetHandler(log.DiscardHandler()) -} - -func enableRootLog(levelStr string, fileOpts FileOptions, terminal bool) error { var ( handler log.Handler - err error ) if fileOpts.Filename != "" { handler = FileHandlerWithRotation(fileOpts, log.LogfmtFormat()) @@ -45,6 +46,14 @@ func enableRootLog(levelStr string, fileOpts FileOptions, terminal bool) error { handler = log.StreamHandler(os.Stderr, log.TerminalFormat(terminal)) } + return enableRootLog(levelStr, handler) +} + +func disableRootLog() { + log.Root().SetHandler(log.DiscardHandler()) +} + +func enableRootLog(levelStr string, handler log.Handler) error { if levelStr == "" { levelStr = "INFO" } diff --git a/logutils/stdhandler.go b/logutils/stdhandler.go new file mode 100644 index 000000000..5f2c81eba --- /dev/null +++ b/logutils/stdhandler.go @@ -0,0 +1,17 @@ +package logutils + +import ( + stdlog "log" + + "github.com/ethereum/go-ethereum/log" +) + +// NewStdHandler returns handler that uses logger from golang std lib. +func NewStdHandler(fmtr log.Format) log.Handler { + return log.FuncHandler(func(r *log.Record) error { + line := fmtr.Format(r) + // 8 is a number of frames that will be skipped when log is printed. + // this is needed to show the file (with line number) where call to a logger was made + return stdlog.Output(8, string(line)) + }) +} diff --git a/params/config.go b/params/config.go index dc3bd127f..78269be3a 100644 --- a/params/config.go +++ b/params/config.go @@ -259,6 +259,9 @@ type NodeConfig struct { // LogEnabled enables the logger LogEnabled bool `json:"LogEnabled"` + // LogMobileSystem enables log redirection to android/ios system logger. + LogMobileSystem bool + // LogFile is filename where exposed logs get written to LogFile string