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

[log] optimize console log format #3499

Merged
merged 20 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 16 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
33 changes: 26 additions & 7 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,38 @@ func InitLoggers(globalCfg GlobalConfig, subCfgs map[string]GlobalConfig, opts .
if globalCfg.EcsIntegration {
cfg.Zap.EncoderConfig = ecszap.ECSCompatibleEncoderConfig(cfg.Zap.EncoderConfig)
}
logger, err := cfg.Zap.Build(opts...)
if err != nil {
return err
}

var cores []zapcore.Core
if cfg.StderrRedirectFile != nil {
stderrF, err := os.OpenFile(*cfg.StderrRedirectFile, os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0600)
if err != nil {
return err
}
if err := redirectStderr(stderrF); err != nil {
return err
}

cores = append(cores, zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.Zap.EncoderConfig),
zapcore.AddSync(stderrF),
cfg.Zap.Level))
}

consoleCfg := zap.NewDevelopmentConfig()
consoleCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
cores = append(cores, zapcore.NewCore(
zapcore.NewConsoleEncoder(consoleCfg.EncoderConfig),
zapcore.Lock(os.Stderr),
zap.InfoLevel))

Copy link
Member

Choose a reason for hiding this comment

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

so looks like add a new console encoder to process the output in human-friendly format?

i assume you already run locally to verify the log format in the file has remained the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, add console encoder. I run in test-net, log format is the same with older

coreOpt := zap.WrapCore(func(zapcore.Core) zapcore.Core {
return zapcore.NewTee(cores...)
})
var buildOpts []zap.Option
buildOpts = append(buildOpts, coreOpt)
buildOpts = append(buildOpts, opts...)
logger, err := cfg.Zap.Build(buildOpts...)
Copy link
Member

Choose a reason for hiding this comment

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

cfg.Zap.Build(coreOpt, opts)?

Copy link
Contributor Author

@huof6829 huof6829 Aug 23, 2022

Choose a reason for hiding this comment

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

no. build error because type of opt is []zap.Option,

if err != nil {
return err
}

_logMu.Lock()
if name == _globalLoggerName {
_globalCfg = cfg
Expand Down
26 changes: 0 additions & 26 deletions pkg/log/redirectStderr_armOS.go

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/log/redirectStderr_darwin_arm64.go

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/log/redirectStderr_other.go

This file was deleted.

44 changes: 0 additions & 44 deletions pkg/log/redirectStderr_windows.go

This file was deleted.