Don't call panic() and os.Exit(1) on .WithLevel() (#110)

* Don't call panic() and os.Exit(1) on .WithLevel()
* Explain behavior of WithLevel(), compared to Panic() & Fatal()
This commit is contained in:
Vojtech Vitek 2018-09-27 21:11:43 -04:00 committed by Olivier Poitrey
parent 20ad1708e7
commit 8e36cbf881
1 changed files with 8 additions and 6 deletions

14
log.go
View File

@ -292,22 +292,24 @@ func (l *Logger) Error() *Event {
} }
// Fatal starts a new message with fatal level. The os.Exit(1) function // Fatal starts a new message with fatal level. The os.Exit(1) function
// is called by the Msg method. // is called by the Msg method, which terminates the program immediately.
// //
// You must call Msg on the returned event in order to send the event. // You must call Msg on the returned event in order to send the event.
func (l *Logger) Fatal() *Event { func (l *Logger) Fatal() *Event {
return l.newEvent(FatalLevel, func(msg string) { os.Exit(1) }) return l.newEvent(FatalLevel, func(msg string) { os.Exit(1) })
} }
// Panic starts a new message with panic level. The message is also sent // Panic starts a new message with panic level. The panic() function
// to the panic function. // is called by the Msg method, which stops the ordinary flow of a goroutine.
// //
// You must call Msg on the returned event in order to send the event. // You must call Msg on the returned event in order to send the event.
func (l *Logger) Panic() *Event { func (l *Logger) Panic() *Event {
return l.newEvent(PanicLevel, func(msg string) { panic(msg) }) return l.newEvent(PanicLevel, func(msg string) { panic(msg) })
} }
// WithLevel starts a new message with level. // WithLevel starts a new message with level. Unlike Fatal and Panic
// methods, WithLevel does not terminate the program or stop the ordinary
// flow of a gourotine when used with their respective levels.
// //
// You must call Msg on the returned event in order to send the event. // You must call Msg on the returned event in order to send the event.
func (l *Logger) WithLevel(level Level) *Event { func (l *Logger) WithLevel(level Level) *Event {
@ -321,9 +323,9 @@ func (l *Logger) WithLevel(level Level) *Event {
case ErrorLevel: case ErrorLevel:
return l.Error() return l.Error()
case FatalLevel: case FatalLevel:
return l.Fatal() return l.newEvent(FatalLevel, nil)
case PanicLevel: case PanicLevel:
return l.Panic() return l.newEvent(PanicLevel, nil)
case NoLevel: case NoLevel:
return l.Log() return l.Log()
case Disabled: case Disabled: