Add a method Exit on Logger that calls os.Exit or alternate exit function.
This keeps backward compatibility for static declaration of logger that does not specify `ExitFunc` field.
This commit is contained in:
+1
-13
@@ -39,24 +39,12 @@ func NewLocal(logger *logrus.Logger) *Hook {
|
||||
|
||||
}
|
||||
|
||||
type TestOption func(logger *logrus.Logger)
|
||||
|
||||
func FatalPanics(logger *logrus.Logger) {
|
||||
logger.Exit = func(code int) {
|
||||
panic(code)
|
||||
}
|
||||
}
|
||||
|
||||
// NewNullLogger creates a discarding logger and installs the test hook.
|
||||
func NewNullLogger(options ...TestOption) (*logrus.Logger, *Hook) {
|
||||
func NewNullLogger() (*logrus.Logger, *Hook) {
|
||||
|
||||
logger := logrus.New()
|
||||
logger.Out = ioutil.Discard
|
||||
|
||||
for _, option := range options {
|
||||
option(logger)
|
||||
}
|
||||
|
||||
return logger, NewLocal(logger)
|
||||
|
||||
}
|
||||
|
||||
@@ -72,13 +72,14 @@ func TestLoggingWithHooksRace(t *testing.T) {
|
||||
assert.Equal(100, len(entries))
|
||||
}
|
||||
|
||||
func TestFatalWithPanic(t *testing.T) {
|
||||
func TestFatalWithAlternateExit(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
logger, hook := NewNullLogger(FatalPanics)
|
||||
logger, hook := NewNullLogger()
|
||||
logger.ExitFunc = func(code int) {}
|
||||
|
||||
assert.Nil(hook.LastEntry())
|
||||
assert.Equal(0, len(hook.Entries))
|
||||
|
||||
assert.Panics(func() { logger.Fatal("something went wrong") })
|
||||
logger.Fatal("something went very wrong")
|
||||
assert.Equal(logrus.FatalLevel, hook.LastEntry().Level)
|
||||
assert.Equal("something went very wrong", hook.LastEntry().Message)
|
||||
assert.Equal(1, len(hook.Entries))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user