mirror of https://github.com/sirupsen/logrus.git
hooks: expose LevelHooks type
This commit is contained in:
parent
d3e4ea4032
commit
93a1736895
6
hooks.go
6
hooks.go
|
@ -11,11 +11,11 @@ type Hook interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal type for storing the hooks on a logger instance.
|
// Internal type for storing the hooks on a logger instance.
|
||||||
type levelHooks map[Level][]Hook
|
type LevelHooks map[Level][]Hook
|
||||||
|
|
||||||
// Add a hook to an instance of logger. This is called with
|
// Add a hook to an instance of logger. This is called with
|
||||||
// `log.Hooks.Add(new(MyHook))` where `MyHook` implements the `Hook` interface.
|
// `log.Hooks.Add(new(MyHook))` where `MyHook` implements the `Hook` interface.
|
||||||
func (hooks levelHooks) Add(hook Hook) {
|
func (hooks LevelHooks) Add(hook Hook) {
|
||||||
for _, level := range hook.Levels() {
|
for _, level := range hook.Levels() {
|
||||||
hooks[level] = append(hooks[level], hook)
|
hooks[level] = append(hooks[level], hook)
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ func (hooks levelHooks) Add(hook Hook) {
|
||||||
|
|
||||||
// Fire all the hooks for the passed level. Used by `entry.log` to fire
|
// Fire all the hooks for the passed level. Used by `entry.log` to fire
|
||||||
// appropriate hooks for a log entry.
|
// appropriate hooks for a log entry.
|
||||||
func (hooks levelHooks) Fire(level Level, entry *Entry) error {
|
func (hooks LevelHooks) Fire(level Level, entry *Entry) error {
|
||||||
for _, hook := range hooks[level] {
|
for _, hook := range hooks[level] {
|
||||||
if err := hook.Fire(entry); err != nil {
|
if err := hook.Fire(entry); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -14,7 +14,7 @@ type Logger struct {
|
||||||
// Hooks for the logger instance. These allow firing events based on logging
|
// Hooks for the logger instance. These allow firing events based on logging
|
||||||
// levels and log entries. For example, to send errors to an error tracking
|
// levels and log entries. For example, to send errors to an error tracking
|
||||||
// service, log to StatsD or dump the core on fatal errors.
|
// service, log to StatsD or dump the core on fatal errors.
|
||||||
Hooks levelHooks
|
Hooks LevelHooks
|
||||||
// All log entries pass through the formatter before logged to Out. The
|
// All log entries pass through the formatter before logged to Out. The
|
||||||
// included formatters are `TextFormatter` and `JSONFormatter` for which
|
// included formatters are `TextFormatter` and `JSONFormatter` for which
|
||||||
// TextFormatter is the default. In development (when a TTY is attached) it
|
// TextFormatter is the default. In development (when a TTY is attached) it
|
||||||
|
@ -37,7 +37,7 @@ type Logger struct {
|
||||||
// var log = &Logger{
|
// var log = &Logger{
|
||||||
// Out: os.Stderr,
|
// Out: os.Stderr,
|
||||||
// Formatter: new(JSONFormatter),
|
// Formatter: new(JSONFormatter),
|
||||||
// Hooks: make(levelHooks),
|
// Hooks: make(LevelHooks),
|
||||||
// Level: logrus.DebugLevel,
|
// Level: logrus.DebugLevel,
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
@ -46,7 +46,7 @@ func New() *Logger {
|
||||||
return &Logger{
|
return &Logger{
|
||||||
Out: os.Stderr,
|
Out: os.Stderr,
|
||||||
Formatter: new(TextFormatter),
|
Formatter: new(TextFormatter),
|
||||||
Hooks: make(levelHooks),
|
Hooks: make(LevelHooks),
|
||||||
Level: InfoLevel,
|
Level: InfoLevel,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue