Merge pull request #913 from sirupsen/example_caller_prettyfier

Example caller prettyfier
This commit is contained in:
David Bariod 2019-03-03 12:00:21 +01:00 committed by GitHub
commit c9b4f5af6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -0,0 +1,28 @@
package logrus_test
import (
"os"
"path"
"runtime"
"strings"
"github.com/sirupsen/logrus"
)
func ExampleCustomFormatter() {
l := logrus.New()
l.SetReportCaller(true)
l.Out = os.Stdout
l.Formatter = &logrus.JSONFormatter{
DisableTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
s := strings.Split(f.Function, ".")
funcname := s[len(s)-1]
_, filename := path.Split(f.File)
return funcname, filename
},
}
l.Info("example of custom format caller")
// Output:
// {"file":"example_custom_caller_test.go","func":"ExampleCustomFormatter","level":"info","msg":"example of custom format caller"}
}

View File

@ -93,7 +93,6 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line) fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
if f.CallerPrettyfier != nil { if f.CallerPrettyfier != nil {
funcVal, fileVal = f.CallerPrettyfier(entry.Caller) funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
fmt.Println(funcVal, fileVal)
} }
if funcVal != "" { if funcVal != "" {
data[f.FieldMap.resolve(FieldKeyFunc)] = funcVal data[f.FieldMap.resolve(FieldKeyFunc)] = funcVal