Skip to content

Commit

Permalink
custom message field (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
closer authored and evalphobia committed Sep 1, 2017
1 parent 98607c4 commit 1b50871
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions fluent.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type FluentHook struct {
levels []logrus.Level
tag *string

messageField string
ignoreFields map[string]struct{}
filters map[string]func(interface{}) interface{}
}
Expand All @@ -56,6 +57,7 @@ func New(host string, port int) (*FluentHook, error) {
return &FluentHook{
levels: defaultLevels,
Fluent: fd,
messageField: MessageField,
ignoreFields: make(map[string]struct{}),
filters: make(map[string]func(interface{}) interface{}),
}, nil
Expand All @@ -69,6 +71,7 @@ func NewHook(host string, port int) *FluentHook {
port: port,
levels: defaultLevels,
tag: nil,
messageField: MessageField,
ignoreFields: make(map[string]struct{}),
filters: make(map[string]func(interface{}) interface{}),
}
Expand Down Expand Up @@ -98,6 +101,11 @@ func (hook *FluentHook) SetTag(tag string) {
hook.tag = &tag
}

// SetMessageField sets custom message field.
func (hook *FluentHook) SetMessageField(messageField string) {
hook.messageField = messageField
}

// AddIgnore adds field name to ignore.
func (hook *FluentHook) AddIgnore(name string) {
hook.ignoreFields[name] = struct{}{}
Expand Down Expand Up @@ -142,7 +150,7 @@ func (hook *FluentHook) Fire(entry *logrus.Entry) error {
setLevelString(entry, data)
tag := hook.getTagAndDel(entry, data)
if tag != entry.Message {
setMessage(entry, data)
hook.setMessage(entry, data)
}

fluentData := ConvertToValue(data, TagName)
Expand Down Expand Up @@ -175,12 +183,18 @@ func (hook *FluentHook) getTagAndDel(entry *logrus.Entry, data logrus.Fields) st
return tag
}

func setLevelString(entry *logrus.Entry, data logrus.Fields) {
data["level"] = entry.Level.String()
func (hook *FluentHook) setMessage(entry *logrus.Entry, data logrus.Fields) {
if _, ok := data[hook.messageField]; ok {
return
}
var v interface{}
v = entry.Message
if fn, ok := hook.filters[hook.messageField]; ok {
v = fn(v)
}
data[hook.messageField] = v
}

func setMessage(entry *logrus.Entry, data logrus.Fields) {
if _, ok := data[MessageField]; !ok {
data[MessageField] = entry.Message
}
func setLevelString(entry *logrus.Entry, data logrus.Fields) {
data["level"] = entry.Level.String()
}

0 comments on commit 1b50871

Please sign in to comment.