Skip to content

Commit

Permalink
chore(logging)_: switch to zap.Logger as central logger
Browse files Browse the repository at this point in the history
Set zap.Logger as the  primary logger for status-go. All geth logs are
now proxied through zap.Logger.

closes: #6029
  • Loading branch information
osmaczko committed Nov 4, 2024
1 parent 8dd1609 commit c6ca8b4
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 406 deletions.
2 changes: 1 addition & 1 deletion api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func (b *GethStatusBackend) setupLogSettings() error {
MaxBackups: b.config.LogMaxBackups,
CompressRotated: b.config.LogCompressRotated,
}
if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil {
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
return err
}
return nil
Expand Down
25 changes: 12 additions & 13 deletions cmd/node-canary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,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() {
Expand All @@ -58,12 +57,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)
}
}
Expand Down
31 changes: 16 additions & 15 deletions cmd/ping-community/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ 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")
ipcEnabled = flag.Bool("ipc", false, "Enable IPC RPC endpoint")
ipcFile = flag.String("ipcfile", "", "Set IPC file path")
seedPhrase = flag.String("seed-phrase", "", "Seed phrase")
version = flag.Bool("version", false, "Print version and dump configuration")
communityID = flag.String("community-id", "", "The id of the community")
shardCluster = flag.Int("shard-cluster", shard.MainStatusShardCluster, "The shard cluster in which the of the community is published")
shardIndex = flag.Int("shard-index", shard.DefaultShardIndex, "The shard index in which the community is published")
chatID = flag.String("chat-id", "", "The id of the chat")
configFiles configFlags
logLevel = flag.String("log", "", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
ipcEnabled = flag.Bool("ipc", false, "Enable IPC RPC endpoint")
ipcFile = flag.String("ipcfile", "", "Set IPC file path")
seedPhrase = flag.String("seed-phrase", "", "Seed phrase")
version = flag.Bool("version", false, "Print version and dump configuration")
communityID = flag.String("community-id", "", "The id of the community")
shardCluster = flag.Int("shard-cluster", shard.MainStatusShardCluster, "The shard cluster in which the of the community is published")
shardIndex = flag.Int("shard-index", shard.DefaultShardIndex, "The shard index in which the community is published")
chatID = flag.String("chat-id", "", "The id of the chat")

dataDir = flag.String("dir", getDefaultDataDir(), "Directory used by node to store data")
networkID = flag.Int(
Expand All @@ -73,8 +72,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)
}

Expand Down Expand Up @@ -242,8 +244,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)
}
}
Expand Down
35 changes: 18 additions & 17 deletions cmd/populate-db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,18 @@ 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")
ipcEnabled = flag.Bool("ipc", false, "Enable IPC RPC endpoint")
ipcFile = flag.String("ipcfile", "", "Set IPC file path")
seedPhrase = flag.String("seed-phrase", "", "Seed phrase")
version = flag.Bool("version", false, "Print version and dump configuration")
nAddedContacts = flag.Int("added-contacts", 100, "Number of added contacts to create")
nContacts = flag.Int("contacts", 100, "Number of contacts to create")
nPublicChats = flag.Int("public-chats", 5, "Number of public chats")
nCommunities = flag.Int("communities", 5, "Number of communities")
nMessages = flag.Int("number-of-messages", 0, "Number of messages for each chat")
nOneToOneChats = flag.Int("one-to-one-chats", 5, "Number of one to one chats")
configFiles configFlags
logLevel = flag.String("log", "", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
ipcEnabled = flag.Bool("ipc", false, "Enable IPC RPC endpoint")
ipcFile = flag.String("ipcfile", "", "Set IPC file path")
seedPhrase = flag.String("seed-phrase", "", "Seed phrase")
version = flag.Bool("version", false, "Print version and dump configuration")
nAddedContacts = flag.Int("added-contacts", 100, "Number of added contacts to create")
nContacts = flag.Int("contacts", 100, "Number of contacts to create")
nPublicChats = flag.Int("public-chats", 5, "Number of public chats")
nCommunities = flag.Int("communities", 5, "Number of communities")
nMessages = flag.Int("number-of-messages", 0, "Number of messages for each chat")
nOneToOneChats = flag.Int("one-to-one-chats", 5, "Number of one to one chats")

dataDir = flag.String("dir", getDefaultDataDir(), "Directory used by node to store data")
networkID = flag.Int(
Expand All @@ -82,8 +81,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)
}

Expand Down Expand Up @@ -286,8 +288,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)
}
}
Expand Down
29 changes: 15 additions & 14 deletions cmd/spiff-workflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ const (
)

var (
configFiles configFlags
logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
logWithoutColors = flag.Bool("log-without-color", false, "Disables log colors")
seedPhrase = flag.String("seed-phrase", "", "Seed phrase")
version = flag.Bool("version", false, "Print version and dump configuration")
apiModules = flag.String("api-modules", "wakuext,ext,waku,ens", "API modules to enable in the HTTP server")
pprofEnabled = flag.Bool("pprof", false, "Enable runtime profiling via pprof")
pprofPort = flag.Int("pprof-port", 52525, "Port for runtime profiling via pprof")
metricsEnabled = flag.Bool("metrics", false, "Expose ethereum metrics with debug_metrics jsonrpc call")
metricsPort = flag.Int("metrics-port", 9305, "Port for the Prometheus /metrics endpoint")
configFiles configFlags
logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
seedPhrase = flag.String("seed-phrase", "", "Seed phrase")
version = flag.Bool("version", false, "Print version and dump configuration")
apiModules = flag.String("api-modules", "wakuext,ext,waku,ens", "API modules to enable in the HTTP server")
pprofEnabled = flag.Bool("pprof", false, "Enable runtime profiling via pprof")
pprofPort = flag.Int("pprof-port", 52525, "Port for runtime profiling via pprof")
metricsEnabled = flag.Bool("metrics", false, "Expose ethereum metrics with debug_metrics jsonrpc call")
metricsPort = flag.Int("metrics-port", 9305, "Port for the Prometheus /metrics endpoint")

dataDir = flag.String("dir", getDefaultDataDir(), "Directory used by node to store data")
networkID = flag.Int(
Expand All @@ -74,8 +73,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)
}

Expand Down Expand Up @@ -185,8 +187,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)
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/status-backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ func init() {
MobileSystem: false,
Level: "INFO",
}
colors := terminal.IsTerminal(int(os.Stdin.Fd()))
if err := logutils.OverrideRootLogWithConfig(logSettings, colors); err != nil {
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
stdlog.Fatalf("failed to initialize log: %v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/status-cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func setupLogger(file string) *zap.Logger {
MaxBackups: 3,
CompressRotated: true,
}
if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil {
if err := logutils.OverrideRootLoggerWithConfig(logSettings); err != nil {
zap.S().Fatalf("Error initializing logger: %v", err)
}
return logutils.ZapLogger()
Expand Down
11 changes: 6 additions & 5 deletions cmd/statusd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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")
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")
Expand Down Expand Up @@ -100,8 +99,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)
}

Expand Down Expand Up @@ -400,8 +402,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)
}
}
Expand Down
8 changes: 8 additions & 0 deletions logutils/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func (core *Core) Enabled(lvl zapcore.Level) bool {
return core.level.Enabled(lvl)
}

func (core *Core) Level() zapcore.Level {
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 {
Expand Down
39 changes: 32 additions & 7 deletions logutils/logger.go
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)
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)
}
}
18 changes: 0 additions & 18 deletions logutils/logger_test.go

This file was deleted.

13 changes: 0 additions & 13 deletions logutils/logrotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package logutils
import (
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"

"github.com/ethereum/go-ethereum/log"
)

// FileOptions are all options supported by internal rotation module.
Expand All @@ -19,17 +17,6 @@ type FileOptions struct {
Compress bool
}

// FileHandlerWithRotation instantiates log.Handler with a configured rotation
func FileHandlerWithRotation(opts FileOptions, format log.Format) log.Handler {
logger := &lumberjack.Logger{
Filename: opts.Filename,
MaxSize: opts.MaxSize,
MaxBackups: opts.MaxBackups,
Compress: opts.Compress,
}
return log.StreamHandler(logger, format)
}

// ZapSyncerWithRotation creates a zapcore.WriteSyncer with a configured rotation
func ZapSyncerWithRotation(opts FileOptions) zapcore.WriteSyncer {
return zapcore.AddSync(&lumberjack.Logger{
Expand Down
Loading

0 comments on commit c6ca8b4

Please sign in to comment.