Skip to content

Instantly share code, notes, and snippets.

@paulyung541
Last active February 17, 2020 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulyung541/90de00b2f233b7d875af553702c79d11 to your computer and use it in GitHub Desktop.
Save paulyung541/90de00b2f233b7d875af553702c79d11 to your computer and use it in GitHub Desktop.
logrus日志框架一些用法
func test1() {
var log = logrus.New()
log.SetReportCaller(true)
log.Level = logrus.TraceLevel
log.Out = os.Stdout
log.Formatter = &logrus.JSONFormatter{
PrettyPrint: true,
FieldMap: logrus.FieldMap{
logrus.FieldKeyLevel: "my_level",
},
DisableTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
s := strings.Split(f.Function, ".")
funcname := s[len(s)-1]
dir, filename := path.Split(f.File)
tmpArray := strings.Split(dir, string(os.PathSeparator))
if len(tmpArray) <= 3 {
filename = f.File // 比较短就直接显示
} else {
tmpArray = tmpArray[len(tmpArray)-3:]
filename = strings.Join(tmpArray, string(os.PathSeparator)) + filename
}
return funcname, filename
},
}
log.WithFields(logrus.Fields{
"key1": "value1",
"size": 10,
}).Info("this is simple")
log.Info("the big is one")
log.Error("this is the error")
log.WithTime(time.Now()).Traceln("error")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment