は、あなたが要求したものを手に入れる方法は次のとおりです。
package main
import (
log "github.com/sirupsen/logrus"
"fmt"
)
type PlainFormatter struct {
TimestampFormat string
LevelDesc []string
}
func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) {
timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat))
return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[entry.Level], timestamp, entry.Message)), nil
}
func main() {
plainFormatter := new(PlainFormatter)
plainFormatter.TimestampFormat = "2006-01-02 15:04:05"
plainFormatter.LevelDesc = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG"}
log.SetFormatter(plainFormatter)
log.Errorln("Not fatal. An error. Won't stop execution")
log.Infoln("Just some info")
}
- は、(タイムスタンプ書式とレベルのフィールドを持つ)PlainFormatter構造体
を作成します。
- フォーマット関数/メソッドを作成する(構造体がレシーバ)
- セットlogrusカスタムフォーマッタを使用する
出力:私の本で
ERRO 2017-07-01 18:22:21 Not fatal. An error. Won't stop execution
INFO 2017-07-01 18:22:21 Just some info
あなたが装飾と機能の追加でアプリケーションロガーを操作する方法のより実用的な例を見ることができますLearn Functional Programming in Goブック章。
出典
2017-07-01 22:24:06
l3x