2014-03-11 03:22:08 +04:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
2018-05-15 20:07:01 +03:00
|
|
|
"bytes"
|
2014-03-11 03:22:08 +04:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
2016-11-18 22:02:11 +03:00
|
|
|
|
2016-11-17 22:28:41 +03:00
|
|
|
type fieldKey string
|
2017-07-26 15:26:30 +03:00
|
|
|
|
|
|
|
// FieldMap allows customization of the key names for default fields.
|
2016-11-18 02:16:46 +03:00
|
|
|
type FieldMap map[fieldKey]string
|
2016-11-17 22:28:41 +03:00
|
|
|
|
2016-11-18 02:16:46 +03:00
|
|
|
func (f FieldMap) resolve(key fieldKey) string {
|
|
|
|
if k, ok := f[key]; ok {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(key)
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:26:30 +03:00
|
|
|
// JSONFormatter formats logs into parsable json
|
2015-04-04 03:25:25 +03:00
|
|
|
type JSONFormatter struct {
|
|
|
|
// TimestampFormat sets the format used for marshaling timestamps.
|
|
|
|
TimestampFormat string
|
2016-11-18 22:02:11 +03:00
|
|
|
|
2016-11-21 21:09:59 +03:00
|
|
|
// DisableTimestamp allows disabling automatic timestamps in output
|
|
|
|
DisableTimestamp bool
|
|
|
|
|
2018-06-19 15:31:57 +03:00
|
|
|
// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
|
|
|
|
DataKey string
|
|
|
|
|
2017-07-26 15:26:30 +03:00
|
|
|
// FieldMap allows users to customize the names of keys for default fields.
|
2016-11-18 22:02:11 +03:00
|
|
|
// As an example:
|
|
|
|
// formatter := &JSONFormatter{
|
|
|
|
// FieldMap: FieldMap{
|
2016-12-06 23:53:21 +03:00
|
|
|
// FieldKeyTime: "@timestamp",
|
|
|
|
// FieldKeyLevel: "@level",
|
|
|
|
// FieldKeyMsg: "@message",
|
|
|
|
// FieldKeyFunc: "@caller",
|
2016-11-18 22:02:11 +03:00
|
|
|
// },
|
|
|
|
// }
|
|
|
|
FieldMap FieldMap
|
2017-11-07 03:19:47 +03:00
|
|
|
|
|
|
|
// PrettyPrint will indent all json logs
|
|
|
|
PrettyPrint bool
|
2015-04-04 03:25:25 +03:00
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
|
2017-07-26 15:26:30 +03:00
|
|
|
// Format renders a single log entry
|
2014-03-11 03:22:08 +04:00
|
|
|
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
2016-11-26 06:02:56 +03:00
|
|
|
data := make(Fields, len(entry.Data)+4)
|
2014-12-10 05:53:40 +03:00
|
|
|
for k, v := range entry.Data {
|
2015-03-10 19:04:57 +03:00
|
|
|
switch v := v.(type) {
|
|
|
|
case error:
|
|
|
|
// Otherwise errors are ignored by `encoding/json`
|
2017-05-12 22:21:58 +03:00
|
|
|
// https://github.com/sirupsen/logrus/issues/137
|
2015-03-10 19:04:57 +03:00
|
|
|
data[k] = v.Error()
|
|
|
|
default:
|
2015-03-09 18:15:08 +03:00
|
|
|
data[k] = v
|
|
|
|
}
|
2014-12-10 05:53:40 +03:00
|
|
|
}
|
2016-12-01 01:07:10 +03:00
|
|
|
|
2018-06-19 15:31:57 +03:00
|
|
|
if f.DataKey != "" {
|
|
|
|
newData := make(Fields, 4)
|
|
|
|
newData[f.DataKey] = data
|
|
|
|
data = newData
|
|
|
|
}
|
|
|
|
|
2018-10-23 07:22:00 +03:00
|
|
|
prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
|
2015-04-04 03:25:25 +03:00
|
|
|
|
2015-05-26 16:02:13 +03:00
|
|
|
timestampFormat := f.TimestampFormat
|
|
|
|
if timestampFormat == "" {
|
2017-07-26 15:26:30 +03:00
|
|
|
timestampFormat = defaultTimestampFormat
|
2015-04-04 03:25:25 +03:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:51:02 +03:00
|
|
|
if entry.err != "" {
|
|
|
|
data[f.FieldMap.resolve(FieldKeyLogrusError)] = entry.err
|
|
|
|
}
|
2016-11-21 21:09:59 +03:00
|
|
|
if !f.DisableTimestamp {
|
|
|
|
data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat)
|
|
|
|
}
|
2016-11-18 02:16:46 +03:00
|
|
|
data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
|
|
|
|
data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
|
2016-12-01 02:15:38 +03:00
|
|
|
if entry.HasCaller() {
|
2016-12-06 23:53:21 +03:00
|
|
|
data[f.FieldMap.resolve(FieldKeyFunc)] = entry.Caller
|
2016-11-26 06:02:56 +03:00
|
|
|
}
|
2014-07-27 05:26:04 +04:00
|
|
|
|
2018-05-15 20:07:01 +03:00
|
|
|
var b *bytes.Buffer
|
|
|
|
if entry.Buffer != nil {
|
|
|
|
b = entry.Buffer
|
|
|
|
} else {
|
|
|
|
b = &bytes.Buffer{}
|
|
|
|
}
|
2017-11-07 03:19:47 +03:00
|
|
|
|
2018-09-08 12:18:16 +03:00
|
|
|
encoder := json.NewEncoder(b)
|
|
|
|
if f.PrettyPrint {
|
|
|
|
encoder.SetIndent("", " ")
|
|
|
|
}
|
|
|
|
if err := encoder.Encode(data); err != nil {
|
2014-03-11 03:22:08 +04:00
|
|
|
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
|
|
|
|
}
|
2018-09-08 12:18:16 +03:00
|
|
|
|
2018-05-15 20:07:01 +03:00
|
|
|
return b.Bytes(), nil
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|