Skip to content

Commit

Permalink
output the internal errors of zap logger to stderr (#24)
Browse files Browse the repository at this point in the history
* provide a new field in Config for users to determine where to store the internal zap logger error
zhaoxinyu authored Dec 7, 2021
1 parent afc726e commit 71a2e58
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -66,6 +66,10 @@ type Config struct {
//
// Values configured here are per-second. See zapcore.NewSampler for details.
Sampling *zap.SamplingConfig `toml:"sampling" json:"sampling"`
// ErrorOutputPath is a path to write internal logger errors to.
// If this field is not set, the internal logger errors will be sent to the same file as in File field.
// Note: if we want to output the logger errors to stderr, we can just set this field to "stderr"
ErrorOutputPath string `toml:"error-output-path" json:"error-output-path"`
}

// ZapProperties records some information about zap.
19 changes: 15 additions & 4 deletions log.go
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ func init() {
// InitLogger initializes a zap logger.
func InitLogger(cfg *Config, opts ...zap.Option) (*zap.Logger, *ZapProperties, error) {
var output zapcore.WriteSyncer
var errOutput zapcore.WriteSyncer
if len(cfg.File.Filename) > 0 {
lg, err := initFileLog(&cfg.File)
if err != nil {
@@ -53,7 +54,17 @@ func InitLogger(cfg *Config, opts ...zap.Option) (*zap.Logger, *ZapProperties, e
}
output = stdOut
}
return InitLoggerWithWriteSyncer(cfg, output, opts...)
if len(cfg.ErrorOutputPath) > 0 {
errOut, _, err := zap.Open([]string{cfg.ErrorOutputPath}...)
if err != nil {
return nil, nil, err
}
errOutput = errOut
} else {
errOutput = output
}

return InitLoggerWithWriteSyncer(cfg, output, errOutput, opts...)
}

func InitTestLogger(t zaptest.TestingT, cfg *Config, opts ...zap.Option) (*zap.Logger, *ZapProperties, error) {
@@ -64,11 +75,11 @@ func InitTestLogger(t zaptest.TestingT, cfg *Config, opts ...zap.Option) (*zap.L
zap.ErrorOutput(writer.WithMarkFailed(true)),
}
opts = append(zapOptions, opts...)
return InitLoggerWithWriteSyncer(cfg, writer, opts...)
return InitLoggerWithWriteSyncer(cfg, writer, writer, opts...)
}

// InitLoggerWithWriteSyncer initializes a zap logger with specified write syncer.
func InitLoggerWithWriteSyncer(cfg *Config, output zapcore.WriteSyncer, opts ...zap.Option) (*zap.Logger, *ZapProperties, error) {
func InitLoggerWithWriteSyncer(cfg *Config, output, errOutput zapcore.WriteSyncer, opts ...zap.Option) (*zap.Logger, *ZapProperties, error) {
level := zap.NewAtomicLevel()
err := level.UnmarshalText([]byte(cfg.Level))
if err != nil {
@@ -84,7 +95,7 @@ func InitLoggerWithWriteSyncer(cfg *Config, output zapcore.WriteSyncer, opts ...
return nil, nil, err
}
core := NewTextCore(encoder, output, level)
opts = append(cfg.buildOptions(output), opts...)
opts = append(cfg.buildOptions(errOutput), opts...)
lg := zap.New(core, opts...)
r := &ZapProperties{
Core: core,

0 comments on commit 71a2e58

Please sign in to comment.