forked from mirror/logrus
Support for Entry data under nested JSON dictionary.
This commit is contained in:
parent
75068beb13
commit
2ce6c0cb44
|
@ -33,6 +33,9 @@ type JSONFormatter struct {
|
|||
// DisableTimestamp allows disabling automatic timestamps in output
|
||||
DisableTimestamp bool
|
||||
|
||||
// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
|
||||
DataKey string
|
||||
|
||||
// FieldMap allows users to customize the names of keys for default fields.
|
||||
// As an example:
|
||||
// formatter := &JSONFormatter{
|
||||
|
@ -58,6 +61,13 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
|||
data[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
if f.DataKey != "" {
|
||||
newData := make(Fields, 4)
|
||||
newData[f.DataKey] = data
|
||||
data = newData
|
||||
}
|
||||
|
||||
prefixFieldClashes(data, f.FieldMap)
|
||||
|
||||
timestampFormat := f.TimestampFormat
|
||||
|
|
|
@ -161,6 +161,48 @@ func TestFieldClashWithRemappedFields(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFieldsInNestedDictionary(t *testing.T) {
|
||||
formatter := &JSONFormatter{
|
||||
DataKey: "args",
|
||||
}
|
||||
|
||||
logEntry := WithFields(Fields{
|
||||
"level": "level",
|
||||
"test": "test",
|
||||
})
|
||||
logEntry.Level = InfoLevel
|
||||
|
||||
b, err := formatter.Format(logEntry)
|
||||
if err != nil {
|
||||
t.Fatal("Unable to format entry: ", err)
|
||||
}
|
||||
|
||||
entry := make(map[string]interface{})
|
||||
err = json.Unmarshal(b, &entry)
|
||||
if err != nil {
|
||||
t.Fatal("Unable to unmarshal formatted entry: ", err)
|
||||
}
|
||||
|
||||
args := entry["args"].(map[string]interface{})
|
||||
|
||||
for _, field := range []string{"test", "level"} {
|
||||
if value, present := args[field]; !present || value != field {
|
||||
t.Errorf("Expected field %v to be present under 'args'; untouched", field)
|
||||
}
|
||||
}
|
||||
|
||||
for _, field := range []string{"test", "fields.level"} {
|
||||
if _, present := entry[field]; present {
|
||||
t.Errorf("Expected field %v not to be present at top level", field)
|
||||
}
|
||||
}
|
||||
|
||||
// with nested object, "level" shouldn't clash
|
||||
if entry["level"] != "info" {
|
||||
t.Errorf("Expected 'level' field to contain 'info'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONEntryEndsWithNewline(t *testing.T) {
|
||||
formatter := &JSONFormatter{}
|
||||
|
||||
|
|
Loading…
Reference in New Issue