logrus/json_formatter.go

126 lines
3.1 KiB
Go
Raw Normal View History

2014-03-11 03:22:08 +04:00
package logrus
import (
"bytes"
2014-03-11 03:22:08 +04:00
"encoding/json"
"fmt"
"runtime"
2014-03-11 03:22:08 +04:00
)
type fieldKey string
2017-07-26 15:26:30 +03:00
// FieldMap allows customization of the key names for default fields.
type FieldMap map[fieldKey]string
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
type JSONFormatter struct {
// TimestampFormat sets the format used for marshaling timestamps.
TimestampFormat string
// DisableTimestamp allows disabling automatic timestamps in output
DisableTimestamp bool
2020-03-13 20:22:12 +03:00
// DisableHTMLEscape allows disabling html escaping in output
DisableHTMLEscape bool
// 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.
// As an example:
// formatter := &JSONFormatter{
// FieldMap: FieldMap{
// FieldKeyTime: "@timestamp",
// FieldKeyLevel: "@level",
// FieldKeyMsg: "@message",
// FieldKeyFunc: "@caller",
// },
// }
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 bool
}
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) {
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`
// https://github.com/sirupsen/logrus/issues/137
2015-03-10 19:04:57 +03:00
data[k] = v.Error()
default:
data[k] = v
}
2014-12-10 05:53:40 +03:00
}
if f.DataKey != "" {
newData := make(Fields, 4)
newData[f.DataKey] = data
data = newData
}
prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
2015-05-26 16:02:13 +03:00
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
2017-07-26 15:26:30 +03:00
timestampFormat = defaultTimestampFormat
}
if entry.err != "" {
data[f.FieldMap.resolve(FieldKeyLogrusError)] = entry.err
}
if !f.DisableTimestamp {
data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat)
}
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() {
funcVal := entry.Caller.Function
fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
if f.CallerPrettyfier != nil {
funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
}
if funcVal != "" {
data[f.FieldMap.resolve(FieldKeyFunc)] = funcVal
}
if fileVal != "" {
data[f.FieldMap.resolve(FieldKeyFile)] = fileVal
}
}
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
encoder := json.NewEncoder(b)
2020-03-13 20:22:12 +03:00
encoder.SetEscapeHTML(!f.DisableHTMLEscape)
if f.PrettyPrint {
encoder.SetIndent("", " ")
}
if err := encoder.Encode(data); err != nil {
2020-11-08 09:19:28 +03:00
return nil, fmt.Errorf("failed to marshal fields to JSON, %w", err)
2014-03-11 03:22:08 +04:00
}
return b.Bytes(), nil
2014-03-11 03:22:08 +04:00
}