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]

相关文章
|
JSON 中间件 Go
Golang高性能日志库zap + lumberjack 日志切割组件详解
Golang高性能日志库zap + lumberjack 日志切割组件详解
Golang高性能日志库zap + lumberjack 日志切割组件详解
|
JSON Go API
一文搞懂 Golang 高性能日志库 - Zap
一文搞懂 Golang 高性能日志库 - Zap
1259 2
|
JSON Go 开发工具
Golang日志库Zap基本使用
Golang日志库Zap基本使用
326 0
|
1月前
|
存储 安全 Java
【Golang】(4)Go里面的指针如何?函数与方法怎么不一样?带你了解Go不同于其他高级语言的语法
结构体可以存储一组不同类型的数据,是一种符合类型。Go抛弃了类与继承,同时也抛弃了构造方法,刻意弱化了面向对象的功能,Go并非是一个传统OOP的语言,但是Go依旧有着OOP的影子,通过结构体和方法也可以模拟出一个类。
100 1
|
Go
Golang语言之管道channel快速入门篇
这篇文章是关于Go语言中管道(channel)的快速入门教程,涵盖了管道的基本使用、有缓冲和无缓冲管道的区别、管道的关闭、遍历、协程和管道的协同工作、单向通道的使用以及select多路复用的详细案例和解释。
577 4
Golang语言之管道channel快速入门篇
|
Go
Golang语言文件操作快速入门篇
这篇文章是关于Go语言文件操作快速入门的教程,涵盖了文件的读取、写入、复制操作以及使用标准库中的ioutil、bufio、os等包进行文件操作的详细案例。
220 4
Golang语言文件操作快速入门篇
|
Go
Golang语言之gRPC程序设计示例
这篇文章是关于Golang语言使用gRPC进行程序设计的详细教程,涵盖了RPC协议的介绍、gRPC环境的搭建、Protocol Buffers的使用、gRPC服务的编写和通信示例。
485 3
Golang语言之gRPC程序设计示例
|
安全 Go
Golang语言goroutine协程并发安全及锁机制
这篇文章是关于Go语言中多协程操作同一数据问题、互斥锁Mutex和读写互斥锁RWMutex的详细介绍及使用案例,涵盖了如何使用这些同步原语来解决并发访问共享资源时的数据安全问题。
277 4
|
Prometheus Cloud Native Go
Golang语言之Prometheus的日志模块使用案例
这篇文章是关于如何在Golang语言项目中使用Prometheus的日志模块的案例,包括源代码编写、编译和测试步骤。
253 4
Golang语言之Prometheus的日志模块使用案例

推荐镜像

更多