Skip to content

Commit

Permalink
Merge branch 'marconi-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rifflock committed Jul 27, 2016
2 parents 67c9273 + 8d5227f commit f9d14dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
22 changes: 17 additions & 5 deletions lfshook.go
Expand Up @@ -9,6 +9,9 @@ import (
"sync"
)

// We are logging to file, strip colors to make the output more readable
var txtFormatter = &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
type PathMap map[logrus.Level]string
Expand All @@ -26,7 +29,7 @@ type lfsHook struct {
func NewHook(levelMap PathMap) *lfsHook {
hook := &lfsHook{
paths: levelMap,
lock: new(sync.Mutex),
lock: new(sync.Mutex),
}
for level, _ := range levelMap {
hook.levels = append(hook.levels, level)
Expand All @@ -44,10 +47,10 @@ func (hook *lfsHook) Fire(entry *logrus.Entry) error {
err error
ok bool
)
hook.lock.Lock()
defer hook.lock.Unlock()

hook.lock.Lock()
defer hook.lock.Unlock()

if path, ok = hook.paths[entry.Level]; !ok {
err = fmt.Errorf("no file provided for loglevel: %d", entry.Level)
log.Println(err.Error())
Expand All @@ -59,7 +62,16 @@ func (hook *lfsHook) Fire(entry *logrus.Entry) error {
return err
}
defer fd.Close()

// switch to TextFormatter
formatter := entry.Logger.Formatter
entry.Logger.Formatter = txtFormatter
defer func() {
// assign back original formatter
entry.Logger.Formatter = formatter
}()
msg, err = entry.String()

if err != nil {
log.Println("failed to generate string for entry:", err)
return err
Expand Down
1 change: 0 additions & 1 deletion lfshook_test.go
Expand Up @@ -15,7 +15,6 @@ const expectedMsg = "This is the expected test message."
func TestLogEntryWritten(t *testing.T) {
log := logrus.New()
// The colors were messing with the regexp so I turned them off.
log.Formatter = &logrus.TextFormatter{DisableColors: true}
tmpfile, err := ioutil.TempFile("", "test_lfshook")
if err != nil {
t.Errorf("Unable to generate logfile due to err: %s", err)
Expand Down

0 comments on commit f9d14dd

Please sign in to comment.