2022-03-20 19:19:42 +00:00
|
|
|
package zlog
|
2017-12-01 17:52:37 +00:00
|
|
|
|
|
|
|
// Hook defines an interface to a log hook.
|
|
|
|
type Hook interface {
|
|
|
|
// Run runs the hook with the event.
|
|
|
|
Run(e *Event, level Level, message string)
|
|
|
|
}
|
|
|
|
|
2018-07-26 22:53:02 +00:00
|
|
|
// HookFunc is an adaptor to allow the use of an ordinary function
|
|
|
|
// as a Hook.
|
|
|
|
type HookFunc func(e *Event, level Level, message string)
|
|
|
|
|
|
|
|
// Run implements the Hook interface.
|
|
|
|
func (h HookFunc) Run(e *Event, level Level, message string) {
|
|
|
|
h(e, level, message)
|
|
|
|
}
|
|
|
|
|
2017-12-01 17:52:37 +00:00
|
|
|
// LevelHook applies a different hook for each level.
|
|
|
|
type LevelHook struct {
|
2019-11-04 19:39:22 +00:00
|
|
|
NoLevelHook, TraceHook, DebugHook, InfoHook, WarnHook, ErrorHook, FatalHook, PanicHook Hook
|
2017-12-01 17:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the Hook interface.
|
|
|
|
func (h LevelHook) Run(e *Event, level Level, message string) {
|
|
|
|
switch level {
|
2019-11-04 19:39:22 +00:00
|
|
|
case TraceLevel:
|
|
|
|
if h.TraceHook != nil {
|
|
|
|
h.TraceHook.Run(e, level, message)
|
|
|
|
}
|
2017-12-01 17:52:37 +00:00
|
|
|
case DebugLevel:
|
|
|
|
if h.DebugHook != nil {
|
|
|
|
h.DebugHook.Run(e, level, message)
|
|
|
|
}
|
|
|
|
case InfoLevel:
|
|
|
|
if h.InfoHook != nil {
|
|
|
|
h.InfoHook.Run(e, level, message)
|
|
|
|
}
|
|
|
|
case WarnLevel:
|
|
|
|
if h.WarnHook != nil {
|
|
|
|
h.WarnHook.Run(e, level, message)
|
|
|
|
}
|
|
|
|
case ErrorLevel:
|
|
|
|
if h.ErrorHook != nil {
|
|
|
|
h.ErrorHook.Run(e, level, message)
|
|
|
|
}
|
|
|
|
case FatalLevel:
|
|
|
|
if h.FatalHook != nil {
|
|
|
|
h.FatalHook.Run(e, level, message)
|
|
|
|
}
|
|
|
|
case PanicLevel:
|
|
|
|
if h.PanicHook != nil {
|
|
|
|
h.PanicHook.Run(e, level, message)
|
|
|
|
}
|
|
|
|
case NoLevel:
|
|
|
|
if h.NoLevelHook != nil {
|
|
|
|
h.NoLevelHook.Run(e, level, message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLevelHook returns a new LevelHook.
|
|
|
|
func NewLevelHook() LevelHook {
|
|
|
|
return LevelHook{}
|
|
|
|
}
|