add fatalhandler to logger, called to handle a fatal log message
This commit is contained in:
parent
c67c3e666c
commit
15b1dffa32
@ -87,11 +87,12 @@ func (f *LogFormat) AppendTimestamp(format string) *LogFormat {
|
||||
}
|
||||
|
||||
type ELog struct {
|
||||
parent *ELog
|
||||
name string
|
||||
Level LogLevel
|
||||
Format *LogFormat
|
||||
Output io.Writer
|
||||
parent *ELog
|
||||
name string
|
||||
Level LogLevel
|
||||
Format *LogFormat
|
||||
Output io.Writer
|
||||
FatalHandler func(*ELog, int, string, ...any)
|
||||
}
|
||||
|
||||
func NewRootLogger() *ELog {
|
||||
@ -110,11 +111,17 @@ func NewRootLogger() *ELog {
|
||||
)
|
||||
}
|
||||
|
||||
func FatalHandler(logger *ELog, exitCode int, message string, a ...any) {
|
||||
_, _ = logger.Log(LOGLEVEL_FATAL, message, a)
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
func NewRootLoggerWithFormat(format *LogFormat) *ELog {
|
||||
return &ELog{
|
||||
name: "main",
|
||||
Format: format,
|
||||
Output: os.Stderr,
|
||||
name: "main",
|
||||
Format: format,
|
||||
Output: os.Stderr,
|
||||
FatalHandler: FatalHandler,
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,7 +133,7 @@ func (e *ELog) GetLogger(name string) *ELog {
|
||||
}
|
||||
}
|
||||
|
||||
func (e *ELog) _Log(level LogLevel, message string, a ...any) (n int, err error) {
|
||||
func (e *ELog) Log(level LogLevel, message string, a ...any) (n int, err error) {
|
||||
return e._DoLog(level, nil, message, a...)
|
||||
}
|
||||
|
||||
@ -169,23 +176,23 @@ func (e *ELog) _DoLog(level LogLevel, path []string, message string, a ...any) (
|
||||
}
|
||||
|
||||
func (e *ELog) Debug(message string, a ...any) (n int, err error) {
|
||||
return e._Log(LOGLEVEL_DEBUG, message, a...)
|
||||
return e.Log(LOGLEVEL_DEBUG, message, a...)
|
||||
}
|
||||
|
||||
func (e *ELog) Info(message string, a ...any) (n int, err error) {
|
||||
return e._Log(LOGLEVEL_INFO, message, a...)
|
||||
return e.Log(LOGLEVEL_INFO, message, a...)
|
||||
}
|
||||
|
||||
func (e *ELog) Warning(message string, a ...any) (n int, err error) {
|
||||
return e._Log(LOGLEVEL_WARNING, message, a...)
|
||||
return e.Log(LOGLEVEL_WARNING, message, a...)
|
||||
}
|
||||
|
||||
func (e *ELog) Error(message string, a ...any) (n int, err error) {
|
||||
return e._Log(LOGLEVEL_ERROR, message, a...)
|
||||
return e.Log(LOGLEVEL_ERROR, message, a...)
|
||||
}
|
||||
|
||||
func (e *ELog) Critical(message string, a ...any) (n int, err error) {
|
||||
return e._Log(LOGLEVEL_CRITICAL, message, a...)
|
||||
return e.Log(LOGLEVEL_CRITICAL, message, a...)
|
||||
}
|
||||
|
||||
func (e *ELog) Fatal(message string, a ...any) (n int, err error) {
|
||||
@ -193,8 +200,7 @@ func (e *ELog) Fatal(message string, a ...any) (n int, err error) {
|
||||
}
|
||||
|
||||
func (e *ELog) Fatalc(exitCode int, message string, a ...any) (n int, err error) {
|
||||
_, _ = e._Log(LOGLEVEL_FATAL, message, a...)
|
||||
os.Exit(exitCode)
|
||||
e.FatalHandler(e, exitCode, message, a...)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user