1. package main
    2. import (
    3. "fmt"
    4. ctrl "sigs.k8s.io/controller-runtime"
    5. "sigs.k8s.io/controller-runtime/pkg/log/zap"
    6. )
    7. func main() {
    8. ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
    9. logger := ctrl.Log.WithName("log name").WithName("a").WithValues("a key", "a value")
    10. logger.Info("log msg", "b key", "b value")
    11. logger.Error(fmt.Errorf("log error"), "c key", "c value")
    12. }

    当使用dev模式时:

    2020-05-13T08:36:10.875+0800    INFO    log name.a      log msg {"a key": "a value", "b key": "b value"}
    2020-05-13T08:36:10.973+0800    DPANIC  log name.a      odd number of arguments passed as key-value pairs for logging   {"a key": "a value", "ignored key": "c value"}
    github.com/go-logr/zapr.handleFields
            D:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/vendor/github.com/go-logr/zapr/zapr.go:106
    github.com/go-logr/zapr.(*zapLogger).Error
            D:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/vendor/github.com/go-logr/zapr/zapr.go:129
    main.main
            D:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/bin/main.go:13
    runtime.main
            D:/Program Files/Go/go135/src/runtime/proc.go:203
    
    panic: odd number of arguments passed as key-value pairs for logging
    
    goroutine 1 [running]:
    go.uber.org/zap/zapcore.(*CheckedEntry).Write(0xc00030e000, 0xc0003000c0, 0x1, 0x1)
            D:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/vendor/go.uber.org/zap/zapcore/entry.go:229 +0x54d
    go.uber.org/zap.(*Logger).DPanic(0xc0002e0cc0, 0x138bfa8, 0x3d, 0xc0003000c0, 0x1, 0x1)
            D:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/vendor/go.uber.org/zap/logger.go:215 +0x86
    github.com/go-logr/zapr.handleFields(0xc0002e0cc0, 0xc0002cc2b0, 0x1, 0x1, 0xc000300040, 0x1, 0x1, 0xc0002cc2a0, 0x10, 0x10)
            D:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/vendor/github.com/go-logr/zapr/zapr.go:106 +0x5d5
    github.com/go-logr/zapr.(*zapLogger).Error(0xc0002ee1e0, 0x14dab20, 0xc0002cc2a0, 0x1348ef4, 0x5, 0xc0002cc2b0, 0x1, 0x1)
            D:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/vendor/github.com/go-logr/zapr/zapr.go:129 +0x245
    main.main()
            D:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/bin/main.go:13 +0x2cb
    

    使用生产模式时,以json格式输出

    {"level":"info","ts":1589330294.632698,"logger":"log name.a","msg":"log msg","a key":"a value","b key":"b value"}
    {"level":"dpanic","ts":1589330294.6336956,"logger":"log name.a","msg":"odd number of arguments passed as key-value pairs for logging","a key":"a value","ignored key":"c value","sta
    cktrace":"github.com/go-logr/zapr.handleFields\n\tD:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/vendor/github.com/go-logr/zapr/zapr.go:
    106\ngithub.com/go-logr/zapr.(*zapLogger).Error\n\tD:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/vendor/github.com/go-logr/zapr/zapr.go
    :129\nmain.main\n\tD:/SoftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/bin/main.go:13\nruntime.main\n\tD:/Program Files/Go/go135/src/runtime/
    proc.go:203"}
    {"level":"error","ts":1589330294.6336956,"logger":"log name.a","msg":"c key","a key":"a value","error":"log error","stacktrace":"github.com/go-logr/zapr.(*zapLogger).Error\n\tD:/So
    ftwareAndProgram/program/Go/Development/src/harmonycloud.cn/calico-fixedip-controller/vendor/github.com/go-logr/zapr/zapr.go:128\nmain.main\n\tD:/SoftwareAndProgram/program/Go/Deve
    lopment/src/harmonycloud.cn/calico-fixedip-controller/bin/main.go:13\nruntime.main\n\tD:/Program Files/Go/go135/src/runtime/proc.go:203"}