2016-04-10 14:39:38 -07:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
2018-11-17 16:28:29 -08:00
|
|
|
"bytes"
|
2016-04-10 14:39:38 -07:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-06-16 14:33:25 -07:00
|
|
|
"runtime"
|
2016-04-10 14:39:38 -07:00
|
|
|
)
|
|
|
|
|
2017-03-25 12:45:10 -07:00
|
|
|
type fieldKey string
|
2018-02-20 14:41:09 -08:00
|
|
|
|
|
|
|
// FieldMap allows customization of the key names for default fields.
|
2017-03-25 12:45:10 -07:00
|
|
|
type FieldMap map[fieldKey]string
|
|
|
|
|
|
|
|
func (f FieldMap) resolve(key fieldKey) string {
|
|
|
|
if k, ok := f[key]; ok {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(key)
|
|
|
|
}
|
|
|
|
|
2018-02-20 14:41:09 -08:00
|
|
|
// JSONFormatter formats logs into parsable json
|
2016-04-10 14:39:38 -07:00
|
|
|
type JSONFormatter struct {
|
|
|
|
// TimestampFormat sets the format used for marshaling timestamps.
|
|
|
|
TimestampFormat string
|
2017-03-25 12:45:10 -07:00
|
|
|
|
|
|
|
// DisableTimestamp allows disabling automatic timestamps in output
|
|
|
|
DisableTimestamp bool
|
|
|
|
|
2020-05-23 15:06:21 -07:00
|
|
|
// DisableHTMLEscape allows disabling html escaping in output
|
|
|
|
DisableHTMLEscape bool
|
|
|
|
|
2018-11-17 16:28:29 -08:00
|
|
|
// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
|
|
|
|
DataKey string
|
|
|
|
|
2018-02-20 14:41:09 -08:00
|
|
|
// FieldMap allows users to customize the names of keys for default fields.
|
2017-03-25 12:45:10 -07:00
|
|
|
// As an example:
|
|
|
|
// formatter := &JSONFormatter{
|
|
|
|
// FieldMap: FieldMap{
|
2018-11-17 16:28:29 -08:00
|
|
|
// FieldKeyTime: "@timestamp",
|
2017-03-25 12:45:10 -07:00
|
|
|
// FieldKeyLevel: "@level",
|
2018-11-17 16:28:29 -08:00
|
|
|
// FieldKeyMsg: "@message",
|
|
|
|
// FieldKeyFunc: "@caller",
|
2017-03-25 12:45:10 -07:00
|
|
|
// },
|
|
|
|
// }
|
|
|
|
FieldMap FieldMap
|
2018-11-17 16:28:29 -08:00
|
|
|
|
2019-06-16 14:33:25 -07:00
|
|
|
// 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)
|
|
|
|
|
2018-11-17 16:28:29 -08:00
|
|
|
// PrettyPrint will indent all json logs
|
|
|
|
PrettyPrint bool
|
2016-04-10 14:39:38 -07:00
|
|
|
}
|
|
|
|
|
2018-02-20 14:41:09 -08:00
|
|
|
// Format renders a single log entry
|
2016-04-10 14:39:38 -07:00
|
|
|
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
2018-11-17 16:28:29 -08:00
|
|
|
data := make(Fields, len(entry.Data)+4)
|
2016-04-10 14:39:38 -07:00
|
|
|
for k, v := range entry.Data {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case error:
|
|
|
|
// Otherwise errors are ignored by `encoding/json`
|
2018-02-20 14:41:09 -08:00
|
|
|
// https://github.com/sirupsen/logrus/issues/137
|
2016-04-10 14:39:38 -07:00
|
|
|
data[k] = v.Error()
|
|
|
|
default:
|
|
|
|
data[k] = v
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 16:28:29 -08:00
|
|
|
|
|
|
|
if f.DataKey != "" {
|
|
|
|
newData := make(Fields, 4)
|
|
|
|
newData[f.DataKey] = data
|
|
|
|
data = newData
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
|
2016-04-10 14:39:38 -07:00
|
|
|
|
|
|
|
timestampFormat := f.TimestampFormat
|
|
|
|
if timestampFormat == "" {
|
2018-02-20 14:41:09 -08:00
|
|
|
timestampFormat = defaultTimestampFormat
|
2016-04-10 14:39:38 -07:00
|
|
|
}
|
|
|
|
|
2018-11-17 16:28:29 -08:00
|
|
|
if entry.err != "" {
|
|
|
|
data[f.FieldMap.resolve(FieldKeyLogrusError)] = entry.err
|
|
|
|
}
|
2017-03-25 12:45:10 -07:00
|
|
|
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()
|
2018-11-17 16:28:29 -08:00
|
|
|
if entry.HasCaller() {
|
2019-06-16 14:33:25 -07:00
|
|
|
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
|
|
|
|
}
|
2018-11-17 16:28:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var b *bytes.Buffer
|
|
|
|
if entry.Buffer != nil {
|
|
|
|
b = entry.Buffer
|
|
|
|
} else {
|
|
|
|
b = &bytes.Buffer{}
|
|
|
|
}
|
2016-04-10 14:39:38 -07:00
|
|
|
|
2018-11-17 16:28:29 -08:00
|
|
|
encoder := json.NewEncoder(b)
|
2020-05-23 15:06:21 -07:00
|
|
|
encoder.SetEscapeHTML(!f.DisableHTMLEscape)
|
2018-11-17 16:28:29 -08:00
|
|
|
if f.PrettyPrint {
|
|
|
|
encoder.SetIndent("", " ")
|
|
|
|
}
|
|
|
|
if err := encoder.Encode(data); err != nil {
|
2019-06-16 14:33:25 -07:00
|
|
|
return nil, fmt.Errorf("failed to marshal fields to JSON, %v", err)
|
2016-04-10 14:39:38 -07:00
|
|
|
}
|
2018-11-17 16:28:29 -08:00
|
|
|
|
|
|
|
return b.Bytes(), nil
|
2016-04-10 14:39:38 -07:00
|
|
|
}
|