From 99a5172d62d45e607945ff99722c24a7db95e7a0 Mon Sep 17 00:00:00 2001 From: David Bariod Date: Sun, 3 Mar 2019 11:53:51 +0100 Subject: [PATCH] Add and example for CallerPrettyfier --- example_custom_caller_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 example_custom_caller_test.go diff --git a/example_custom_caller_test.go b/example_custom_caller_test.go new file mode 100644 index 0000000..e0023b9 --- /dev/null +++ b/example_custom_caller_test.go @@ -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"} +}