forked from sohlich/elogrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
238 lines (209 loc) · 6.92 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package elogrus
import (
"context"
"fmt"
"path"
"strings"
"time"
"github.com/olivere/elastic/v7"
"github.com/sirupsen/logrus"
)
var (
// ErrCannotCreateIndex Fired if the index is not created
ErrCannotCreateIndex = fmt.Errorf("cannot create index")
)
// IndexNameFunc get index name
type IndexNameFunc func() string
type fireFunc func(entry *logrus.Entry, hook *ElasticHook) error
// ElasticHook is a logrus
// hook for ElasticSearch
type ElasticHook struct {
client *elastic.Client
host string
index IndexNameFunc
levels []logrus.Level
ctx context.Context
ctxCancel context.CancelFunc
fireFunc fireFunc
}
type message struct {
Host string `json:"Host,omitempty"`
Timestamp string `json:"@timestamp"`
File string `json:"File,omitempty"`
Func string `json:"Func,omitempty"`
Line string `json:"Line,omitempty"`
Message string `json:"Message,omitempty"`
Data logrus.Fields
Level string `json:"Level,omitempty"`
}
// NewElasticHook creates new hook.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// index - name of the index in ElasticSearch
func NewElasticHook(client *elastic.Client, host string, level logrus.Level, index string) (*ElasticHook, error) {
return NewElasticHookWithFunc(client, host, level, func() string { return index })
}
// NewAsyncElasticHook creates new hook with asynchronous log.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// index - name of the index in ElasticSearch
func NewAsyncElasticHook(client *elastic.Client, host string, level logrus.Level, index string) (*ElasticHook, error) {
return NewAsyncElasticHookWithFunc(client, host, level, func() string { return index })
}
// NewBulkProcessorElasticHook creates new hook that uses a bulk processor for indexing.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// index - name of the index in ElasticSearch
func NewBulkProcessorElasticHook(client *elastic.Client, host string, level logrus.Level, index string) (*ElasticHook, error) {
return NewBulkProcessorElasticHookWithFunc(client, host, level, func() string { return index })
}
// NewElasticHookWithFunc creates new hook with
// function that provides the index name. This is useful if the index name is
// somehow dynamic especially based on time.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// indexFunc - function providing the name of index
func NewElasticHookWithFunc(client *elastic.Client, host string, level logrus.Level, indexFunc IndexNameFunc) (*ElasticHook, error) {
return newHookFuncAndFireFunc(client, host, level, indexFunc, syncFireFunc)
}
// NewAsyncElasticHookWithFunc creates new asynchronous hook with
// function that provides the index name. This is useful if the index name is
// somehow dynamic especially based on time.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// indexFunc - function providing the name of index
func NewAsyncElasticHookWithFunc(client *elastic.Client, host string, level logrus.Level, indexFunc IndexNameFunc) (*ElasticHook, error) {
return newHookFuncAndFireFunc(client, host, level, indexFunc, asyncFireFunc)
}
// NewBulkProcessorElasticHookWithFunc creates new hook with
// function that provides the index name. This is useful if the index name is
// somehow dynamic especially based on time that uses a bulk processor for
// indexing.
// client - ElasticSearch client with specific es version (v5/v6/v7/...)
// host - host of system
// level - log level
// indexFunc - function providing the name of index
func NewBulkProcessorElasticHookWithFunc(client *elastic.Client, host string, level logrus.Level, indexFunc IndexNameFunc) (*ElasticHook, error) {
fireFunc, err := makeBulkFireFunc(client)
if err != nil {
return nil, err
}
return newHookFuncAndFireFunc(client, host, level, indexFunc, fireFunc)
}
func newHookFuncAndFireFunc(client *elastic.Client, host string, level logrus.Level, indexFunc IndexNameFunc, fireFunc fireFunc) (*ElasticHook, error) {
var levels []logrus.Level
for _, l := range []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
logrus.TraceLevel,
} {
if l <= level {
levels = append(levels, l)
}
}
ctx, cancel := context.WithCancel(context.TODO())
// Use the IndexExists service to check if a specified index exists.
exists, err := client.IndexExists(indexFunc()).Do(ctx)
if err != nil {
// Handle error
cancel()
return nil, err
}
if !exists {
createIndex, err := client.CreateIndex(indexFunc()).Do(ctx)
if err != nil {
cancel()
return nil, err
}
if !createIndex.Acknowledged {
cancel()
return nil, ErrCannotCreateIndex
}
}
return &ElasticHook{
client: client,
host: host,
index: indexFunc,
levels: levels,
ctx: ctx,
ctxCancel: cancel,
fireFunc: fireFunc,
}, nil
}
// Fire is required to implement
// Logrus hook
func (hook *ElasticHook) Fire(entry *logrus.Entry) error {
return hook.fireFunc(entry, hook)
}
func asyncFireFunc(entry *logrus.Entry, hook *ElasticHook) error {
go syncFireFunc(entry, hook)
return nil
}
func createMessage(entry *logrus.Entry, hook *ElasticHook) *message {
level := entry.Level.String()
if e, ok := entry.Data[logrus.ErrorKey]; ok && e != nil {
if err, ok := e.(error); ok {
entry.Data[logrus.ErrorKey] = err.Error()
}
}
var file string
var function string
var line string
if entry.HasCaller() {
file = entry.Caller.File
//function = entry.Caller.Function
function = fmt.Sprintf("%s()", entry.Caller.Function)
filename := path.Base(entry.Caller.File)
line = fmt.Sprintf("%s:%d", filename, entry.Caller.Line)
}
return &message{
hook.host,
entry.Time.UTC().Format(time.RFC3339Nano),
file,
function,
line,
entry.Message,
entry.Data,
strings.ToUpper(level),
}
}
func syncFireFunc(entry *logrus.Entry, hook *ElasticHook) error {
_, err := hook.client.
Index().
Index(hook.index()).
BodyJson(*createMessage(entry, hook)).
Do(hook.ctx)
return err
}
// Create closure with bulk processor tied to fireFunc.
func makeBulkFireFunc(client *elastic.Client) (fireFunc, error) {
processor, err := client.BulkProcessor().
Name("elogrus.v3.bulk.processor").
Workers(2).
FlushInterval(time.Second).
Do(context.Background())
return func(entry *logrus.Entry, hook *ElasticHook) error {
r := elastic.NewBulkIndexRequest().
Index(hook.index()).
Doc(*createMessage(entry, hook))
processor.Add(r)
return nil
}, err
}
// Levels Required for logrus hook implementation
func (hook *ElasticHook) Levels() []logrus.Level {
return hook.levels
}
// Cancel all calls to elastic
func (hook *ElasticHook) Cancel() {
hook.ctxCancel()
}