go 日志库logrus

1. logrus

  1. 日志库

  2. 包路径:"github.com/sirupsen/logrus"

  3. 快速入门例子:

    	package main
    
    import (
      "os"
      log "github.com/sirupsen/logrus"
    )
    
    func init() {
      // Log as JSON instead of the default ASCII formatter.
      log.SetFormatter(&log.JSONFormatter{})
    
      // Output to stdout instead of the default stderr
      // Can be any io.Writer, see below for File example
      log.SetOutput(os.Stdout)
    
      // Only log the warning severity or above.
      log.SetLevel(log.WarnLevel)
    }
    
    func main() {
      log.WithFields(log.Fields{
        "animal": "walrus",
        "size":   10,
      }).Info("A group of walrus emerges from the ocean")
    
      log.WithFields(log.Fields{
        "omg":    true,
        "number": 122,
      }).Warn("The group's number increased tremendously!")
    
      log.WithFields(log.Fields{
        "omg":    true,
        "number": 100,
      }).Fatal("The ice breaks!")
    
      // A common pattern is to re-use fields between logging statements by re-using
      // the logrus.Entry returned from WithFields()
      contextLogger := log.WithFields(log.Fields{
        "common": "this is a common field",
        "other": "I also should be logged always",
      })
    
      contextLogger.Info("I'll be logged with common and other field")
      contextLogger.Info("Me too")
    }

2. 使用

1. 设置输出格式

  1. json格式

  1. 文本格式

2. 更多见文档

  1. https://github.com/sirupsen/logrus

最后更新于

这有帮助吗?