-
Notifications
You must be signed in to change notification settings - Fork 22
/
fluent.go
237 lines (204 loc) · 5.68 KB
/
fluent.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
package logrus_fluent
import (
"github.com/fluent/fluent-logger-golang/fluent"
"github.com/sirupsen/logrus"
)
const (
// TagName is struct field tag name.
// Some basic option is allowed in the field tag,
//
// type myStruct {
// Value1: `fluent:"value_1"` // change field name.
// Value2: `fluent:"-"` // always omit this field.
// Value3: `fluent:",omitempty"` // omit this field when zero-value.
// }
TagName = "fluent"
// TagField is logrus field name used as fluentd tag
TagField = "tag"
// MessageField is logrus field name used as message.
// If missing in the log fields, entry.Message is set to this field.
MessageField = "message"
)
var defaultLevels = []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
}
// FluentHook is logrus hook for fluentd.
type FluentHook struct {
// Fluent is actual fluentd logger.
// If set, this logger is used for logging.
// otherwise new logger is created every time.
Fluent *fluent.Fluent
conf Config
levels []logrus.Level
tag *string
messageField string
ignoreFields map[string]struct{}
filters map[string]func(interface{}) interface{}
customizers []func(entry *logrus.Entry, data logrus.Fields)
}
// New returns initialized logrus hook for fluentd with persistent fluentd logger.
func New(host string, port int) (*FluentHook, error) {
return NewWithConfig(Config{
Host: host,
Port: port,
DefaultMessageField: MessageField,
})
}
// NewWithConfig returns initialized logrus hook by config setting.
func NewWithConfig(conf Config) (*FluentHook, error) {
var fd *fluent.Fluent
if !conf.DisableConnectionPool {
var err error
fd, err = fluent.New(conf.FluentConfig())
if err != nil {
return nil, err
}
}
hook := &FluentHook{
Fluent: fd,
conf: conf,
levels: conf.LogLevels,
ignoreFields: make(map[string]struct{}),
filters: make(map[string]func(interface{}) interface{}),
}
// set default values
if len(hook.levels) == 0 {
hook.levels = defaultLevels
}
if conf.DefaultTag != "" {
tag := conf.DefaultTag
hook.tag = &tag
}
if conf.DefaultMessageField != "" {
hook.messageField = conf.DefaultMessageField
}
for k, v := range conf.DefaultIgnoreFields {
hook.ignoreFields[k] = v
}
for k, v := range conf.DefaultFilters {
hook.filters[k] = v
}
return hook, nil
}
// NewHook returns initialized logrus hook for fluentd.
// (** deperecated: use New() or NewWithConfig() **)
func NewHook(host string, port int) *FluentHook {
hook, _ := NewWithConfig(Config{
Host: host,
Port: port,
DefaultMessageField: MessageField,
DisableConnectionPool: true,
})
return hook
}
// Levels returns logging level to fire this hook.
func (hook *FluentHook) Levels() []logrus.Level {
return hook.levels
}
// SetLevels sets logging level to fire this hook.
func (hook *FluentHook) SetLevels(levels []logrus.Level) {
hook.levels = levels
}
// Tag returns custom static tag.
func (hook *FluentHook) Tag() string {
if hook.tag == nil {
return ""
}
return *hook.tag
}
// SetTag sets custom static tag to override tag in the message fields.
func (hook *FluentHook) SetTag(tag string) {
hook.tag = &tag
}
// SetMessageField sets custom message field.
func (hook *FluentHook) SetMessageField(messageField string) {
hook.messageField = messageField
}
// AddIgnore adds field name to ignore.
func (hook *FluentHook) AddIgnore(name string) {
hook.ignoreFields[name] = struct{}{}
}
// AddFilter adds a custom filter function.
func (hook *FluentHook) AddFilter(name string, fn func(interface{}) interface{}) {
hook.filters[name] = fn
}
// AddCustomizer adds a custom function to modify data.
func (hook *FluentHook) AddCustomizer(fn func(entry *logrus.Entry, data logrus.Fields)) {
hook.customizers = append(hook.customizers, fn)
}
// Fire is invoked by logrus and sends log to fluentd logger.
func (hook *FluentHook) Fire(entry *logrus.Entry) error {
var logger *fluent.Fluent
var err error
switch {
case hook.Fluent != nil:
logger = hook.Fluent
default:
logger, err = fluent.New(hook.conf.FluentConfig())
if err != nil {
return err
}
defer logger.Close()
}
// Create a map for passing to FluentD
data := make(logrus.Fields)
for k, v := range entry.Data {
if _, ok := hook.ignoreFields[k]; ok {
continue
}
if fn, ok := hook.filters[k]; ok {
v = fn(v)
}
data[k] = v
}
setLevelString(entry, data)
tag := hook.getTagAndDel(entry, data)
if tag != entry.Message {
hook.setMessage(entry, data)
}
// modify data to your own needs.
for _, fn := range hook.customizers {
fn(entry, data)
}
fluentData := ConvertToValue(data, TagName)
err = logger.PostWithTime(tag, entry.Time, fluentData)
return err
}
// getTagAndDel extracts tag data from log entry and custom log fields.
// 1. if tag is set in the hook, use it.
// 2. if tag is set in custom fields, use it.
// 3. if cannot find tag data, use entry.Message as tag.
func (hook *FluentHook) getTagAndDel(entry *logrus.Entry, data logrus.Fields) string {
// use static tag from
if hook.tag != nil {
return *hook.tag
}
tagField, ok := data[TagField]
if !ok {
return entry.Message
}
tag, ok := tagField.(string)
if !ok {
return entry.Message
}
// remove tag from data fields
delete(data, TagField)
return tag
}
func (hook *FluentHook) setMessage(entry *logrus.Entry, data logrus.Fields) {
if _, ok := data[hook.messageField]; ok {
return
}
var v interface{} = entry.Message
if fn, ok := hook.filters[hook.messageField]; ok {
v = fn(v)
}
data[hook.messageField] = v
}
func setLevelString(entry *logrus.Entry, data logrus.Fields) {
data["level"] = entry.Level.String()
}