Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
This is a breaking change. Major version revision required.
Add formatter to constructor signature
Add formatter to test NewHook
  • Loading branch information
riffle committed Nov 30, 2017
1 parent 3bcf86f commit bd08170
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lfshook.go
Expand Up @@ -13,7 +13,7 @@ import (
)

// We are logging to file, strip colors to make the output more readable
var txtFormatter = &logrus.TextFormatter{DisableColors: true}
var defaultFormatter = &logrus.TextFormatter{DisableColors: true}

// Map for linking a log level to a log file
// Multiple levels may share a file, but multiple files may not be used for one level
Expand All @@ -34,10 +34,16 @@ type lfsHook struct {
// Given a map with keys equal to log levels.
// We can generate our levels handled on the fly, and write to a specific file for each level.
// We can also write to the same file for all levels. They just need to be specified.
func NewHook(levelMap interface{}) *lfsHook {
func NewHook(levelMap interface{}, userFormatter logrus.Formatter) *lfsHook {
var formatter logrus.Formatter
if userFormatter != nil {
formatter = userFormatter
} else {
formatter = defaultFormatter
}
hook := &lfsHook{
lock: new(sync.Mutex),
formatter: txtFormatter,
formatter: formatter,
}

switch levelMap.(type) {
Expand Down
2 changes: 1 addition & 1 deletion lfshook_test.go
Expand Up @@ -27,7 +27,7 @@ func TestLogEntryWritten(t *testing.T) {
}()
hook := NewHook(PathMap{
logrus.InfoLevel: fname,
})
}, nil)
log.Hooks.Add(hook)

log.Info(expectedMsg)
Expand Down

0 comments on commit bd08170

Please sign in to comment.