logrus自定义日志输出格式 您所在的位置:网站首页 data_list_intergrated_alljson logrus自定义日志输出格式

logrus自定义日志输出格式

#logrus自定义日志输出格式| 来源: 网络整理| 查看: 265

1. 设置日志格式的方法

logrus中,使用如下方法设置日志格式

func SetFormatter(formatter Formatter)

其中Formatter是一个接口

type Formatter interface { Format(*Entry) ([]byte, error) }

所以,实现自定义日志格式,本质上就是实现Formatter接口,然后通过SetFormatter方式将其告知logrus。

2. 已有Formatter

logrus包中自带两种Formatter,分别是TextFormatter和JSONFormatter。 默认情况下,使用TextFormatter输出。

func main(){ logrus.WithField("name", "ball").Info("this is from logrus") } //输出 INFO[0000] this is from logrus name=ball

说明:

以上是go version go1.14.4 linux/amd64环境的输出。 go version go1.14.4 windows/amd64中的输出较为友好:time="2021-05-10T15:54:33+08:00" level=info msg="this is from logrus" name=ball下文输出均以go version go1.14.4 linux/amd64为准。2.1 TextFormatter

TextFormatter有若干可定制参数,常用参数如下,更多参数及功能,可在源码的text_formatter.go中看到。

type TextFormatter struct{ //颜色显示相关 ForceColors bool EnvironmentOverrideColors bool DisableColors bool //日志中的键值对加引号相关 ForceQuote bool DisableQuote bool QuoteEmptyFields bool //时间戳相关 DisableTimestamp bool FullTimestamp bool TimestampFormat string }例1func main(){ logrus.SetFormatter(&logrus.TextFormatter{ ForceQuote:true, //键值对加引号 TimestampFormat:"2006-01-02 15:04:05", //时间格式 FullTimestamp:true, }) logrus.WithField("name", "ball").WithField("say", "hi").Info("info log") } // 输出 INFO[2021-05-10 16:28:50] info log name="ball" say="hi"

说明: 默认是Colors模式,该模式下,必须设置FullTimestamp:true, 否则时间显示不生效。

例2func main(){ logrus.SetFormatter(&logrus.TextFormatter{ DisableColors:true, ForceQuote:false, TimestampFormat:"2006-01-02 15:04:05", }) logrus.WithField("name", "ball").WithField("say", "hi").Info("info log") } //输出 time="2021-05-10 16:32:42" level=info msg="info log" name=ball say=hi

说明: DisableColors为true时,日志的样子有所改变。

2.2 JSONFormatter

常用参数如下,更多参数及功能,可在源码的json_formatter.go中看到。

type JSONFormatter struct { //时间戳相关 TimestampFormat string DisableTimestamp bool DisableHTMLEscape bool // PrettyPrint will indent all json logs PrettyPrint bool }例func main(){ logrus.SetFormatter(&logrus.JSONFormatter{ TimestampFormat:"2006-01-02 15:04:05", PrettyPrint: true, }) logrus.WithField("name", "ball").WithField("say", "hi").Info("info log") } //输出 { "level": "info", "msg": "info log", "name": "ball", "say": "hi", "time": "2021-05-10 16:36:05" }

说明: 若不设置PrettyPrint: true, 则json为单行输出。

3. 自定义Formatter

自定义Formatter,其实就是实现Formatter接口。

type Formatter interface { Format(*Entry) ([]byte, error) }

接口的返回值[]byte,即为输出串。关键在于搞懂输入参数Entry。

3.1 Entry参数type Entry struct { // Contains all the fields set by the user. Data Fields // Time at which the log entry was created Time time.Time // Level the log entry was logged at: Trace, Debug, Info, Warn, Error, Fatal or Panic Level Level //Calling method, with package name Caller *runtime.Frame //Message passed to Trace, Debug, Info, Warn, Error, Fatal or Panic Message string //When formatter is called in entry.log(), a Buffer may be set to entry Buffer *bytes.Buffer }

说明:

Data中是key/value形式的数据,是使用WithField设置的日志。Caller是日志调用者相关的信息,可以利用其输出文件名,行号等信息,感兴趣可以参看《logrus中输出文件名、行号及函数名》例type MyFormatter struct { } func (m *MyFormatter) Format(entry *logrus.Entry) ([]byte, error){ var b *bytes.Buffer if entry.Buffer != nil { b = entry.Buffer } else { b = &bytes.Buffer{} } timestamp := entry.Time.Format("2006-01-02 15:04:05") var newLog string newLog = fmt.Sprintf("[%s] [%s] %s\n", timestamp, entry.Level, entry.Message) b.WriteString(newLog) return b.Bytes(), nil } func main(){ logrus.SetFormatter(&MyFormatter{}) logrus.WithField("name", "ball").WithField("say", "hi").Info("info log") } //输出 [2021-05-10 17:26:06] [info] info log

说明:例子中没有处理entry.Data的数据,因此使用WithField设置的name,say数据均没有输出。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有