go:用zap和go-file-rotatelogs实现日志的记录和日志按时间分割

一,安装zap和go-file-rotatelogs两个库:

   1,安装zap:

liuhongdi@ku:/data/liuhongdi/zaplog$ go get -u go.uber.org/zap

  2,安装go-file-rotatelogs

liuhongdi@ku:/data/liuhongdi/zaplog2$ go get -u github.com/lestrrat/go-file-rotatelogs

  3,  go-file-rotatelogs的源码地址:

https://github.com/lestrrat/go-file-rotatelogs 

 

说明:刘宏缔的go森林是一个专注golang的博客,
          地址:https://blog.csdn.net/weixin_43881017

说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,演示项目的相关信息

1,项目地址:

   https://github.com/liuhongdi/zaplog2

 

三,代码说明

main.go

  1.  
    package main
  2.  
     
  3.  
    import (
  4.  
    "fmt"
  5.  
    rotatelogs "github.com/lestrrat/go-file-rotatelogs"
  6.  
    "go.uber.org/zap"
  7.  
    "go.uber.org/zap/zapcore"
  8.  
    "io"
  9.  
    "net/http"
  10.  
    "time"
  11.  
    )
  12.  
     
  13.  
    var sugarLogger *zap.SugaredLogger
  14.  
     
  15.  
    func main() {
  16.  
    fmt.Println("begin main")
  17.  
    InitLogger()
  18.  
    defer sugarLogger.Sync()
  19.  
    simpleHttpGet("www.cnblogs.com")
  20.  
    simpleHttpGet("https://www.baidu.com")
  21.  
    }
  22.  
     
  23.  
    //例子,http访问url,返回状态
  24.  
    func simpleHttpGet(url string) {
  25.  
    fmt.Println("begin simpleHttpGet:"+url)
  26.  
    sugarLogger.Debugf("Trying to hit GET request for %s", url)
  27.  
    resp, err := http.Get(url)
  28.  
    if err != nil {
  29.  
    sugarLogger.Errorf("Error fetching URL %s : Error = %s", url, err)
  30.  
    } else {
  31.  
    sugarLogger.Infof("Success! statusCode = %s for URL %s", resp.Status, url)
  32.  
    resp.Body.Close()
  33.  
    }
  34.  
    }
  35.  
     
  36.  
    func InitLogger() {
  37.  
    encoder := getEncoder()
  38.  
     
  39.  
    //两个interface,判断日志等级
  40.  
    //warnlevel以下归到info日志
  41.  
    infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
  42.  
    return lvl < zapcore.WarnLevel
  43.  
    })
  44.  
    //warnlevel及以上归到warn日志
  45.  
    warnLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
  46.  
    return lvl >= zapcore.WarnLevel
  47.  
    })
  48.  
     
  49.  
    infoWriter := getLogWriter("/data/liuhongdi/logs/zaplog2info")
  50.  
    warnWriter := getLogWriter("/data/liuhongdi/logs/zaplog2warn")
  51.  
     
  52.  
    //创建zap.Core,for logger
  53.  
    core := zapcore.NewTee(
  54.  
    zapcore.NewCore(encoder, infoWriter, infoLevel),
  55.  
    zapcore.NewCore(encoder, warnWriter, warnLevel),
  56.  
    )
  57.  
    //生成Logger
  58.  
    logger := zap.New(core, zap.AddCaller())
  59.  
    sugarLogger = logger.Sugar()
  60.  
    }
  61.  
     
  62.  
    //getEncoder
  63.  
    func getEncoder() zapcore.Encoder {
  64.  
    encoderConfig := zap.NewProductionEncoderConfig()
  65.  
    encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
  66.  
    encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
  67.  
    return zapcore.NewConsoleEncoder(encoderConfig)
  68.  
    }
  69.  
     
  70.  
    //得到LogWriter
  71.  
    func getLogWriter(filePath string) zapcore.WriteSyncer {
  72.  
    warnIoWriter := getWriter(filePath)
  73.  
    return zapcore.AddSync(warnIoWriter)
  74.  
    }
  75.  
     
  76.  
     
  77.  
    //日志文件切割
  78.  
    func getWriter(filename string) io.Writer {
  79.  
    // 保存日志30天,每24小时分割一次日志
  80.  
    /*
  81.  
    hook, err := rotatelogs.New(
  82.  
    filename+"_%Y%m%d.log",
  83.  
    rotatelogs.WithLinkName(filename),
  84.  
    rotatelogs.WithMaxAge(time.Hour*24*30),
  85.  
    rotatelogs.WithRotationTime(time.Hour*24),
  86.  
    )
  87.  
    */
  88.  
    //保存日志30天,每1分钟分割一次日志
  89.  
    hook, err := rotatelogs.New(
  90.  
    filename+"_%Y%m%d%H%M.log",
  91.  
    rotatelogs.WithLinkName(filename),
  92.  
    rotatelogs.WithMaxAge(time.Hour*24*30),
  93.  
    rotatelogs.WithRotationTime(time.Minute*1),
  94.  
    )
  95.  
    if err != nil {
  96.  
    panic(err)
  97.  
    }
  98.  
    return hook
  99.  
    }

 

