Add option to panic in `test.NewNullLogger` to allow testing of

calls to `Fatal*`

See #813
This commit is contained in:
Albert Salim 2018-10-06 18:08:19 +08:00
parent 1ed61965b9
commit 2be620216a
5 changed files with 35 additions and 7 deletions

View File

@ -45,6 +45,8 @@ func runHandlers() {
}
}
type exitFunc func(int)
// Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code)
func Exit(code int) {
runHandlers()

View File

@ -198,7 +198,7 @@ func (entry *Entry) Fatal(args ...interface{}) {
if entry.Logger.IsLevelEnabled(FatalLevel) {
entry.log(FatalLevel, fmt.Sprint(args...))
}
Exit(1)
entry.Logger.Exit(1)
}
func (entry *Entry) Panic(args ...interface{}) {
@ -246,7 +246,7 @@ func (entry *Entry) Fatalf(format string, args ...interface{}) {
if entry.Logger.IsLevelEnabled(FatalLevel) {
entry.Fatal(fmt.Sprintf(format, args...))
}
Exit(1)
entry.Logger.Exit(1)
}
func (entry *Entry) Panicf(format string, args ...interface{}) {
@ -293,7 +293,7 @@ func (entry *Entry) Fatalln(args ...interface{}) {
if entry.Logger.IsLevelEnabled(FatalLevel) {
entry.Fatal(entry.sprintlnn(args...))
}
Exit(1)
entry.Logger.Exit(1)
}
func (entry *Entry) Panicln(args ...interface{}) {

View File

@ -39,12 +39,24 @@ 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() (*logrus.Logger, *Hook) {
func NewNullLogger(options ...TestOption) (*logrus.Logger, *Hook) {
logger := logrus.New()
logger.Out = ioutil.Discard
for _, option := range options {
option(logger)
}
return logger, NewLocal(logger)
}

View File

@ -71,3 +71,14 @@ func TestLoggingWithHooksRace(t *testing.T) {
entries := hook.AllEntries()
assert.Equal(100, len(entries))
}
func TestFatalWithPanic(t *testing.T) {
assert := assert.New(t)
logger, hook := NewNullLogger(FatalPanics)
assert.Nil(hook.LastEntry())
assert.Equal(0, len(hook.Entries))
assert.Panics(func() { logger.Fatal("something went wrong") })
}

View File

@ -32,6 +32,8 @@ type Logger struct {
mu MutexWrap
// Reusable empty entry
entryPool sync.Pool
// Function to exit the application, defaults to `Exit()`
Exit exitFunc
}
type MutexWrap struct {
@ -73,6 +75,7 @@ func New() *Logger {
Formatter: new(TextFormatter),
Hooks: make(LevelHooks),
Level: InfoLevel,
Exit: Exit,
}
}
@ -173,7 +176,7 @@ func (logger *Logger) Fatalf(format string, args ...interface{}) {
entry.Fatalf(format, args...)
logger.releaseEntry(entry)
}
Exit(1)
logger.Exit(1)
}
func (logger *Logger) Panicf(format string, args ...interface{}) {
@ -236,7 +239,7 @@ func (logger *Logger) Fatal(args ...interface{}) {
entry.Fatal(args...)
logger.releaseEntry(entry)
}
Exit(1)
logger.Exit(1)
}
func (logger *Logger) Panic(args ...interface{}) {
@ -299,7 +302,7 @@ func (logger *Logger) Fatalln(args ...interface{}) {
entry.Fatalln(args...)
logger.releaseEntry(entry)
}
Exit(1)
logger.Exit(1)
}
func (logger *Logger) Panicln(args ...interface{}) {