forked from mirror/logrus
Merge branch 'hooks_replace' of git://github.com/betrok/logrus into betrok-hooks_replace
This commit is contained in:
commit
b24eae79a4
|
@ -351,3 +351,12 @@ func (logger *Logger) SetOutput(output io.Writer) {
|
||||||
defer logger.mu.Unlock()
|
defer logger.mu.Unlock()
|
||||||
logger.Out = output
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package logrus
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -421,6 +422,27 @@ func TestLoggingRaceWithHooksOnEntry(t *testing.T) {
|
||||||
wg.Wait()
|
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
|
// Compile test
|
||||||
func TestLogrusInterface(t *testing.T) {
|
func TestLogrusInterface(t *testing.T) {
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
|
|
Loading…
Reference in New Issue