golang之zap探索 - Go语言中文社区

golang之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

 

 

Go代码   收藏代码
  1. package main  
  2.   
  3. import (  
  4.     "go.uber.org/zap"  
  5.     "time"  
  6.     "go.uber.org/zap/zapcore"  
  7.     "net/http"  
  8.     "bufio"  
  9.     "os"  
  10. )  
  11.   
  12. func main() {  
  13.     //logger, _ := zap.NewProduction()  
  14.     //defer logger.Sync() // flushes buffer, if any  
  15.     //sugar := logger.Sugar()  
  16.     //url := "www.baidu.com"  
  17.     //sugar.Infow("Failed to fetch URL.",  
  18.     //    // Structured context as loosely-typed key-value pairs.  
  19.     //    "url", url,  
  20.     //    "attempt"3,  
  21.     //    "backoff", time.Second,  
  22.     //)  
  23.     //sugar.Infof("Failed to fetch URL: %s", url)  
  24.   
  25.     encoder_cfg := zapcore.EncoderConfig{  
  26.         // Keys can be anything except the empty string.  
  27.         TimeKey:        "T",  
  28.         LevelKey:       "L",  
  29.         NameKey:        "N",  
  30.         CallerKey:      "C",  
  31.         MessageKey:     "M",  
  32.         StacktraceKey:  "S",  
  33.         LineEnding:     zapcore.DefaultLineEnding,  
  34.         EncodeLevel:    zapcore.CapitalLevelEncoder,  
  35.         EncodeTime:     TimeEncoder,  
  36.         EncodeDuration: zapcore.StringDurationEncoder,  
  37.         EncodeCaller:   zapcore.ShortCallerEncoder,  
  38.     }  
  39.   
  40.     Curr_level := zap.NewAtomicLevelAt(zap.DebugLevel)  
  41.   
  42.     go func() {  
  43.         http.ListenAndServe(":9090", &Curr_level)  
  44.     }()  
  45.   
  46.   
  47.     custom_cfg := zap.Config{  
  48.         Level:            Curr_level,  
  49.         Development:      true,  
  50.         Encoding:         "console",  
  51.         EncoderConfig:    encoder_cfg,  
  52.         OutputPaths:      []string{"stderr""qihu-secret-business.log"},  
  53.         ErrorOutputPaths: []string{"stderr"},  
  54.     }  
  55.   
  56.   
  57.     url := "www.baidu.com"  
  58.   
  59.     logger, _ := custom_cfg.Build()  
  60.     new_logger := logger.Named("qihu-secret-business")  
  61.     defer new_logger.Sync()  
  62.   
  63.     new_logger.Debug("adv_event_type_handle", zap.String("a""1"))  
  64.     new_logger.Info("adv_event_type_handle",  
  65.         // Structured context as strongly-typed Field values.  
  66.         zap.String("url", url),  
  67.         zap.Int("attempt"3),  
  68.         zap.Duration("backoff", time.Second),  
  69.     )  
  70.   
  71.     reader := bufio.NewReader(os.Stdin)  
  72.     for {  
  73.         data, _, _ := reader.ReadLine()  
  74.         command := string(data)  
  75.         if command == "start" {  
  76.             break  
  77.         }  
  78.     }  
  79.   
  80.   
  81.     new_logger.Debug("adv_event_type_handle", zap.String("b""2"))  
  82.     new_logger.Info("adv_event_type_handle", zap.String("c""3"))  
  83. }  
  84.   
  85. func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {  
  86.     enc.AppendString("[" + t.Format("2006-01-02 15:04:05") + "]")  
  87. }  

 

 

[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

}


原文链接:[http://wely.iteye.com/blog/2375584]

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_33813128/article/details/90499299
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-16 22:59:27
  • 阅读 ( 967 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