Skip to content

Commit

Permalink
hook with default fields
Browse files Browse the repository at this point in the history
  • Loading branch information
xomaczar committed Feb 9, 2018
1 parent b5b21a8 commit f7f7e19
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package hooks

import (
"github.com/sirupsen/logrus"
)

// DefaultFieldsHook represents hook with fields
type DefaultFieldsHook struct {
defaultFields map[string]interface{}
levels []logrus.Level
}

func (dfh DefaultFieldsHook) AddDefaultField(key string, val interface{}) {
dfh.defaultFields[key] = val
}

func (dfh DefaultFieldsHook) Levels() []logrus.Level {
return dfh.levels
}

func (dfh DefaultFieldsHook) Fire(entry *logrus.Entry) error {
for key, val := range dfh.defaultFields {
entry.Data[key] = val
}
return nil
}

func NewDefaultFieldsHook(levels []logrus.Level) *DefaultFieldsHook {
fields := map[string]interface{}{}
return &DefaultFieldsHook{
defaultFields: fields,
levels: levels,
}

}

0 comments on commit f7f7e19

Please sign in to comment.