package logrusimport ()type fieldKey string// FieldMap allows customization of the key names for default fields.typeFieldMapmap[fieldKey]stringfunc ( FieldMap) ( fieldKey) string {if , := []; {return }returnstring()}// JSONFormatter formats logs into parsable JSON.//// Fields from [Entry.Data] are included in the JSON object together with the// standard fields derived from the entry. If a field conflicts with a standard// field, it is prefixed with "fields.". Standard field names can be customized// through FieldMap. When DataKey is set, fields from [Entry.Data] are nested// under that key instead.typeJSONFormatterstruct {// TimestampFormat sets the format used for marshaling timestamps. // The format to use is the same than for time.Format or time.Parse from the standard // library. // The standard Library already provides a set of predefined format. TimestampFormat string// DisableTimestamp allows disabling automatic timestamps in output DisableTimestamp bool// 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// 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}// Format renders a single log entryfunc ( *JSONFormatter) ( *Entry) ([]byte, error) { := .Caller := make(Fields, len(.Data)+defaultFields)for , := range .Data {switch v := .(type) {caseerror:// Otherwise errors are ignored by `encoding/json` // https://github.com/sirupsen/logrus/issues/137 [] = .Error()default: [] = } }if .DataKey != "" && len(.Data) > 0 { := make(Fields, defaultFields+1) [.DataKey] = = } := != nilprefixFieldClashes(, .FieldMap, ) := .TimestampFormatif == "" { = defaultTimestampFormat }if .err != "" { [.FieldMap.resolve(FieldKeyLogrusError)] = .err }if !.DisableTimestamp { [.FieldMap.resolve(FieldKeyTime)] = .Time.Format() } [.FieldMap.resolve(FieldKeyMsg)] = .Message [.FieldMap.resolve(FieldKeyLevel)] = .Level.String()if != nil {var , stringif .CallerPrettyfier != nil { , = .CallerPrettyfier() } else { = .Function = .File + ":" + strconv.FormatInt(int64(.Line), 10) }if != "" { [.FieldMap.resolve(FieldKeyFunc)] = }if != "" { [.FieldMap.resolve(FieldKeyFile)] = } } := .Bufferif == nil { = new(bytes.Buffer) } := json.NewEncoder() .SetEscapeHTML(!.DisableHTMLEscape)if .PrettyPrint { .SetIndent("", " ") }if := .Encode(); != nil {returnnil, fmt.Errorf("failed to marshal fields to JSON, %w", ) }return .Bytes(), nil}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.