关于golang:聊聊golang的zap的NewDevelopment

46次阅读

共计 5460 个字符,预计需要花费 14 分钟才能阅读完成。

本文次要钻研一下 golang 的 zap 的 NewDevelopment

NewDevelopment

zap@v1.16.0/logger.go

func NewDevelopment(options ...Option) (*Logger, error) {return NewDevelopmentConfig().Build(options...)
}

NewDevelopment 应用 NewDevelopmentConfig 进行 build

NewDevelopmentConfig

zap@v1.16.0/config.go

func NewDevelopmentConfig() Config {
    return Config{Level:            NewAtomicLevelAt(DebugLevel),
        Development:      true,
        Encoding:         "console",
        EncoderConfig:    NewDevelopmentEncoderConfig(),
        OutputPaths:      []string{"stderr"},
        ErrorOutputPaths: []string{"stderr"},
    }
}

NewDevelopmentConfig 创立 Config,其 Level 为 NewAtomicLevelAt(DebugLevel),Development 为 true,Encoding 为 console,EncoderConfig 为 NewDevelopmentEncoderConfig,OutputPaths 及 ErrorOutputPaths 均为 stderr

NewDevelopmentEncoderConfig

zap@v1.16.0/config.go

func NewDevelopmentEncoderConfig() zapcore.EncoderConfig {
    return zapcore.EncoderConfig{
        // Keys can be anything except the empty string.
        TimeKey:        "T",
        LevelKey:       "L",
        NameKey:        "N",
        CallerKey:      "C",
        FunctionKey:    zapcore.OmitKey,
        MessageKey:     "M",
        StacktraceKey:  "S",
        LineEnding:     zapcore.DefaultLineEnding,
        EncodeLevel:    zapcore.CapitalLevelEncoder,
        EncodeTime:     zapcore.ISO8601TimeEncoder,
        EncodeDuration: zapcore.StringDurationEncoder,
        EncodeCaller:   zapcore.ShortCallerEncoder,
    }
}

NewDevelopmentEncoderConfig 创立 zapcore.EncoderConfig,其 LineEnding 为 zapcore.DefaultLineEnding,EncodeLevel 为 zapcore.CapitalLevelEncoder,EncodeTime 为 zapcore.ISO8601TimeEncoder,EncodeDuration 为 zapcore.StringDurationEncoder,EncodeCaller 为 zapcore.ShortCallerEncoder

encoder

zap@v1.16.0/zapcore/encoder.go

const DefaultLineEnding = "\n"

func CapitalLevelEncoder(l Level, enc PrimitiveArrayEncoder) {enc.AppendString(l.CapitalString())
}

func ISO8601TimeEncoder(t time.Time, enc PrimitiveArrayEncoder) {encodeTimeLayout(t, "2006-01-02T15:04:05.000Z0700", enc)
}

func StringDurationEncoder(d time.Duration, enc PrimitiveArrayEncoder) {enc.AppendString(d.String())
}

func ShortCallerEncoder(caller EntryCaller, enc PrimitiveArrayEncoder) {
    // TODO: consider using a byte-oriented API to save an allocation.
    enc.AppendString(caller.TrimmedPath())
}

encoder

zap@v1.16.0/encoder.go

var (errNoEncoderNameSpecified = errors.New("no encoder name specified")

    _encoderNameToConstructor = map[string]func(zapcore.EncoderConfig) (zapcore.Encoder, error){"console": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {return zapcore.NewConsoleEncoder(encoderConfig), nil
        },
        "json": func(encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) {return zapcore.NewJSONEncoder(encoderConfig), nil
        },
    }
    _encoderMutex sync.RWMutex
)

_encoderNameToConstructor 内置了 console、json 两种 encoder

NewConsoleEncoder

zap@v1.16.0/console_encoder.go

func NewConsoleEncoder(cfg EncoderConfig) Encoder {if len(cfg.ConsoleSeparator) == 0 {
        // Use a default delimiter of '\t' for backwards compatibility
        cfg.ConsoleSeparator = "\t"
    }
    return consoleEncoder{newJSONEncoder(cfg, true)}
}

type consoleEncoder struct {*jsonEncoder}

