-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
71 lines (57 loc) · 1.83 KB
/
hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package logger
import (
"context"
"github.com/sirupsen/logrus"
)
// Hook defines the interface a custom Hook needs to implement
type Hook interface {
Fire(*HookEntry) (changed bool, err error)
}
// HookFunc can be used to convert a simple function to implement the Hook interface.
type HookFunc func(*HookEntry) (changed bool, err error)
// Fire redirects a function call to the function receiver
func (hf HookFunc) Fire(he *HookEntry) (changed bool, err error) {
return hf(he)
}
// HookEntry contains the fields provided for mutation in a hook.
type HookEntry struct {
// Contains all the fields set by the user.
Data Fields
// Level the log entry was logged at: Trace, Debug, Info, Warn, Error, Fatal or Panic
// This field will be set on entry firing and the value will be equal to the one in Logger struct field.
Level Level
// Message passed to Trace, Debug, Info, Warn, Error, Fatal or Panic
Message string
// Contains the context set by the user. Useful for hook processing etc.
Context context.Context
}
type logrusHook struct {
hook Hook
}
// Levels implements the logrus.Hook interface.
func (h *logrusHook) Levels() []logrus.Level {
return logrus.AllLevels
}
// Fire implements the logrus.Hook interface.
func (h *logrusHook) Fire(entry *logrus.Entry) error {
// Provide all entry-data so the hook can mutate them.
hookEntry := &HookEntry{
Data: Fields(entry.Data),
Level: mapLogrusLevelToLevel(entry.Level),
Message: entry.Message,
Context: entry.Context,
}
changed, err := h.hook.Fire(hookEntry)
if err != nil {
return err
}
if !changed {
return nil
}
// Mutate the actual logrus entry with the mutations done in the hook.
entry.Data = logrus.Fields(hookEntry.Data)
entry.Level = mapLevelToLogrusLevel(hookEntry.Level)
entry.Message = hookEntry.Message
entry.Context = hookEntry.Context
return nil
}