• Go整合Logrus实现日志打印


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

    1 初步使用

    package main
    
    import (
       "context"
       "github.com/sirupsen/logrus"
    )
    
    func main() {
       method0()
    }
    
    func method0() {
       logger:= logrus.New()
       logger.Warning("This is a first log.")
       ctx := context.WithValue(context.Background(),"key","value")
       logger.Warning(ctx,"This is a second log.")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2 增加标签WithFields

    package main
    
    import (
       "context"
       "github.com/sirupsen/logrus"
    )
    
    func main() {
       method1()
    }
    
    func method1() {
       log.WithFields(log.Fields{
          "fieldKey": "fieldValue",
       }).Warning("This is a first field log.")
    
       log.WithFields(log.Fields{
          "fieldKey": "fieldValue",
          "fieldKey2": "fieldValue2",
       }).Warning("This is a second field log.")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3 配置常见参数

    package main
    
    import (
       "context"
       "github.com/sirupsen/logrus"
       log "github.com/sirupsen/logrus"
       "os"
    )
    
    func main() {
       method2()
    }
    
    func init() {
       // 日志作为JSON而不是默认的ASCII格式器.
       log.SetFormatter(&log.JSONFormatter{})
    
       // 输出到标准输出,可以是任何io.Writer
       log.SetOutput(os.Stdout)
    
       // 只记录xx级别或以上的日志
       log.SetLevel(log.TraceLevel)
    }
    
    func method2() {
       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!")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    Formatter一般分为两种:

    • &log.JSONFormatter{}
    • &log.TextFormatter{}

    日志级别一共七种:

    • log.Trace()
    • log.Debug()
    • log.Info()
    • log.Warn()
    • log.Error()
    • log.Fatal()
    • log.Panic()

    4 输出日志到文件

    package main
    
    import (
    	"context"
    	"github.com/sirupsen/logrus"
    	"os"
    )
    
    func main() {
    	method4()
    }
    
    func method4() {
       var log = logrus.New()
       file ,err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
       if err == nil{
          log.Out = file
       }else{
          log.Info("Failed to log to file")
       }
    
       log.WithFields(logrus.Fields{
          "filename": "123.txt",
       }).Info("This is a file log")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    logrus.log文件的内容:

    time="2022-01-06T13:04:25+08:00" level=info msg="This is a file log" filename=123.txt\
    
    • 1

    5 利用Hooks将日志输出到其他地方

    import (
      log "github.com/sirupsen/logrus"
      "gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "airbrake"
      logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
      "log/syslog"
    )
    
    func init() {
    
      // 使用气闸挂钩来报告错误严重程度或以上的错误一个异常追踪。您可以创建自定义钩子,请参见钩子部分。
      log.AddHook(airbrake.NewHook(123, "xyz", "production"))
    
      hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
      if err != nil {
        log.Error("Unable to connect to local syslog daemon")
      } else {
        log.AddHook(hook)
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    只需要在AddHook是添加相应的Hook就可以了

  • 相关阅读:
    【云原生专题】基于Docker+Neo4j图数据库搭建企业级分布式应用拓扑图
    分享一下公众号抽奖活动怎么做
    2024年04月09日 Go生态洞察:2024年上半年Go开发者调查报告洞察
    前端工程化面试题及答案【集合】
    Java之多线程的综合练习二
    全网首讲最详细AMR系统介绍(1):Abstract Meaning Representation(AMR) 的基础表示逻辑;附英文版原 pdf 资料
    ubuntu服务器上java和tomcat等服务的日志时间不正确
    【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
    Springboot响应式代购商城APP毕业设计-附源码191654
    “2024杭州国际物联网展览会”定于4月份在杭州国际博览中心召开
  • 原文地址:https://blog.csdn.net/Mr_YanMingXin/article/details/125551486