func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer, error) {line := bufferpool.Get()

    // We don't want the entry's metadata to be quoted and escaped (if it's
    // encoded as strings), which means that we can't use the JSON encoder. The
    // simplest option is to use the memory encoder and fmt.Fprint.
    //
    // If this ever becomes a performance bottleneck, we can implement
    // ArrayEncoder for our plain-text format.
    arr := getSliceEncoder()
    if c.TimeKey != "" && c.EncodeTime != nil {c.EncodeTime(ent.Time, arr)
    }
    if c.LevelKey != "" && c.EncodeLevel != nil {c.EncodeLevel(ent.Level, arr)
    }
    if ent.LoggerName != ""&& c.NameKey !="" {
        nameEncoder := c.EncodeName

        if nameEncoder == nil {
            // Fall back to FullNameEncoder for backward compatibility.
            nameEncoder = FullNameEncoder
        }

        nameEncoder(ent.LoggerName, arr)
    }
    if ent.Caller.Defined {
        if c.CallerKey != "" && c.EncodeCaller != nil {c.EncodeCaller(ent.Caller, arr)
        }
        if c.FunctionKey != "" {arr.AppendString(ent.Caller.Function)
        }
    }
    for i := range arr.elems {
        if i > 0 {line.AppendString(c.ConsoleSeparator)
        }
        fmt.Fprint(line, arr.elems[i])
    }
    putSliceEncoder(arr)

    // Add the message itself.
    if c.MessageKey != "" {c.addSeparatorIfNecessary(line)
        line.AppendString(ent.Message)
    }

    // Add any structured context.
    c.writeContext(line, fields)

    // If there's no stacktrace key, honor that; this allows users to force
    // single-line output.
    if ent.Stack != ""&& c.StacktraceKey !="" {line.AppendByte('\n')
        line.AppendString(ent.Stack)
    }

    if c.LineEnding != "" {line.AppendString(c.LineEnding)
    } else {line.AppendString(DefaultLineEnding)
    }
    return line, nil
}

consoleEncoder 内嵌了 *jsonEncoder,其 EncodeEntry 办法通过 getSliceEncoder() 获取 `*sliceArrayEncoder,而后顺次往 arr 增加 time、level、loggerName、caller,最初再增加业务的 message 自身,对于有 stacktrace 还会追加 stacktrace

实例

func developmentDemo() {logger, _ := zap.NewDevelopment()
    defer logger.Sync() // flushes buffer, if any
    sugar := logger.Sugar()
    sugar.Info("this will be logged")
    sugar.Panic("test panic")
}

输入

2020-12-06T23:29:08.081+0800    INFO    log-demo/zap_demo.go:17 this will be logged
2020-12-06T23:29:08.082+0800    PANIC   log-demo/zap_demo.go:18 test panic
main.developmentDemo
        /zap_demo.go:18
main.main
        /zap_demo.go:10
runtime.main
        /usr/local/go/src/runtime/proc.go:204
panic: test panic

goroutine 1 [running]:
go.uber.org/zap/zapcore.(*CheckedEntry).Write(0xc0000f20c0, 0x0, 0x0, 0x0)
        /go/pkg/mod/go.uber.org/zap@v1.16.0/zapcore/entry.go:234 +0x585
go.uber.org/zap.(*SugaredLogger).log(0xc0000fbed0, 0x4, 0x0, 0x0, 0xc0000fbed8, 0x1, 0x1, 0x0, 0x0, 0x0)
        /go/pkg/mod/go.uber.org/zap@v1.16.0/sugar.go:234 +0xf6
go.uber.org/zap.(*SugaredLogger).Panic(...)
        /go/pkg/mod/go.uber.org/zap@v1.16.0/sugar.go:123
main.developmentDemo()
        /zap_demo.go:18 +0x199
main.main()
        /zap_demo.go:10 +0x25
exit status 2

小结

NewDevelopmentEncoderConfig 创立 zapcore.EncoderConfig,其 LineEnding 为 zapcore.DefaultLineEnding,EncodeLevel 为 zapcore.CapitalLevelEncoder,EncodeTime 为 zapcore.ISO8601TimeEncoder,EncodeDuration 为 zapcore.StringDurationEncoder,EncodeCaller 为 zapcore.ShortCallerEncoder

doc

  • zap

正文完
 0