Redirect log output to mobile system loggers

This commit is contained in:
Dmitry 2019-03-05 11:03:06 +02:00 committed by Dmitry Shulyak
parent b2f02d0dbe
commit 5bce7e793f
3 changed files with 39 additions and 10 deletions

View File

@ -8,8 +8,19 @@ import (
"github.com/status-im/status-go/params" "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. // OverrideRootLogWithConfig derives all configuration from params.NodeConfig and configures logger using it.
func OverrideRootLogWithConfig(config *params.NodeConfig, colors bool) error { 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{ return OverrideRootLog(config.LogEnabled, config.LogLevel, FileOptions{
Filename: config.LogFile, Filename: config.LogFile,
MaxSize: config.LogMaxSize, MaxSize: config.LogMaxSize,
@ -26,18 +37,8 @@ func OverrideRootLog(enabled bool, levelStr string, fileOpts FileOptions, termin
disableRootLog() disableRootLog()
return nil return nil
} }
return enableRootLog(levelStr, fileOpts, terminal)
}
func disableRootLog() {
log.Root().SetHandler(log.DiscardHandler())
}
func enableRootLog(levelStr string, fileOpts FileOptions, terminal bool) error {
var ( var (
handler log.Handler handler log.Handler
err error
) )
if fileOpts.Filename != "" { if fileOpts.Filename != "" {
handler = FileHandlerWithRotation(fileOpts, log.LogfmtFormat()) 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)) 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 == "" { if levelStr == "" {
levelStr = "INFO" levelStr = "INFO"
} }

17
logutils/stdhandler.go Normal file
View File

@ -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))
})
}

View File

@ -259,6 +259,9 @@ type NodeConfig struct {
// LogEnabled enables the logger // LogEnabled enables the logger
LogEnabled bool `json:"LogEnabled"` 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 is filename where exposed logs get written to
LogFile string LogFile string