mirror of https://github.com/sirupsen/logrus.git
Add in preprocessing hook to add additional key/value pairs
This commit is contained in:
parent
b50299cfaa
commit
8b395d0834
|
@ -49,6 +49,8 @@ type JSONFormatter struct {
|
|||
// }
|
||||
FieldMap FieldMap
|
||||
|
||||
PreprocessorHook func(Fields)
|
||||
|
||||
// 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
|
||||
|
@ -108,6 +110,10 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
|||
}
|
||||
}
|
||||
|
||||
if f.PreprocessorHook != nil {
|
||||
f.PreprocessorHook(data)
|
||||
}
|
||||
|
||||
var b *bytes.Buffer
|
||||
if entry.Buffer != nil {
|
||||
b = entry.Buffer
|
||||
|
|
|
@ -370,3 +370,21 @@ func TestJSONEnableHTMLEscape(t *testing.T) {
|
|||
t.Error("Message should be HTML escaped", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreprocessorHook(t *testing.T) {
|
||||
formatter := &JSONFormatter{}
|
||||
formatter.PreprocessorHook = func(f Fields) {
|
||||
f["testme"] = "hello"
|
||||
}
|
||||
b, err := formatter.Format(&Entry{Message: "My Message"})
|
||||
if err != nil {
|
||||
t.Fatal("Unable to format entry: ", err)
|
||||
}
|
||||
s := string(b)
|
||||
if !strings.Contains(s, `"testme"`) {
|
||||
t.Error("Message should contain key added by preprocessor hook")
|
||||
}
|
||||
if !strings.Contains(s, `"hello"`) {
|
||||
t.Error("Message should contain value added by preprocessor hook")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue