diff --git a/gelf.go b/gelf.go index 20b3b72..b22dd93 100644 --- a/gelf.go +++ b/gelf.go @@ -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 { diff --git a/gelf_test.go b/gelf_test.go index 71e2b96..397284d 100644 --- a/gelf_test.go +++ b/gelf_test.go @@ -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) {