Skip to content
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

Open
wants to merge 2 commits into
base: feat/categorized-logging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 12 additions & 15 deletions cmd/node-canary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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)
}
}
Expand Down
32 changes: 16 additions & 16 deletions cmd/ping-community/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/google/uuid"
"golang.org/x/crypto/ssh/terminal"

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

Expand All @@ -40,17 +39,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 +71,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why color support removed?

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 +243,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
36 changes: 18 additions & 18 deletions cmd/populate-db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/google/uuid"
"golang.org/x/crypto/ssh/terminal"

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

Expand Down Expand Up @@ -47,19 +46,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 +80,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 +287,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
30 changes: 15 additions & 15 deletions cmd/spiff-workflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/google/uuid"
"golang.org/x/crypto/ssh/terminal"

"github.com/ethereum/go-ethereum/log"
gethmetrics "github.com/ethereum/go-ethereum/metrics"
Expand Down Expand Up @@ -42,16 +41,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 +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 @@ -185,8 +186,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
6 changes: 1 addition & 5 deletions cmd/status-backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package main
import (
"flag"
stdlog "log"
"os"

"golang.org/x/crypto/ssh/terminal"

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

Expand All @@ -24,8 +21,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
12 changes: 6 additions & 6 deletions cmd/statusd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 cmds.
Or ensure that it's not used anywhere. And mark the commit as a breaking change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls don't remove it,I like the colorful output. Especially, we will frequently use the status backend server now.
image

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

Expand Down Expand Up @@ -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)
}
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ require (
golang.org/x/mod v0.20.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.3.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2720,8 +2720,6 @@ golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
}
}
Loading