1 2
| INFO[2021-07-04 15:26:26]main.go:28 have a nice day zs=log INFO[2021-07-04 15:26:26]main.go:29 zs gogogo zs=log
|
code sample
show timestamp
the meaning of [0000]
add common prefix
have a little overhead, add filename and line number
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
| package main
import ( "path" "runtime" "strconv"
"github.com/sirupsen/logrus" )
func main() { var log = logrus.New()
formatter := &logrus.TextFormatter{ FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05", CallerPrettyfier: func(f *runtime.Frame) (string, string) { _, filename := path.Split(f.File) return "", filename + ":" + strconv.Itoa(f.Line) }, } log.SetFormatter(formatter) log.SetReportCaller(true)
contextLogger := log.WithField("zs", "log")
contextLogger.Info("have a nice day") contextLogger.Infof("%s gogogo", "zs") }
|
https://github.com/sirupsen/logrus#formatters
1 2
| [2021-07-04 15:50:26] INFO log: have a nice day [2021-07-04 15:50:26] INFO log: zs gogogo
|
code sample
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package main
import ( "github.com/sirupsen/logrus" prefixed "github.com/x-cray/logrus-prefixed-formatter" )
func main() { var log = logrus.New()
formatter := &prefixed.TextFormatter{ FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05", } log.Formatter = formatter
contextLogger := log.WithField("prefix", "log")
contextLogger.Info("have a nice day") contextLogger.Infof("%s gogogo", "zs") }
|
as previous code show
1
| contextLogger := log.WithField("prefix", "log")
|
u can prefix a log key and colon before the msg output