mirror of https://github.com/sirupsen/logrus.git
Add option to panic in `test.NewNullLogger` to allow testing of
calls to `Fatal*` See #813
This commit is contained in:
parent
1ed61965b9
commit
2be620216a
|
@ -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)
|
// Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code)
|
||||||
func Exit(code int) {
|
func Exit(code int) {
|
||||||
runHandlers()
|
runHandlers()
|
||||||
|
|
6
entry.go
6
entry.go
|
@ -198,7 +198,7 @@ func (entry *Entry) Fatal(args ...interface{}) {
|
||||||
if entry.Logger.IsLevelEnabled(FatalLevel) {
|
if entry.Logger.IsLevelEnabled(FatalLevel) {
|
||||||
entry.log(FatalLevel, fmt.Sprint(args...))
|
entry.log(FatalLevel, fmt.Sprint(args...))
|
||||||
}
|
}
|
||||||
Exit(1)
|
entry.Logger.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Panic(args ...interface{}) {
|
func (entry *Entry) Panic(args ...interface{}) {
|
||||||
|
@ -246,7 +246,7 @@ func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
||||||
if entry.Logger.IsLevelEnabled(FatalLevel) {
|
if entry.Logger.IsLevelEnabled(FatalLevel) {
|
||||||
entry.Fatal(fmt.Sprintf(format, args...))
|
entry.Fatal(fmt.Sprintf(format, args...))
|
||||||
}
|
}
|
||||||
Exit(1)
|
entry.Logger.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Panicf(format string, args ...interface{}) {
|
func (entry *Entry) Panicf(format string, args ...interface{}) {
|
||||||
|
@ -293,7 +293,7 @@ func (entry *Entry) Fatalln(args ...interface{}) {
|
||||||
if entry.Logger.IsLevelEnabled(FatalLevel) {
|
if entry.Logger.IsLevelEnabled(FatalLevel) {
|
||||||
entry.Fatal(entry.sprintlnn(args...))
|
entry.Fatal(entry.sprintlnn(args...))
|
||||||
}
|
}
|
||||||
Exit(1)
|
entry.Logger.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (entry *Entry) Panicln(args ...interface{}) {
|
func (entry *Entry) Panicln(args ...interface{}) {
|
||||||
|
|
|
@ -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.
|
// 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 := logrus.New()
|
||||||
logger.Out = ioutil.Discard
|
logger.Out = ioutil.Discard
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(logger)
|
||||||
|
}
|
||||||
|
|
||||||
return logger, NewLocal(logger)
|
return logger, NewLocal(logger)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,3 +71,14 @@ func TestLoggingWithHooksRace(t *testing.T) {
|
||||||
entries := hook.AllEntries()
|
entries := hook.AllEntries()
|
||||||
assert.Equal(100, len(entries))
|
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") })
|
||||||
|
}
|
||||||
|
|
|
@ -32,6 +32,8 @@ type Logger struct {
|
||||||
mu MutexWrap
|
mu MutexWrap
|
||||||
// Reusable empty entry
|
// Reusable empty entry
|
||||||
entryPool sync.Pool
|
entryPool sync.Pool
|
||||||
|
// Function to exit the application, defaults to `Exit()`
|
||||||
|
Exit exitFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
type MutexWrap struct {
|
type MutexWrap struct {
|
||||||
|
@ -73,6 +75,7 @@ func New() *Logger {
|
||||||
Formatter: new(TextFormatter),
|
Formatter: new(TextFormatter),
|
||||||
Hooks: make(LevelHooks),
|
Hooks: make(LevelHooks),
|
||||||
Level: InfoLevel,
|
Level: InfoLevel,
|
||||||
|
Exit: Exit,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,7 +176,7 @@ func (logger *Logger) Fatalf(format string, args ...interface{}) {
|
||||||
entry.Fatalf(format, args...)
|
entry.Fatalf(format, args...)
|
||||||
logger.releaseEntry(entry)
|
logger.releaseEntry(entry)
|
||||||
}
|
}
|
||||||
Exit(1)
|
logger.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) Panicf(format string, args ...interface{}) {
|
func (logger *Logger) Panicf(format string, args ...interface{}) {
|
||||||
|
@ -236,7 +239,7 @@ func (logger *Logger) Fatal(args ...interface{}) {
|
||||||
entry.Fatal(args...)
|
entry.Fatal(args...)
|
||||||
logger.releaseEntry(entry)
|
logger.releaseEntry(entry)
|
||||||
}
|
}
|
||||||
Exit(1)
|
logger.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) Panic(args ...interface{}) {
|
func (logger *Logger) Panic(args ...interface{}) {
|
||||||
|
@ -299,7 +302,7 @@ func (logger *Logger) Fatalln(args ...interface{}) {
|
||||||
entry.Fatalln(args...)
|
entry.Fatalln(args...)
|
||||||
logger.releaseEntry(entry)
|
logger.releaseEntry(entry)
|
||||||
}
|
}
|
||||||
Exit(1)
|
logger.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) Panicln(args ...interface{}) {
|
func (logger *Logger) Panicln(args ...interface{}) {
|
||||||
|
|
Loading…
Reference in New Issue