Skip to content

Commit

Permalink
Merge pull request #24 from rifflock/allow-setting-formatter-in-const…
Browse files Browse the repository at this point in the history
…ructor

Add formatter to constructor signature
  • Loading branch information
rifflock committed Dec 6, 2017
2 parents 3bcf86f + bd08170 commit 765177c
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 765177c

Please sign in to comment.