1. 設(shè)置日志格式的方法
logrus中,使用如下方法設(shè)置日志格式
func SetFormatter(formatter Formatter)
其中Formatter是一個(gè)接口
type Formatter interface {
Format(*Entry) ([]byte, error)
}
所以,實(shí)現(xiàn)自定義日志格式,本質(zhì)上就是實(shí)現(xiàn)Formatter接口,然后通過SetFormatter方式將其告知logrus。
2. 已有Formatter
logrus包中自帶兩種Formatter,分別是TextFormatter和JSONFormatter。 默認(rèn)情況下,使用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環(huán)境的輸出。
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為準(zhǔn)。
2.1 TextFormatter
TextFormatter有若干可定制參數(shù),常用參數(shù)如下,更多參數(shù)及功能,可在源碼的text_formatter.go中看到。
type TextFormatter struct{
//顏色顯示相關(guān)
ForceColors bool
EnvironmentOverrideColors bool
DisableColors bool
//日志中的鍵值對(duì)加引號(hào)相關(guān)
ForceQuote bool
DisableQuote bool
QuoteEmptyFields bool
//時(shí)間戳相關(guān)
DisableTimestamp bool
FullTimestamp bool
TimestampFormat string
}
例1
func main(){
logrus.SetFormatter(&logrus.TextFormatter{
ForceQuote:true, //鍵值對(duì)加引號(hào)
TimestampFormat:"2006-01-02 15:04:05", //時(shí)間格式
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"
說明:
默認(rèn)是Colors模式,該模式下,必須設(shè)置FullTimestamp:true, 否則時(shí)間顯示不生效。
例2
func 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時(shí),日志的樣子有所改變。
2.2 JSONFormatter
常用參數(shù)如下,更多參數(shù)及功能,可在源碼的json_formatter.go中看到。
type JSONFormatter struct {
//時(shí)間戳相關(guān)
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"
}
說明:
若不設(shè)置PrettyPrint: true, 則json為單行輸出。
3. 自定義Formatter
自定義Formatter,其實(shí)就是實(shí)現(xiàn)Formatter接口。
type Formatter interface {
Format(*Entry) ([]byte, error)
}
接口的返回值[]byte,即為輸出串。關(guān)鍵在于搞懂輸入?yún)?shù)Entry。
3.1 Entry參數(shù)
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形式的數(shù)據(jù),是使用WithField設(shè)置的日志。
- Caller是日志調(diào)用者相關(guān)的信息,可以利用其輸出文件名,行號(hào)等信息,感興趣可以參看《logrus中輸出文件名、行號(hào)及函數(shù)名》
例
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的數(shù)據(jù),因此使用WithField設(shè)置的name,say數(shù)據(jù)均沒有輸出。