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:
@@ -32,8 +32,8 @@ type Logger struct {
|
||||
mu MutexWrap
|
||||
// Reusable empty entry
|
||||
entryPool sync.Pool
|
||||
// Function to exit the application, defaults to `Exit()`
|
||||
Exit exitFunc
|
||||
// Function to exit the application, defaults to `osExit()`
|
||||
ExitFunc exitFunc
|
||||
}
|
||||
|
||||
type MutexWrap struct {
|
||||
@@ -75,7 +75,7 @@ func New() *Logger {
|
||||
Formatter: new(TextFormatter),
|
||||
Hooks: make(LevelHooks),
|
||||
Level: InfoLevel,
|
||||
Exit: Exit,
|
||||
ExitFunc: osExit,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,6 +313,14 @@ func (logger *Logger) Panicln(args ...interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Exit(code int) {
|
||||
runHandlers()
|
||||
if logger.ExitFunc == nil {
|
||||
logger.ExitFunc = osExit
|
||||
}
|
||||
logger.ExitFunc(code)
|
||||
}
|
||||
|
||||
//When file is opened with appending mode, it's safe to
|
||||
//write concurrently to a file (within 4k message on Linux).
|
||||
//In these cases user can choose to disable the lock.
|
||||
|
||||
Reference in New Issue
Block a user