Skip to content

Commit

Permalink
Merge pull request #913 from sirupsen/example_caller_prettyfier
Browse files Browse the repository at this point in the history
Example caller prettyfier
  • Loading branch information
dgsb committed Mar 3, 2019
2 parents fa3c1df + 99a5172 commit c9b4f5a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
28 changes: 28 additions & 0 deletions example_custom_caller_test.go
Original file line number Diff line number Diff line change
@@ -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"}
}
1 change: 0 additions & 1 deletion json_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
if f.CallerPrettyfier != nil {
funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
fmt.Println(funcVal, fileVal)
}
if funcVal != "" {
data[f.FieldMap.resolve(FieldKeyFunc)] = funcVal
Expand Down

0 comments on commit c9b4f5a

Please sign in to comment.