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:
Albert Salim
2018-10-10 21:54:15 +08:00
parent 2be620216a
commit 99bc300c8d
5 changed files with 26 additions and 25 deletions
+11 -3
View File
@@ -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.