mirror of https://github.com/sirupsen/logrus.git
Merge pull request #913 from sirupsen/example_caller_prettyfier
Example caller prettyfier
This commit is contained in:
commit
c9b4f5af6d
|
@ -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"}
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue