mirror of https://github.com/sirupsen/logrus.git
Add a CallerPrettyfier callback to the json formatter
This commit is contained in:
parent
4f5fd631f1
commit
5e9b246bea
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fieldKey string
|
type fieldKey string
|
||||||
|
@ -42,6 +43,12 @@ type JSONFormatter struct {
|
||||||
// }
|
// }
|
||||||
FieldMap FieldMap
|
FieldMap FieldMap
|
||||||
|
|
||||||
|
// CallerPrettyfier can be set by the user to modify the content
|
||||||
|
// of the function and file keys in the json data when ReportCaller is
|
||||||
|
// activated. If any of the returned value is the empty string the
|
||||||
|
// corresponding key will be removed from json fields.
|
||||||
|
CallerPrettyfier func(*runtime.Frame) (function string, file string)
|
||||||
|
|
||||||
// PrettyPrint will indent all json logs
|
// PrettyPrint will indent all json logs
|
||||||
PrettyPrint bool
|
PrettyPrint bool
|
||||||
}
|
}
|
||||||
|
@ -82,8 +89,18 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
|
data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
|
||||||
data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
|
data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
|
||||||
if entry.HasCaller() {
|
if entry.HasCaller() {
|
||||||
data[f.FieldMap.resolve(FieldKeyFunc)] = entry.Caller.Function
|
funcVal := entry.Caller.Function
|
||||||
data[f.FieldMap.resolve(FieldKeyFile)] = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
|
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
|
||||||
|
}
|
||||||
|
if fileVal != "" {
|
||||||
|
data[f.FieldMap.resolve(FieldKeyFile)] = fileVal
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var b *bytes.Buffer
|
var b *bytes.Buffer
|
||||||
|
|
|
@ -40,7 +40,18 @@ func TestReportCallerWhenConfigured(t *testing.T) {
|
||||||
assert.Equal(t, "testWithCaller", fields["msg"])
|
assert.Equal(t, "testWithCaller", fields["msg"])
|
||||||
assert.Equal(t, "info", fields["level"])
|
assert.Equal(t, "info", fields["level"])
|
||||||
assert.Equal(t,
|
assert.Equal(t,
|
||||||
"github.com/sirupsen/logrus_test.TestReportCallerWhenConfigured.func3", fields["func"])
|
"github.com/sirupsen/logrus_test.TestReportCallerWhenConfigured.func3", fields[FieldKeyFunc])
|
||||||
|
})
|
||||||
|
|
||||||
|
LogAndAssertJSON(t, func(log *Logger) {
|
||||||
|
log.ReportCaller = true
|
||||||
|
log.Formatter.(*JSONFormatter).CallerPrettyfier = func(f *runtime.Frame) (string, string) {
|
||||||
|
return "somekindoffunc", "thisisafilename"
|
||||||
|
}
|
||||||
|
log.Print("testWithCallerPrettyfier")
|
||||||
|
}, func(fields Fields) {
|
||||||
|
assert.Equal(t, "somekindoffunc", fields[FieldKeyFunc])
|
||||||
|
assert.Equal(t, "thisisafilename", fields[FieldKeyFile])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue