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

Use logfmt instead of terminal format #226

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 30 additions & 10 deletions common/logging/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import (
)

const (
PathFlagName = "log.path"
FileLevelFlagName = "log.level-file"
StdLevelFlagName = "log.level-std"
PathFlagName = "log.path"
FileFormatFlagName = "log.format-file"
FileLevelFlagName = "log.level-file"
StdFormatFlagName = "log.format-std"
StdLevelFlagName = "log.level-std"
)

type Config struct {
Path string
Prefix string
FileLevel string
StdLevel string
Path string
Prefix string
FileLevel string
FileFormat string
StdLevel string
StdFormat string
}

func CLIFlags(envPrefix string, flagPrefix string) []cli.Flag {
Expand All @@ -38,21 +42,37 @@ func CLIFlags(envPrefix string, flagPrefix string) []cli.Flag {
Value: "",
EnvVar: common.PrefixEnvVar(envPrefix, "LOG_PATH"),
},
cli.StringFlag{
Name: common.PrefixFlag(flagPrefix, StdFormatFlagName),
Usage: "Format of the log messages. Accepted options are 'terminal', 'json', and 'logfmt'",
Value: "terminal",
EnvVar: common.PrefixEnvVar(envPrefix, "STD_LOG_FORMAT"),
},
cli.StringFlag{
Name: common.PrefixFlag(flagPrefix, FileFormatFlagName),
Usage: "Format of the log messages. Accepted options are 'terminal', 'json', and 'logfmt'",
Value: "logfmt",
EnvVar: common.PrefixEnvVar(envPrefix, "FILE_LOG_FORMAT"),
},
}
}

func DefaultCLIConfig() Config {
return Config{
Path: "",
FileLevel: "debug",
StdLevel: "debug",
Path: "",
FileFormat: "logfmt",
FileLevel: "debug",
StdFormat: "terminal",
StdLevel: "debug",
}
}

func ReadCLIConfig(ctx *cli.Context, flagPrefix string) Config {
cfg := DefaultCLIConfig()
cfg.StdFormat = ctx.GlobalString(common.PrefixFlag(flagPrefix, StdFormatFlagName))
cfg.StdLevel = ctx.GlobalString(common.PrefixFlag(flagPrefix, StdLevelFlagName))
cfg.FileLevel = ctx.GlobalString(common.PrefixFlag(flagPrefix, FileLevelFlagName))
cfg.FileFormat = ctx.GlobalString(common.PrefixFlag(flagPrefix, FileFormatFlagName))
cfg.Path = ctx.GlobalString(common.PrefixFlag(flagPrefix, PathFlagName))
return cfg
}
33 changes: 31 additions & 2 deletions common/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ func (l *Logger) SetHandler(h log.Handler) {
l.Logger.SetHandler(h)
}

func getStreamHandlerFromFormat(format string) (log.Handler, error) {
switch format {
case "terminal":
return log.StreamHandler(os.Stdout, log.TerminalFormat(false)), nil
case "json":
return log.StreamHandler(os.Stdout, log.JSONFormat()), nil
case "logfmt":
return log.StreamHandler(os.Stdout, log.LogfmtFormat()), nil
default:
return nil, fmt.Errorf("invalid log format: %s", format)
}
}

func getFileHandlerFromFormat(path string, format string) (log.Handler, error) {
switch format {
case "terminal":
return log.FileHandler(path, log.TerminalFormat(false))
case "json":
return log.FileHandler(path, log.JSONFormat())
case "logfmt":
return log.FileHandler(path, log.LogfmtFormat())
default:
return nil, fmt.Errorf("invalid log format: %s", format)
}
}

// GetLogger returns a logger with the specified configuration.
func GetLogger(cfg Config) (common.Logger, error) {
fileLevel, err := log.LvlFromString(cfg.FileLevel)
Expand All @@ -38,10 +64,13 @@ func GetLogger(cfg Config) (common.Logger, error) {
// This was due to it being very expensive to compute origins
// We should evaluate enabling/disabling this based on the flag
log.PrintOrigins(true)
stdh := log.StreamHandler(os.Stdout, log.TerminalFormat(false))
stdh, err := getStreamHandlerFromFormat(cfg.StdFormat)
if err != nil {
return nil, err
}
stdHandler := log.CallerFileHandler(log.LvlFilterHandler(stdLevel, stdh))
if cfg.Path != "" {
fh, err := log.FileHandler(cfg.Path, log.LogfmtFormat())
fh, err := getFileHandlerFromFormat(cfg.Path, cfg.FileFormat)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions core/indexer/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ var _ = Describe("Indexer", func() {
}

logger, err := logging.GetLogger(logging.Config{
StdLevel: "debug",
FileLevel: "debug",
StdFormat: "terminal",
StdLevel: "debug",
FileFormat: "logfmt",
FileLevel: "debug",
})
Expect(err).ToNot(HaveOccurred())

Expand Down
6 changes: 4 additions & 2 deletions core/thegraph/state_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ func TestIndexerIntegration(t *testing.T) {
defer teardown()

logger, err := logging.GetLogger(logging.Config{
StdLevel: "debug",
FileLevel: "debug",
StdFormat: "terminal",
StdLevel: "debug",
FileFormat: "logfmt",
FileLevel: "debug",
})
assert.NoError(t, err)

Expand Down
12 changes: 8 additions & 4 deletions core/thegraph/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func (m *mockChainState) GetOperatorStateByOperator(ctx context.Context, blockNu

func TestIndexedChainState_GetIndexedOperatorState(t *testing.T) {
logger, err := logging.GetLogger(logging.Config{
StdLevel: "debug",
FileLevel: "debug",
StdFormat: "terminal",
StdLevel: "debug",
FileFormat: "logfmt",
FileLevel: "debug",
})
assert.NoError(t, err)

Expand Down Expand Up @@ -104,8 +106,10 @@ func TestIndexedChainState_GetIndexedOperatorState(t *testing.T) {

func TestIndexedChainState_GetIndexedOperatorInfoByOperatorId(t *testing.T) {
logger, err := logging.GetLogger(logging.Config{
StdLevel: "debug",
FileLevel: "debug",
StdFormat: "terminal",
StdLevel: "debug",
FileFormat: "logfmt",
FileLevel: "debug",
})
assert.NoError(t, err)

Expand Down
Loading