Skip to content

Commit

Permalink
Add hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
iancmcc committed Aug 18, 2016
1 parent 488ad3c commit 21ac1a3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package logri_test
import (
"bytes"

. "github.com/iancmcc/logri"
. "github.com/zenoss/logri"

. "gopkg.in/check.v1"
)
Expand Down
31 changes: 31 additions & 0 deletions hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package logri

import "github.com/Sirupsen/logrus"

// LoggerHook is a Logrus hook that adds the logger name as a field to the entry
type LoggerHook struct {
loggerName string
}

// Levels satisfies the logrus.Hook interface
func (hook LoggerHook) Levels() []logrus.Level {
return logrus.AllLevels
}

// Fire satisfies the logrus.Hook interface
func (hook LoggerHook) Fire(entry *logrus.Entry) error {
entry.Data["logger"] = hook.loggerName
return nil
}

func copyHooksExceptLoggerHook(orig logrus.LevelHooks) logrus.LevelHooks {
result := logrus.LevelHooks{}
for level, hooks := range orig {
for _, hook := range hooks {
if _, ok := hook.(LoggerHook); !ok {
result[level] = append(result[level], hook)
}
}
}
return result
}
2 changes: 1 addition & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (l *Logger) GetChild(name string) *Logger {
logger: &logrus.Logger{
Out: parent.logger.Out,
Formatter: parent.logger.Formatter,
Hooks: CopyHooksExceptLoggerHook(parent.logger.Hooks),
Hooks: copyHooksExceptLoggerHook(parent.logger.Hooks),
Level: parent.logger.Level,
},
}
Expand Down
2 changes: 1 addition & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"

"github.com/Sirupsen/logrus"
. "github.com/iancmcc/logri"
. "github.com/zenoss/logri"
. "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion logri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/Sirupsen/logrus"
"github.com/Sirupsen/logrus/hooks/test"
. "github.com/iancmcc/logri"
. "github.com/zenoss/logri"
. "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"path/filepath"

. "github.com/iancmcc/logri"
. "github.com/zenoss/logri"

. "gopkg.in/check.v1"
)
Expand Down

0 comments on commit 21ac1a3

Please sign in to comment.