Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 1 | package logrus |
| 2 | |
| 3 | import ( |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 4 | "bytes" |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | ) |
| 8 | |
| 9 | type fieldKey string |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 10 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 11 | // FieldMap allows customization of the key names for default fields. |
| 12 | type FieldMap map[fieldKey]string |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 13 | |
| 14 | func (f FieldMap) resolve(key fieldKey) string { |
| 15 | if k, ok := f[key]; ok { |
| 16 | return k |
| 17 | } |
| 18 | |
| 19 | return string(key) |
| 20 | } |
| 21 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 22 | // JSONFormatter formats logs into parsable json |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 23 | type JSONFormatter struct { |
| 24 | // TimestampFormat sets the format used for marshaling timestamps. |
| 25 | TimestampFormat string |
| 26 | |
| 27 | // DisableTimestamp allows disabling automatic timestamps in output |
| 28 | DisableTimestamp bool |
| 29 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 30 | // DataKey allows users to put all the log entry parameters into a nested dictionary at a given key. |
| 31 | DataKey string |
| 32 | |
| 33 | // FieldMap allows users to customize the names of keys for default fields. |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 34 | // As an example: |
| 35 | // formatter := &JSONFormatter{ |
| 36 | // FieldMap: FieldMap{ |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 37 | // FieldKeyTime: "@timestamp", |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 38 | // FieldKeyLevel: "@level", |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 39 | // FieldKeyMsg: "@message", |
| 40 | // FieldKeyFunc: "@caller", |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 41 | // }, |
| 42 | // } |
| 43 | FieldMap FieldMap |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 44 | |
| 45 | // PrettyPrint will indent all json logs |
| 46 | PrettyPrint bool |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 49 | // Format renders a single log entry |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 50 | func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 51 | data := make(Fields, len(entry.Data)+4) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 52 | for k, v := range entry.Data { |
| 53 | switch v := v.(type) { |
| 54 | case error: |
| 55 | // Otherwise errors are ignored by `encoding/json` |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 56 | // https://github.com/sirupsen/logrus/issues/137 |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 57 | data[k] = v.Error() |
| 58 | default: |
| 59 | data[k] = v |
| 60 | } |
| 61 | } |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 62 | |
| 63 | if f.DataKey != "" { |
| 64 | newData := make(Fields, 4) |
| 65 | newData[f.DataKey] = data |
| 66 | data = newData |
| 67 | } |
| 68 | |
| 69 | prefixFieldClashes(data, f.FieldMap, entry.HasCaller()) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 70 | |
| 71 | timestampFormat := f.TimestampFormat |
| 72 | if timestampFormat == "" { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 73 | timestampFormat = defaultTimestampFormat |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 76 | if entry.err != "" { |
| 77 | data[f.FieldMap.resolve(FieldKeyLogrusError)] = entry.err |
| 78 | } |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 79 | if !f.DisableTimestamp { |
| 80 | data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat) |
| 81 | } |
| 82 | data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message |
| 83 | data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String() |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 84 | if entry.HasCaller() { |
| 85 | data[f.FieldMap.resolve(FieldKeyFunc)] = entry.Caller.Function |
| 86 | data[f.FieldMap.resolve(FieldKeyFile)] = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line) |
| 87 | } |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 88 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 89 | var b *bytes.Buffer |
| 90 | if entry.Buffer != nil { |
| 91 | b = entry.Buffer |
| 92 | } else { |
| 93 | b = &bytes.Buffer{} |
| 94 | } |
| 95 | |
| 96 | encoder := json.NewEncoder(b) |
| 97 | if f.PrettyPrint { |
| 98 | encoder.SetIndent("", " ") |
| 99 | } |
| 100 | if err := encoder.Encode(data); err != nil { |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 101 | return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err) |
| 102 | } |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame] | 103 | |
| 104 | return b.Bytes(), nil |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 105 | } |