-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(logging)_: switch to zap.Logger as central logger #6025
base: feat/categorized-logging
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,6 @@ import ( | |
"path/filepath" | ||
"time" | ||
|
||
"golang.org/x/crypto/ssh/terminal" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/p2p" | ||
"github.com/ethereum/go-ethereum/p2p/enode" | ||
|
@@ -25,13 +23,12 @@ import ( | |
var logger = log.New("package", "status-go/cmd/node-canary") | ||
|
||
var ( | ||
staticEnodeAddr = flag.String("staticnode", "", "checks if static node talks waku protocol (e.g. enode://[email protected]:30303)") | ||
minPow = flag.Float64("waku.pow", params.WakuMinimumPoW, "PoW for messages to be added to queue, in float format") | ||
ttl = flag.Int("waku.ttl", params.WakuTTL, "Time to live for messages, in seconds") | ||
homePath = flag.String("home-dir", ".", "Home directory where state is stored") | ||
logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`) | ||
logFile = flag.String("logfile", "", "Path to the log file") | ||
logWithoutColors = flag.Bool("log-without-color", false, "Disables log colors") | ||
staticEnodeAddr = flag.String("staticnode", "", "checks if static node talks waku protocol (e.g. enode://[email protected]:30303)") | ||
minPow = flag.Float64("waku.pow", params.WakuMinimumPoW, "PoW for messages to be added to queue, in float format") | ||
ttl = flag.Int("waku.ttl", params.WakuTTL, "Time to live for messages, in seconds") | ||
homePath = flag.String("home-dir", ".", "Home directory where state is stored") | ||
logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`) | ||
logFile = flag.String("logfile", "", "Path to the log file") | ||
) | ||
|
||
func main() { | ||
|
@@ -58,12 +55,12 @@ func main() { | |
func init() { | ||
flag.Parse() | ||
|
||
colors := !(*logWithoutColors) | ||
if colors { | ||
colors = terminal.IsTerminal(int(os.Stdin.Fd())) | ||
} | ||
|
||
if err := logutils.OverrideRootLog(*logLevel != "", *logLevel, logutils.FileOptions{Filename: *logFile}, colors); err != nil { | ||
err := logutils.OverrideRootLoggerWithConfig(logutils.LogSettings{ | ||
Enabled: *logLevel != "", | ||
Level: *logLevel, | ||
File: *logFile, | ||
}) | ||
if err != nil { | ||
stdlog.Fatalf("Error initializing logger: %s", err) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ import ( | |
|
||
"github.com/google/uuid" | ||
"github.com/okzk/sdnotify" | ||
"golang.org/x/crypto/ssh/terminal" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
gethmetrics "github.com/ethereum/go-ethereum/metrics" | ||
|
@@ -46,7 +45,6 @@ const ( | |
var ( | ||
configFiles configFlags | ||
logLevel = flag.String("log", "", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`) | ||
logWithoutColors = flag.Bool("log-without-color", false, "Disables log colors") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to keep the flag, but deprecate it. I'm not sure of all usages of our There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
ipcEnabled = flag.Bool("ipc", false, "Enable IPC RPC endpoint") | ||
ipcFile = flag.String("ipcfile", "", "Set IPC file path") | ||
pprofEnabled = flag.Bool("pprof", false, "Enable runtime profiling via pprof") | ||
|
@@ -100,8 +98,11 @@ func init() { | |
|
||
// nolint:gocyclo | ||
func main() { | ||
colors := terminal.IsTerminal(int(os.Stdin.Fd())) | ||
if err := logutils.OverrideRootLog(true, "ERROR", logutils.FileOptions{}, colors); err != nil { | ||
err := logutils.OverrideRootLoggerWithConfig(logutils.LogSettings{ | ||
Enabled: true, | ||
Level: "ERROR", | ||
}) | ||
if err != nil { | ||
stdlog.Fatalf("Error initializing logger: %v", err) | ||
} | ||
|
||
|
@@ -400,8 +401,7 @@ func setupLogging(config *params.NodeConfig) { | |
MaxBackups: config.LogMaxBackups, | ||
CompressRotated: config.LogCompressRotated, | ||
} | ||
colors := !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd())) | ||
if err := logutils.OverrideRootLogWithConfig(logSettings, colors); err != nil { | ||
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil { | ||
stdlog.Fatalf("Error initializing logger: %v", err) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,14 @@ func (core *Core) Enabled(lvl zapcore.Level) bool { | |
return core.level.Enabled(lvl) | ||
} | ||
|
||
func (core *Core) Level() zapcore.Level { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we rename the file to zap_core.go? |
||
return core.level.Level() | ||
} | ||
|
||
func (core *Core) SetLevel(lvl zapcore.Level) { | ||
core.level.SetLevel(lvl) | ||
} | ||
|
||
func (core *Core) With(fields []zapcore.Field) zapcore.Core { | ||
clonedEncoder := encoderWrapper{Encoder: core.getEncoder().Clone()} | ||
for i := range fields { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,52 @@ | ||
package logutils | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/status-im/status-go/protocol/zaputil" | ||
) | ||
|
||
var ( | ||
_zapLogger *zap.Logger | ||
_initZapLogger sync.Once | ||
) | ||
|
||
// ZapLogger creates a custom zap.Logger which will forward logs | ||
// to status-go logger. | ||
func ZapLogger() *zap.Logger { | ||
_initZapLogger.Do(func() { | ||
var err error | ||
_zapLogger, err = NewZapLoggerWithAdapter(log.Root()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
_zapLogger = defaultLogger() | ||
|
||
// forward geth logs to zap logger | ||
_gethLogger := _zapLogger.Named("geth") | ||
log.Root().SetHandler(gethAdapter(_gethLogger)) | ||
}) | ||
return _zapLogger | ||
} | ||
|
||
func defaultLogger() *zap.Logger { | ||
core := NewCore( | ||
defaultEncoder(), | ||
zapcore.AddSync(io.Discard), | ||
zap.NewAtomicLevelAt(zap.InfoLevel), | ||
) | ||
return zap.New(core) | ||
} | ||
|
||
func defaultEncoder() zapcore.Encoder { | ||
encoderConfig := zap.NewDevelopmentEncoderConfig() | ||
encoderConfig.EncodeTime = utcTimeEncoder(encoderConfig.EncodeTime) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
encoderConfig.EncodeCaller = zapcore.FullCallerEncoder | ||
|
||
return zaputil.NewConsoleHexEncoder(encoderConfig) | ||
} | ||
|
||
func utcTimeEncoder(encoder zapcore.TimeEncoder) zapcore.TimeEncoder { | ||
return func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { | ||
encoder(t.UTC(), enc) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why color support removed?