Skip to content

Commit 7275750

Browse files
macratevalphobia
authored andcommitted
Fixed bug that some default values of Config doesn't work (#27)
1 parent f27fe73 commit 7275750

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

fluent.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ func NewWithConfig(conf Config) (*FluentHook, error) {
6767
}
6868

6969
hook := &FluentHook{
70-
Fluent: fd,
71-
conf: conf,
72-
levels: conf.LogLevels,
70+
Fluent: fd,
71+
conf: conf,
72+
levels: conf.LogLevels,
73+
ignoreFields: make(map[string]struct{}),
74+
filters: make(map[string]func(interface{}) interface{}),
7375
}
7476
// set default values
7577
if len(hook.levels) == 0 {
@@ -82,12 +84,13 @@ func NewWithConfig(conf Config) (*FluentHook, error) {
8284
if conf.DefaultMessageField != "" {
8385
hook.messageField = conf.DefaultMessageField
8486
}
85-
if hook.ignoreFields == nil {
86-
hook.ignoreFields = make(map[string]struct{})
87+
for k, v := range conf.DefaultIgnoreFields {
88+
hook.ignoreFields[k] = v
8789
}
88-
if hook.filters == nil {
89-
hook.filters = make(map[string]func(interface{}) interface{})
90+
for k, v := range conf.DefaultFilters {
91+
hook.filters[k] = v
9092
}
93+
9194
return hook, nil
9295
}
9396

fluent_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func TestNewWithConfig(t *testing.T) {
6666
Host: testHOST,
6767
Port: port,
6868
DefaultMessageField: "DefaultMessageField",
69+
DefaultIgnoreFields: map[string]struct{}{"ignored": {}},
70+
DefaultFilters: map[string]func(interface{}) interface{}{
71+
"filtered": func(x interface{}) interface{} {
72+
return x
73+
},
74+
},
6975
}
7076
hook, err := NewWithConfig(conf)
7177
switch {
@@ -83,6 +89,10 @@ func TestNewWithConfig(t *testing.T) {
8389
t.Errorf("hook.Fluent should not be nil")
8490
case hook.messageField != "DefaultMessageField":
8591
t.Errorf("hook.messageField should be DefaultMessageField")
92+
case len(hook.ignoreFields) != len(conf.DefaultIgnoreFields):
93+
t.Errorf("hook.ignoreFields should be same as conf.DefaultIgnoreFields")
94+
case len(hook.filters) != len(conf.DefaultFilters):
95+
t.Errorf("hook.filters should be same as conf.DefaultFilters")
8696
}
8797
}
8898

0 commit comments

Comments
 (0)