kompose/vendor/github.com/Sirupsen/logrus/json_formatter.go
Charlie Drage 43b1ca0fe0 Resolve logrus and gojsonschema vendoring conflicts
Some vendoring is blocking other PR's due to changes to logrus (Sirupsen
vs sirupsen for lower-case) as well as a non-versioned gojsonschema in
glide.yaml

This updates glide.yaml to reflect the upper-case as well as lower-case
versions of logrus as well as adds a versionized gojsonschema in order
to get rid of the following error:

```sh
▶ make bin
go build -ldflags="-w -X github.com/kubernetes/kompose/cmd.GITCOMMIT=0e56b7d" -o kompose main.go
vendor/github.com/docker/cli/cli/compose/schema/schema.go:34: cannot use portsFormatChecker literal (type portsFormatChecker) as type gojsonschema.FormatChecker in argument to gojsonschema.FormatCheckers.Add:
        portsFormatChecker does not implement gojsonschema.FormatChecker (wrong type for IsFormat method)
                have IsFormat(string) bool
                want IsFormat(interface {}) bool
vendor/github.com/docker/cli/cli/compose/schema/schema.go:35: cannot use portsFormatChecker literal (type portsFormatChecker) as type gojsonschema.FormatChecker in argument to gojsonschema.FormatCheckers.Add:
        portsFormatChecker does not implement gojsonschema.FormatChecker (wrong type for IsFormat method)
                have IsFormat(string) bool
                want IsFormat(interface {}) bool
vendor/github.com/docker/cli/cli/compose/schema/schema.go:36: cannot use durationFormatChecker literal (type durationFormatChecker) as type gojsonschema.FormatChecker in argument to gojsonschema.FormatCheckers.Add:
        durationFormatChecker does not implement gojsonschema.FormatChecker (wrong type for IsFormat method)
                have IsFormat(string) bool
                want IsFormat(interface {}) bool
vendor/github.com/docker/libcompose/config/schema_helpers.go:60: cannot use environmentFormatChecker literal (type environmentFormatChecker) as type gojsonschema.FormatChecker in argument to gojsonschema.FormatCheckers.Add:
        environmentFormatChecker does not implement gojsonschema.FormatChecker (wrong type for IsFormat method)
                have IsFormat(string) bool
                want IsFormat(interface {}) bool
vendor/github.com/docker/libcompose/config/schema_helpers.go:61: cannot use portsFormatChecker literal (type portsFormatChecker) as type gojsonschema.FormatChecker in argument to gojsonschema.FormatCheckers.Add:
        portsFormatChecker does not implement gojsonschema.FormatChecker (wrong type for IsFormat method)
                have IsFormat(string) bool
                want IsFormat(interface {}) bool
vendor/github.com/docker/libcompose/config/schema_helpers.go:62: cannot use portsFormatChecker literal (type portsFormatChecker) as type gojsonschema.FormatChecker in argument to gojsonschema.FormatCheckers.Add:
        portsFormatChecker does not implement gojsonschema.FormatChecker (wrong type for IsFormat method)
                have IsFormat(string) bool
                want IsFormat(interface {}) bool
^CMakefile:29: recipe for target 'bin' failed
make: *** [bin] Interrupt
```
2017-10-02 11:29:18 -04:00

80 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
}