Skip to content

Commit c9b4f5a

Browse files
authored
Merge pull request #913 from sirupsen/example_caller_prettyfier
Example caller prettyfier
2 parents fa3c1df + 99a5172 commit c9b4f5a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

example_custom_caller_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

json_formatter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
9393
fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
9494
if f.CallerPrettyfier != nil {
9595
funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
96-
fmt.Println(funcVal, fileVal)
9796
}
9897
if funcVal != "" {
9998
data[f.FieldMap.resolve(FieldKeyFunc)] = funcVal

0 commit comments

Comments
 (0)