Zap日志库

参考

安装

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    小迈克阅读 3,034评论 1 3
  • 偶然发现,用过一些,分享给大家 { "XcodeChaJian": [ { "Dname":"...
    MonkeyHan阅读 6,558评论 0 4
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,546评论 6 427
  • 今天看到一句话, 我们时间应该花在两件事情上: 1. 读有意义的书 2.过值得写成书的生活 记得某个周六,我废寝忘...
    李大刀阅读 623评论 0 50
  • 值得纪念的一段经历。 这真实的经历关乎一个国家,一段历史,一个事件,也关乎民族尊严,国民责任。 Christian...
    Jing_靖阅读 350评论 1 1