golang之zap探索

golang之zap探索,第1张

概述GO Web 编程:http://www.kancloud.cn/kancloud/web-application-with-golang/44105 我的golang工程:https://github.com/javahongxi/go.web.red.git   uber zap test     package mainimport ( "go.uber.org/zap"

GO Web 编程:http://www.kancloud.cn/kancloud/web-application-with-golang/44105

我的golang工程:https://github.com/javahongxi/go.web.red.git

uber zap test

package mainimport (    "go.uber.org/zap"    "time"    "go.uber.org/zap/zapcore"    "net/http"    "bufio"    "os")func main() {    //logger,_ := zap.NewProduction()    //defer logger.Sync() // flushes buffer,if any    //sugar := logger.Sugar()    //url := "www.baIDu.com"    //sugar.Infow("Failed to fetch URL.",//    // Structured context as loosely-typed key-value pairs.    //    "url",url,//    "attempt",3,//    "backoff",time.Second,//)    //sugar.Infof("Failed to fetch URL: %s",url)    encoder_cfg := zapcore.EncoderConfig{        // Keys can be anything except the empty string.        TimeKey:        "T",LevelKey:       "L",nameKey:        "N",CallerKey:      "C",MessageKey:     "M",StacktraceKey:  "S",lineEnding:     zapcore.DefaultlineEnding,EncodeLevel:    zapcore.CAPItalLevelEncoder,EncodeTime:     TimeEncoder,EncodeDuration: zapcore.StringDurationEncoder,EncodeCaller:   zapcore.ShortCallerEncoder,}    Curr_level := zap.NewAtomicLevelAt(zap.DeBUGLevel)    go func() {        http.ListenAndServe(":9090",&Curr_level)    }()    custom_cfg := zap.Config{        Level:            Curr_level,Development:      true,EnCoding:         "console",EncoderConfig:    encoder_cfg,OutputPaths:      []string{"stderr","qihu-secret-business.log"},ErrorOutputPaths: []string{"stderr"},}    url := "www.baIDu.com"    logger,_ := custom_cfg.Build()    new_logger := logger.named("qihu-secret-business")    defer new_logger.Sync()    new_logger.DeBUG("adv_event_type_handle",zap.String("a","1"))    new_logger.Info("adv_event_type_handle",// Structured context as strongly-typed FIEld values.        zap.String("url",url),zap.Int("attempt",3),zap.Duration("backoff",time.Second),)    reader := bufio.NewReader(os.Stdin)    for {        data,_,_ := reader.Readline()        command := string(data)        if command == "start" {            break        }    }    new_logger.DeBUG("adv_event_type_handle",zap.String("b","2"))    new_logger.Info("adv_event_type_handle",zap.String("c","3"))}func TimeEncoder(t time.Time,enc zapcore.PrimitiveArrayEncoder) {    enc.AppendString("[" + t.Format("2006-01-02 15:04:05") + "]")}

[2017-05-23 12:08:26]DEBUGadv_os_businesszap_t/zap_t.go:63adv_event_type_handle{"a": "1"}

[2017-05-23 12:08:26]INFOadv_os_businesszap_t/zap_t.go:69adv_event_type_handle{"url": "www.baIDu.com","attempt": 3,"backoff": "1s"}

其他:进程启动管理supervisor,日志分割logrotate,性能监控https://github.com/grafana/grafana

/etc/supervisor.conf

[program:simpletest]

command=/home/shenhongxi/go/bin/a

autostart=true

autorestart=true

startsecs=10

;logfile=/home/shenhongxi/log/simpletest.log

/etc/logrotate.d/simpletest

/home/shenhongxi/go/*.log {

missingok

notifempty

nocompress

daily

rotate 5

size 204800

}

总结

以上是内存溢出为你收集整理的golang之zap探索全部内容,希望文章能够帮你解决golang之zap探索所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1275537.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存