package logrus
import (
)
type fieldKey string
type FieldMap map[fieldKey]string
func ( FieldMap) ( fieldKey) string {
if , := []; {
return
}
return string()
}
type JSONFormatter struct {
TimestampFormat string
DisableTimestamp bool
DisableHTMLEscape bool
DataKey string
FieldMap FieldMap
CallerPrettyfier func(*runtime.Frame) (function string, file string)
PrettyPrint bool
}
func ( *JSONFormatter) ( *Entry) ([]byte, error) {
:= make(Fields, len(.Data)+4)
for , := range .Data {
switch v := .(type) {
case error:
[] = .Error()
default:
[] =
}
}
if .DataKey != "" {
:= make(Fields, 4)
[.DataKey] =
=
}
prefixFieldClashes(, .FieldMap, .HasCaller())
:= .TimestampFormat
if == "" {
= defaultTimestampFormat
}
if .err != "" {
[.FieldMap.resolve(FieldKeyLogrusError)] = .err
}
if !.DisableTimestamp {
[.FieldMap.resolve(FieldKeyTime)] = .Time.Format()
}
[.FieldMap.resolve(FieldKeyMsg)] = .Message
[.FieldMap.resolve(FieldKeyLevel)] = .Level.String()
if .HasCaller() {
:= .Caller.Function
:= fmt.Sprintf("%s:%d", .Caller.File, .Caller.Line)
if .CallerPrettyfier != nil {
, = .CallerPrettyfier(.Caller)
}
if != "" {
[.FieldMap.resolve(FieldKeyFunc)] =
}
if != "" {
[.FieldMap.resolve(FieldKeyFile)] =
}
}
var *bytes.Buffer
if .Buffer != nil {
= .Buffer
} else {
= &bytes.Buffer{}
}
:= json.NewEncoder()
.SetEscapeHTML(!.DisableHTMLEscape)
if .PrettyPrint {
.SetIndent("", " ")
}
if := .Encode(); != nil {
return nil, fmt.Errorf("failed to marshal fields to JSON, %w", )
}
return .Bytes(), nil
}