Skip to content

Commit

Permalink
Merge pull request #6 from b-markowski/dynamic_log_level_support
Browse files Browse the repository at this point in the history
Add configuration option for zap.NewAtomicLevel() in order to change level dynamically.
  • Loading branch information
snovichkov authored Feb 20, 2023
2 parents f5dd0b1 + e6a343d commit 34e1242
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions gelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ func TimeKey(value string) Option {
})
}

// LevelAtomic set atomic logging level which can be changed dynamically.
func LevelAtomic(level zap.AtomicLevel) Option {
return optionFunc(func(conf *optionConf) (err error) {
conf.enabler = level
return nil
})
}

// NameKey set zapcore.EncoderConfig NameKey property.
func NameKey(value string) Option {
return optionFunc(func(conf *optionConf) error {
Expand Down
27 changes: 25 additions & 2 deletions gelf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,43 @@ func TestNewReflectedEncoder(t *testing.T) {

func TestLevel(t *testing.T) {
var core, err = gelf.NewCore(
gelf.Level(zap.DebugLevel),
gelf.Level(zap.ErrorLevel),
)

assert.Nil(t, err, "Unexpected error")
assert.Implements(t, (*zapcore.Core)(nil), core, "Expect zapcore.Core")
assert.True(t, core.Enabled(zap.ErrorLevel))
assert.False(t, core.Enabled(zap.WarnLevel))
}

func TestLevelString(t *testing.T) {
var core, err = gelf.NewCore(
gelf.LevelString("debug"),
gelf.LevelString("error"),
)

assert.Nil(t, err, "Unexpected error")
assert.Implements(t, (*zapcore.Core)(nil), core, "Expect zapcore.Core")
assert.True(t, core.Enabled(zap.ErrorLevel))
assert.False(t, core.Enabled(zap.WarnLevel))
}

func TestLevelAtomic(t *testing.T) {
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(zap.ErrorLevel)

var core, err = gelf.NewCore(
gelf.LevelAtomic(atomicLevel),
)

assert.Nil(t, err, "Unexpected error")
assert.Implements(t, (*zapcore.Core)(nil), core, "Expect zapcore.Core")
assert.True(t, core.Enabled(zap.ErrorLevel))
assert.False(t, core.Enabled(zap.WarnLevel))

atomicLevel.SetLevel(zap.WarnLevel)

assert.True(t, core.Enabled(zap.ErrorLevel))
assert.True(t, core.Enabled(zap.WarnLevel))
}

func TestChunkSize(t *testing.T) {
Expand Down

0 comments on commit 34e1242

Please sign in to comment.