美文网首页
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

相关文章

  • golang高性能日志库zap配置示例

    golang高性能日志库zap配置示例 zap是uber开源的Go高性能日志库,gitlab地址 安装 请注意,z...

  • golang的zap怎么使用

    zap是一个高性能日志库,下面简单介绍一下zap的使用。 1.下载zap包 因为zap是uber开源的,zap内使...

  • Zap日志库

    参考 https://github.com/uber-go/zap https://godoc.org/go.ub...

  • 日志库使用zap

    lumberjack的Logger结构体说明 Filename 写日志的文件名称 MaxSize 每个日志文件长...

  • 记ELK丢消息问题

    第一次用elk,应用内部日志使用uber的zap日志库打印,通过systemd系统服务启动,通过journalbe...

  • golang日志框架zap简洁配置

    golang日志框架zap简洁配置 $ 前言 zap是uber开源的一款高性能日志组件框架 $ 配置目标 日志按天...

  • Go-zap

    本文先介绍了Go语言原生的日志库的使用,然后详细介绍了非常流行的Uber开源的zap日志库,同时介绍了如何搭配Lu...

  • 在Go语言项目中使用Zap日志库

    本文先介绍了Go语言原生的日志库的使用,然后详细介绍了非常流行的Uber开源的zap日志库,同时介绍了如何搭配Lu...

  • GO Log统一接口

    一、问题 go日志库有很多:zap、 Logrus等,它们没有统一的接口。作为中间件提供方,中间件的日志怎么输出到...

  • go.uber.org/zap

    参考资料 深度 | 从Go高性能日志库zap看如何实现高性能Go组件[https://mp.weixin.qq.c...

网友评论

      本文标题:Zap日志库

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