Add and example for CallerPrettyfier

This commit is contained in:
David Bariod 2019-03-03 11:53:51 +01:00
parent 5c2b39a4f8
commit 99a5172d62
1 changed files with 28 additions and 0 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"}
}