forked from mirror/logrus
Added FieldMap to reduce potential struct bloat
This commit is contained in:
parent
b2c6f8aa8b
commit
d5ca23f998
|
@ -4,21 +4,27 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fieldKey string
|
type fieldKey string
|
||||||
|
type FieldMap map[fieldKey]string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultKeyMsg = "msg"
|
FieldKeyMsg = "msg"
|
||||||
DefaultKeyLevel = "level"
|
FieldKeyLevel = "level"
|
||||||
DefaultKeyTime = "time"
|
FieldKeyTime = "time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (f FieldMap) resolve(key fieldKey) string {
|
||||||
|
if k, ok := f[key]; ok {
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(key)
|
||||||
|
}
|
||||||
|
|
||||||
type JSONFormatter struct {
|
type JSONFormatter struct {
|
||||||
// TimestampFormat sets the format used for marshaling timestamps.
|
// TimestampFormat sets the format used for marshaling timestamps.
|
||||||
TimestampFormat string
|
TimestampFormat string
|
||||||
MessageKey string
|
FieldMap FieldMap
|
||||||
LevelKey string
|
|
||||||
TimeKey string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
|
@ -40,9 +46,9 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
timestampFormat = DefaultTimestampFormat
|
timestampFormat = DefaultTimestampFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
data[f.resolveKey(f.TimeKey, DefaultKeyTime)] = entry.Time.Format(timestampFormat)
|
data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat)
|
||||||
data[f.resolveKey(f.MessageKey, DefaultKeyMsg)] = entry.Message
|
data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
|
||||||
data[f.resolveKey(f.LevelKey, DefaultKeyLevel)] = entry.Level.String()
|
data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
|
||||||
|
|
||||||
serialized, err := json.Marshal(data)
|
serialized, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -50,10 +56,3 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
}
|
}
|
||||||
return append(serialized, '\n'), nil
|
return append(serialized, '\n'), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *JSONFormatter) resolveKey(key, defaultKey string) string {
|
|
||||||
if len(key) > 0 {
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
return defaultKey
|
|
||||||
}
|
|
||||||
|
|
|
@ -120,7 +120,11 @@ func TestJSONEntryEndsWithNewline(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSONMessageKey(t *testing.T) {
|
func TestJSONMessageKey(t *testing.T) {
|
||||||
formatter := &JSONFormatter{MessageKey: "message"}
|
formatter := &JSONFormatter{
|
||||||
|
FieldMap: FieldMap{
|
||||||
|
FieldKeyMsg: "message",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
b, err := formatter.Format(&Entry{Message: "oh hai"})
|
b, err := formatter.Format(&Entry{Message: "oh hai"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -133,7 +137,11 @@ func TestJSONMessageKey(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSONLevelKey(t *testing.T) {
|
func TestJSONLevelKey(t *testing.T) {
|
||||||
formatter := &JSONFormatter{LevelKey: "somelevel"}
|
formatter := &JSONFormatter{
|
||||||
|
FieldMap: FieldMap{
|
||||||
|
FieldKeyLevel: "somelevel",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
b, err := formatter.Format(WithField("level", "something"))
|
b, err := formatter.Format(WithField("level", "something"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -146,7 +154,11 @@ func TestJSONLevelKey(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSONTimeKey(t *testing.T) {
|
func TestJSONTimeKey(t *testing.T) {
|
||||||
formatter := &JSONFormatter{TimeKey: "timeywimey"}
|
formatter := &JSONFormatter{
|
||||||
|
FieldMap: FieldMap{
|
||||||
|
FieldKeyTime: "timeywimey",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
b, err := formatter.Format(WithField("level", "something"))
|
b, err := formatter.Format(WithField("level", "something"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue