Last active
February 17, 2020 02:42
-
-
Save paulyung541/90de00b2f233b7d875af553702c79d11 to your computer and use it in GitHub Desktop.
logrus日志框架一些用法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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