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
}
例1
func 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, 否則時間顯示不生效。
例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時,日志的樣子有所改變。
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數據均沒有輸出。