Removed unnecessary dependency from logutil package

util packages should have a few dependencies as possible, particularly from within the same repo, to prevent the application from creating import loops

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Samuel Hawksby-Robinson 2021-08-18 13:44:10 +01:00 committed by Jakub
parent bc63fa606e
commit 78892847ee
5 changed files with 63 additions and 18 deletions

View File

@ -276,7 +276,16 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password
return err return err
} }
if err := logutils.OverrideRootLogWithConfig(conf, false); err != nil { logSettings := logutils.LogSettings{
Enabled: conf.LogEnabled,
MobileSystem: conf.LogMobileSystem,
Level: conf.LogLevel,
File: conf.LogFile,
MaxSize: conf.LogMaxSize,
MaxBackups: conf.LogMaxBackups,
CompressRotated: conf.LogCompressRotated,
}
if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil {
return err return err
} }
@ -336,7 +345,17 @@ func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pass
if err != nil { if err != nil {
return err return err
} }
if err := logutils.OverrideRootLogWithConfig(conf, false); err != nil {
logSettings := logutils.LogSettings{
Enabled: conf.LogEnabled,
MobileSystem: conf.LogMobileSystem,
Level: conf.LogLevel,
File: conf.LogFile,
MaxSize: conf.LogMaxSize,
MaxBackups: conf.LogMaxBackups,
CompressRotated: conf.LogCompressRotated,
}
if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil {
return err return err
} }
b.account = &acc b.account = &acc

View File

@ -262,8 +262,17 @@ func setupLogging(config *params.NodeConfig) {
config.LogLevel = *logLevel config.LogLevel = *logLevel
} }
logSettings := logutils.LogSettings{
Enabled: config.LogEnabled,
MobileSystem: config.LogMobileSystem,
Level: config.LogLevel,
File: config.LogFile,
MaxSize: config.LogMaxSize,
MaxBackups: config.LogMaxBackups,
CompressRotated: config.LogCompressRotated,
}
colors := !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd())) colors := !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd()))
if err := logutils.OverrideRootLogWithConfig(config, colors); err != nil { if err := logutils.OverrideRootLogWithConfig(logSettings, colors); err != nil {
stdlog.Fatalf("Error initializing logger: %v", err) stdlog.Fatalf("Error initializing logger: %v", err)
} }
} }

View File

@ -255,8 +255,17 @@ func setupLogging(config *params.NodeConfig) {
config.LogLevel = *logLevel config.LogLevel = *logLevel
} }
logSettings := logutils.LogSettings{
Enabled: config.LogEnabled,
MobileSystem: config.LogMobileSystem,
Level: config.LogLevel,
File: config.LogFile,
MaxSize: config.LogMaxSize,
MaxBackups: config.LogMaxBackups,
CompressRotated: config.LogCompressRotated,
}
colors := !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd())) colors := !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd()))
if err := logutils.OverrideRootLogWithConfig(config, colors); err != nil { if err := logutils.OverrideRootLogWithConfig(logSettings, colors); err != nil {
stdlog.Fatalf("Error initializing logger: %v", err) stdlog.Fatalf("Error initializing logger: %v", err)
} }
} }

View File

@ -1,7 +1,7 @@
package logutils package logutils
import ( import (
lumberjack "gopkg.in/natefinch/lumberjack.v2" "gopkg.in/natefinch/lumberjack.v2"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )

View File

@ -5,28 +5,36 @@ import (
"strings" "strings"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/params"
) )
type LogSettings struct {
Enabled bool
MobileSystem bool
Level string
File string
MaxSize int
MaxBackups int
CompressRotated bool
}
// OverrideWithStdLogger overwrites ethereum's root logger with a logger from golang std lib. // OverrideWithStdLogger overwrites ethereum's root logger with a logger from golang std lib.
func OverrideWithStdLogger(config *params.NodeConfig) error { func OverrideWithStdLogger(logLevel string) error {
return enableRootLog(config.LogLevel, NewStdHandler(log.LogfmtFormat())) return enableRootLog(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(settings LogSettings, colors bool) error {
if !config.LogEnabled { if !settings.Enabled {
return nil return nil
} }
if config.LogMobileSystem { if settings.MobileSystem {
return OverrideWithStdLogger(config) return OverrideWithStdLogger(settings.Level)
} }
return OverrideRootLog(config.LogEnabled, config.LogLevel, FileOptions{ return OverrideRootLog(settings.Enabled, settings.Level, FileOptions{
Filename: config.LogFile, Filename: settings.File,
MaxSize: config.LogMaxSize, MaxSize: settings.MaxSize,
MaxBackups: config.LogMaxBackups, MaxBackups: settings.MaxBackups,
Compress: config.LogCompressRotated, Compress: settings.CompressRotated,
}, colors) }, colors)
} }