/
hook.go
152 lines (130 loc) · 2.72 KB
/
hook.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package mysqlhook
import (
"database/sql"
"io"
"sync"
"github.com/LyricTian/queue"
"github.com/Sirupsen/logrus"
)
var defaultOptions = options{
maxQueues: 128,
maxWorkers: 2,
levels: []logrus.Level{
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
},
}
// FilterHandle a filter handler
type FilterHandle func(*logrus.Entry) *logrus.Entry
type options struct {
maxQueues int
maxWorkers int
extra map[string]interface{}
exec Execer
filter FilterHandle
levels []logrus.Level
out io.Writer
}
// SetMaxQueues set the number of buffers
func SetMaxQueues(maxQueues int) Option {
return func(o *options) {
o.maxQueues = maxQueues
}
}
// SetMaxWorkers set the number of worker threads
func SetMaxWorkers(maxWorkers int) Option {
return func(o *options) {
o.maxWorkers = maxWorkers
}
}
// SetExtra set extended parameters
func SetExtra(extra map[string]interface{}) Option {
return func(o *options) {
o.extra = extra
}
}
// SetExec set the Execer interface
func SetExec(exec Execer) Option {
return func(o *options) {
o.exec = exec
}
}
// SetFilter set the entry filter
func SetFilter(filter FilterHandle) Option {
return func(o *options) {
o.filter = filter
}
}
// SetLevels set the available log level
func SetLevels(levels ...logrus.Level) Option {
return func(o *options) {
if len(levels) == 0 {
return
}
o.levels = levels
}
}
// SetOut set error output
func SetOut(out io.Writer) Option {
return func(o *options) {
o.out = out
}
}
// Option a hook parameter options
type Option func(*options)
// Default create a default mysql hook
func Default(db *sql.DB, tableName string) *Hook {
return New(
SetExec(NewExec(db, tableName)),
)
}
// New creates a hook to be added to an instance of logger
func New(opt ...Option) *Hook {
opts := defaultOptions
for _, o := range opt {
o(&opts)
}
if opts.exec == nil {
panic("Unknown Execer interface implementation")
}
q := queue.NewQueue(opts.maxQueues, opts.maxWorkers)
q.Run()
return &Hook{
opts: opts,
q: q,
pool: sync.Pool{
New: func() interface{} {
return &job{
out: opts.out,
extra: opts.extra,
exec: opts.exec,
filter: opts.filter,
}
},
},
}
}
// Hook to send logs to a mysql database
type Hook struct {
opts options
q *queue.Queue
pool sync.Pool
}
// Levels returns the available logging levels
func (h *Hook) Levels() []logrus.Level {
return h.opts.levels
}
// Fire is called when a log event is fired
func (h *Hook) Fire(entry *logrus.Entry) error {
job := h.pool.Get().(*job)
job.Reset(entry)
h.q.Push(job)
return nil
}
// Flush waits for the log queue to be empty
func (h *Hook) Flush() {
h.q.Terminate()
}