logger.ReplaceHooks

This commit is contained in:
betrok 2018-08-22 12:10:05 +03:00
parent fc587f31c8
commit 7a0120e2c6
2 changed files with 22 additions and 0 deletions

View File

@ -336,3 +336,9 @@ func (logger *Logger) AddHook(hook Hook) {
defer logger.mu.Unlock()
logger.Hooks.Add(hook)
}
func (logger *Logger) ReplaceHooks(hooks LevelHooks) {
logger.mu.Lock()
logger.Hooks = hooks
logger.mu.Unlock()
}

View File

@ -421,6 +421,22 @@ func TestLoggingRaceWithHooksOnEntry(t *testing.T) {
wg.Wait()
}
func TestHooksReplace(t *testing.T) {
old, cur := &TestHook{}, &TestHook{}
logger := New()
logger.AddHook(old)
hooks := make(LevelHooks)
hooks.Add(cur)
logger.ReplaceHooks(hooks)
logger.Info("test")
assert.Equal(t, old.Fired, false)
assert.Equal(t, cur.Fired, true)
}
// Compile test
func TestLogrusInterface(t *testing.T) {
var buffer bytes.Buffer