mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
008b217844
this fixes the issue that was blocking a test from running on ppc64le. the logrus revendor changes the color code used in that same test, so that breaks the test for all platforms (updated in this pr) Signed-off-by: Christy Perez <christy@linux.vnet.ibm.com>
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package logrus
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
type fieldKey string
|
|
|
|
// FieldMap allows customization of the key names for default fields.
|
|
type FieldMap map[fieldKey]string
|
|
|
|
// Default key names for the default fields
|
|
const (
|
|
FieldKeyMsg = "msg"
|
|
FieldKeyLevel = "level"
|
|
FieldKeyTime = "time"
|
|
)
|
|
|
|
func (f FieldMap) resolve(key fieldKey) string {
|
|
if k, ok := f[key]; ok {
|
|
return k
|
|
}
|
|
|
|
return string(key)
|
|
}
|
|
|
|
// 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
|
|
|
|
// 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",
|
|
// },
|
|
// }
|
|
FieldMap FieldMap
|
|
}
|
|
|
|
// Format renders a single log entry
|
|
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
|
data := make(Fields, len(entry.Data)+3)
|
|
for k, v := range entry.Data {
|
|
switch v := v.(type) {
|
|
case error:
|
|
// Otherwise errors are ignored by `encoding/json`
|
|
// https://github.com/sirupsen/logrus/issues/137
|
|
data[k] = v.Error()
|
|
default:
|
|
data[k] = v
|
|
}
|
|
}
|
|
prefixFieldClashes(data)
|
|
|
|
timestampFormat := f.TimestampFormat
|
|
if timestampFormat == "" {
|
|
timestampFormat = defaultTimestampFormat
|
|
}
|
|
|
|
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()
|
|
|
|
serialized, err := json.Marshal(data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
|
|
}
|
|
return append(serialized, '\n'), nil
|
|
}
|