四,测试效果

1,运行代码,控制台输出:

  1.  
    begin main
  2.  
    begin simpleHttpGet:www.cnblogs.com
  3.  
    begin simpleHttpGet:https://www.baidu.com

两个url地址中,第一个缺少协议,所以会生成错误日志,写到warn日志中,
另一个地址正确,会写到info日志中

2,查看日志文件内容:

  1.  
    root@ku:/data/liuhongdi/logs# more zaplog2warn_202011261853.log
  2.  
    2020-11-26T18:53:18.846+0800 ERROR zaplog2/main.go:29 Error fetching URL www.cnblogs.com : Error = Get "www.cnblogs.com": unsupported protocol schem
  3.  
    e ""
  4.  
     
  5.  
    root@ku:/data/liuhongdi/logs# more zaplog2info_202011261853.log
  6.  
    2020-11-26T18:53:18.845+0800 DEBUG zaplog2/main.go:26 Trying to hit GET request for www.cnblogs.com
  7.  
    2020-11-26T18:53:18.846+0800 DEBUG zaplog2/main.go:26 Trying to hit GET request for https://www.baidu.com
  8.  
    2020-11-26T18:53:24.011+0800 INFO zaplog2/main.go:31 Success! statusCode = 200 OK for URL https://www.baidu.com

3,查看目录下的日志文件:  

  1.  
    root@ku:/data/liuhongdi/logs# ls -l
  2.  
    总用量 8
  3.  
    lrwxrwxrwx 1 liuhongdi liuhongdi 49 11月 26 18:53 zaplog2info -> /data/liuhongdi/logs/zaplog2info_202011261853.log
  4.  
    -rw-r--r-- 1 liuhongdi liuhongdi 636 11月 26 18:53 zaplog2info_202011261853.log
  5.  
    lrwxrwxrwx 1 liuhongdi liuhongdi 49 11月 26 18:53 zaplog2warn -> /data/liuhongdi/logs/zaplog2warn_202011261853.log
  6.  
    -rw-r--r-- 1 liuhongdi liuhongdi 306 11月 26 18:53 zaplog2warn_202011261853.log

可以看到go-file-rotatelogs实现日志切分时是使用了linux的符号链接,
日志切分时,把当前写入的符号链接指向另一个日志文件即可

 

五,查看go.mod中的版本信息:

    1.  
      module github.com/liuhongdi/zaplog2
    2.  
       
    3.  
      go 1.15
    4.  
       
    5.  
      require (
    6.  
      github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f // indirect
    7.  
      github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect
    8.  
      github.com/pkg/errors v0.9.1 // indirect
    9.  
      go.uber.org/multierr v1.6.0 // indirect
    10.  
      go.uber.org/zap v1.16.0 // indirect
    11.  
      )

posted on 2021-01-22 10:58  ExplorerMan  阅读(711)  评论(0编辑  收藏  举报

导航