美文网首页
Zap日志库

Zap日志库

作者: 8411e9740257 | 来源:发表于2018-05-16 15:24 被阅读0次

    参考

    安装

    go get -u github.com/uber-go/zap

    示例

    NewProduction

    func main() {
        var i8 int8 = 10
        var str = "string"
        any := struct {
            I int `json:"int"`
            S string
        }{
            I: 1,
            S: "str",
        }
    
        pl, _ := zap.NewProduction()
    
        pl.With(zap.Namespace("namespace")).Named("name").Warn("NewProduction name", zap.Any("any", any))
        //pl.Fatal("NewProduction")
        go func() {
            defer func() {
                if err := recover(); err != nil {
                    log.Println(err)
                }
            }()
            pl.Panic("NewProduction", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        }()
        pl.DPanic("NewProduction", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        pl.Error("NewProduction", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        pl.Warn("NewProduction", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        pl.Info("NewProduction", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        pl.With(zap.Int8("i8", i8)).Info("NewProduction", zap.Any("any", any), zap.String("str", str))
        pl.Info("NewProduction", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str), zap.Namespace("namespace"))
        pl.Info("NewProduction", zap.Namespace("namespace"), zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        pl.Debug("NewProduction", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
    }
    

    结果

    {"level":"warn","ts":1526455361.749451,"logger":"name","caller":"helloworld/cmd.go:62","msg":"NewProduction name","namespace":{"any":{"int":1,"S":"str"}}}
    {"level":"dpanic","ts":1526455361.7494962,"caller":"helloworld/cmd.go:74","msg":"NewProduction","i8":10,"any":{"int":1,"S":"str"},"str":"string","stacktrace":"main.main\n\t/home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:74\nruntime.main\n\t/opt/golang/go1.10.1/src/runtime/proc.go:198"}
    {"level":"error","ts":1526455361.749533,"caller":"helloworld/cmd.go:75","msg":"NewProduction","i8":10,"any":{"int":1,"S":"str"},"str":"string","stacktrace":"main.main\n\t/home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:75\nruntime.main\n\t/opt/golang/go1.10.1/src/runtime/proc.go:198"}
    {"level":"warn","ts":1526455361.7495432,"caller":"helloworld/cmd.go:76","msg":"NewProduction","i8":10,"any":{"int":1,"S":"str"},"str":"string"}
    {"level":"info","ts":1526455361.749549,"caller":"helloworld/cmd.go:77","msg":"NewProduction","i8":10,"any":{"int":1,"S":"str"},"str":"string"}
    {"level":"info","ts":1526455361.7495563,"caller":"helloworld/cmd.go:78","msg":"NewProduction","i8":10,"any":{"int":1,"S":"str"},"str":"string"}
    {"level":"info","ts":1526455361.7495608,"caller":"helloworld/cmd.go:79","msg":"NewProduction","i8":10,"any":{"int":1,"S":"str"},"str":"string","namespace":{}}
    {"level":"info","ts":1526455361.749565,"caller":"helloworld/cmd.go:80","msg":"NewProduction","namespace":{"i8":10,"any":{"int":1,"S":"str"},"str":"string"}}
    

    NewDevelopment

    func main() {
        var i8 int8 = 10
        var str = "string"
        any := struct {
            I int `json:"int"`
            S string
        }{
            I: 1,
            S: "str",
        }
    
        dl, _ := zap.NewDevelopment()
    
        //dl.Fatal("NewDevelopment")
        go func() {
            defer func() {
                if err := recover(); err != nil {
                    log.Println(err)
                }
            }()
            dl.Panic("NewDevelopment", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        }()
        go func() {
            defer func() {
                if err := recover(); err != nil {
                    log.Println(err)
                }
            }()
            dl.DPanic("NewDevelopment", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        }()
        dl.Error("NewDevelopment", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        dl.Warn("NewDevelopment", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        dl.Info("NewDevelopment", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))
        dl.Debug("NewDevelopment", zap.Int8("i8", i8), zap.Any("any", any), zap.String("str", str))    
    }
    

    结果

    2018-05-16T15:23:44.008+0800    ERROR   helloworld/cmd.go:102   NewDevelopment  {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    main.main
        /home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:102
    runtime.main
        /opt/golang/go1.10.1/src/runtime/proc.go:198
    2018-05-16T15:23:44.008+0800    PANIC   helloworld/cmd.go:92    NewDevelopment  {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    main.main.func1
        /home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:92
    2018-05-16T15:23:44.008+0800    WARN    helloworld/cmd.go:103   NewDevelopment  {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    main.main
        /home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:103
    runtime.main
        /opt/golang/go1.10.1/src/runtime/proc.go:198
    2018-05-16T15:23:44.008+0800    INFO    helloworld/cmd.go:104   NewDevelopment  {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    2018/05/16 15:23:44 NewDevelopment
    2018-05-16T15:23:44.008+0800    DEBUG   helloworld/cmd.go:105   NewDevelopment  {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    

    NewDevelopment Sugar

    func main() {
        var i8 int8 = 10
        var str = "string"
        any := struct {
            I int `json:"int"`
            S string
        }{
            I: 1,
            S: "str",
        }
    
        dl, _ := zap.NewDevelopment()
        dls := dl.Sugar()
    
        //dls.Fatal("Sugar NewDevelopment", "i8", i8, "any", any, "str", str)
        go func() {
            defer func() {
                if err := recover(); err != nil {
                    log.Println(err)
                }
            }()
            dls.Panicw("Sugar NewDevelopment", "i8", i8, "any", any, "str", str)
        }()
        go func() {
            defer func() {
                if err := recover(); err != nil {
                    log.Println(err)
                }
            }()
            dls.DPanicw("Sugar NewDevelopment", "i8", i8, "any", any, "str", str)
        }()
        dls.Errorw("Sugar NewDevelopment", "i8", i8, "any", any, "str", str)
        dls.Warnw("Sugar NewDevelopment", "i8", i8, "any", any, "str", str)
        dls.Infow("Sugar NewDevelopment", "i8", i8, "any", any, "str", str)
        dls.Debugw("Sugar NewDevelopment", "i8", i8, "any", any, "str", str)
    
    
        go func() {
            defer func() {
                if err := recover(); err != nil {
                    log.Println(err)
                }
            }()
            dls.Panic("Sugar NewDevelopment", "i8", i8, "any", any, "str", str)
        }()
        go func() {
            defer func() {
                if err := recover(); err != nil {
                    log.Println(err)
                }
            }()
            dls.DPanic("Sugar NewDevelopment", "i8", i8, "any", any, "str", str)
        }()
        dls.Error("Sugar NewDevelopment", "i8", i8, "any", any, "str", str, "end")
        dls.Warn("Sugar NewDevelopment", "i8", i8, "any", any, "str", str, "end")
        dls.Info("Sugar NewDevelopment", "i8", i8, "any", any, "str", str, "end")
        dls.Debug("Sugar NewDevelopment", "i8", i8, "any", any, "str", str, "end") 
    }
    

    结果

    2018-05-16T15:24:30.878+0800    ERROR   helloworld/cmd.go:126   Sugar NewDevelopment    {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    main.main
        /home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:126
    runtime.main
        /opt/golang/go1.10.1/src/runtime/proc.go:198
    2018-05-16T15:24:30.878+0800    DPANIC  helloworld/cmd.go:124   Sugar NewDevelopment    {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    main.main.func2
        /home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:124
    2018/05/16 15:24:30 Sugar NewDevelopment
    2018-05-16T15:24:30.878+0800    WARN    helloworld/cmd.go:127   Sugar NewDevelopment    {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    main.main
        /home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:127
    runtime.main
        /opt/golang/go1.10.1/src/runtime/proc.go:198
    2018-05-16T15:24:30.878+0800    INFO    helloworld/cmd.go:128   Sugar NewDevelopment    {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    2018-05-16T15:24:30.878+0800    DEBUG   helloworld/cmd.go:129   Sugar NewDevelopment    {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    2018-05-16T15:24:30.878+0800    ERROR   helloworld/cmd.go:148   Sugar NewDevelopmenti810any{1 str}strstringend
    main.main
        /home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:148
    runtime.main
        /opt/golang/go1.10.1/src/runtime/proc.go:198
    2018-05-16T15:24:30.878+0800    PANIC   helloworld/cmd.go:116   Sugar NewDevelopment    {"i8": 10, "any": {"int":1,"S":"str"}, "str": "string"}
    main.main.func1
        /home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:116
    2018/05/16 15:24:30 Sugar NewDevelopment
    2018-05-16T15:24:30.878+0800    WARN    helloworld/cmd.go:149   Sugar NewDevelopmenti810any{1 str}strstringend
    main.main
        /home/obe/Documents/coding/golang/src/gitee.com/microsevice/helloworld/cmd.go:149
    runtime.main
        /opt/golang/go1.10.1/src/runtime/proc.go:198
    2018-05-16T15:24:30.878+0800    INFO    helloworld/cmd.go:150   Sugar NewDevelopmenti810any{1 str}strstringend
    2018-05-16T15:24:30.878+0800    DEBUG   helloworld/cmd.go:151   Sugar NewDevelopmenti810any{1 str}strstringend
    

    相关文章

      网友评论

          本文标题:Zap日志库

          本文链接:https://www.haomeiwen.com/subject/vnipdftx.html