Browse Source

Add and example for CallerPrettyfier

tags/v1.4.0
David Bariod 7 months ago
parent
commit
99a5172d62
1 changed files with 28 additions and 0 deletions
  1. 28
    0
      example_custom_caller_test.go

+ 28
- 0
example_custom_caller_test.go View File

@@ -0,0 +1,28 @@
1
+package logrus_test
2
+
3
+import (
4
+	"os"
5
+	"path"
6
+	"runtime"
7
+	"strings"
8
+
9
+	"github.com/sirupsen/logrus"
10
+)
11
+
12
+func ExampleCustomFormatter() {
13
+	l := logrus.New()
14
+	l.SetReportCaller(true)
15
+	l.Out = os.Stdout
16
+	l.Formatter = &logrus.JSONFormatter{
17
+		DisableTimestamp: true,
18
+		CallerPrettyfier: func(f *runtime.Frame) (string, string) {
19
+			s := strings.Split(f.Function, ".")
20
+			funcname := s[len(s)-1]
21
+			_, filename := path.Split(f.File)
22
+			return funcname, filename
23
+		},
24
+	}
25
+	l.Info("example of custom format caller")
26
+	// Output:
27
+	// {"file":"example_custom_caller_test.go","func":"ExampleCustomFormatter","level":"info","msg":"example of custom format caller"}
28
+}

Loading…
Cancel
Save