Skip to content

Commit

Permalink
Fixed bug that some default values of Config doesn't work (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat authored and evalphobia committed Aug 28, 2018
1 parent f27fe73 commit 7275750
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 10 additions & 7 deletions fluent.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ func NewWithConfig(conf Config) (*FluentHook, error) {
}

hook := &FluentHook{
Fluent: fd,
conf: conf,
levels: conf.LogLevels,
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 {
Expand All @@ -82,12 +84,13 @@ func NewWithConfig(conf Config) (*FluentHook, error) {
if conf.DefaultMessageField != "" {
hook.messageField = conf.DefaultMessageField
}
if hook.ignoreFields == nil {
hook.ignoreFields = make(map[string]struct{})
for k, v := range conf.DefaultIgnoreFields {
hook.ignoreFields[k] = v
}
if hook.filters == nil {
hook.filters = make(map[string]func(interface{}) interface{})
for k, v := range conf.DefaultFilters {
hook.filters[k] = v
}

return hook, nil
}

Expand Down
10 changes: 10 additions & 0 deletions fluent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func TestNewWithConfig(t *testing.T) {
Host: testHOST,
Port: port,
DefaultMessageField: "DefaultMessageField",
DefaultIgnoreFields: map[string]struct{}{"ignored": {}},
DefaultFilters: map[string]func(interface{}) interface{}{
"filtered": func(x interface{}) interface{} {
return x
},
},
}
hook, err := NewWithConfig(conf)
switch {
Expand All @@ -83,6 +89,10 @@ func TestNewWithConfig(t *testing.T) {
t.Errorf("hook.Fluent should not be nil")
case hook.messageField != "DefaultMessageField":
t.Errorf("hook.messageField should be DefaultMessageField")
case len(hook.ignoreFields) != len(conf.DefaultIgnoreFields):
t.Errorf("hook.ignoreFields should be same as conf.DefaultIgnoreFields")
case len(hook.filters) != len(conf.DefaultFilters):
t.Errorf("hook.filters should be same as conf.DefaultFilters")
}
}

Expand Down

0 comments on commit 7275750

Please sign in to comment.