Merge branch 'hooks_replace' of git://github.com/betrok/logrus into betrok-hooks_replace

This commit is contained in:
David Bariod 2018-08-26 20:44:53 +02:00
commit b24eae79a4
2 changed files with 31 additions and 0 deletions

View File

@ -351,3 +351,12 @@ func (logger *Logger) SetOutput(output io.Writer) {
defer logger.mu.Unlock()
logger.Out = output
}
// ReplaceHooks replaces the logger hooks and returns the old ones
func (logger *Logger) ReplaceHooks(hooks LevelHooks) LevelHooks {
logger.mu.Lock()
oldHooks := logger.Hooks
logger.Hooks = hooks
logger.mu.Unlock()
return oldHooks
}

View File

@ -3,6 +3,7 @@ package logrus
import (
"bytes"
"encoding/json"
"io/ioutil"
"strconv"
"strings"
"sync"
@ -421,6 +422,27 @@ func TestLoggingRaceWithHooksOnEntry(t *testing.T) {
wg.Wait()
}
func TestReplaceHooks(t *testing.T) {
old, cur := &TestHook{}, &TestHook{}
logger := New()
logger.SetOutput(ioutil.Discard)
logger.AddHook(old)
hooks := make(LevelHooks)
hooks.Add(cur)
replaced := logger.ReplaceHooks(hooks)
logger.Info("test")
assert.Equal(t, old.Fired, false)
assert.Equal(t, cur.Fired, true)
logger.ReplaceHooks(replaced)
logger.Info("test")
assert.Equal(t, old.Fired, true)
}
// Compile test
func TestLogrusInterface(t *testing.T) {
var buffer bytes.Buffer