|
|
@ -20,7 +20,6 @@ type entry = Entry
|
|
|
|
// Logger 中保存了日志所需的全局配置,
|
|
|
|
// Logger 中保存了日志所需的全局配置,
|
|
|
|
// 使用 Logger 处理日志.
|
|
|
|
// 使用 Logger 处理日志.
|
|
|
|
type Logger struct {
|
|
|
|
type Logger struct {
|
|
|
|
exit func(code int) // protected by lock
|
|
|
|
|
|
|
|
beforeExitFns []func() // protected by lock
|
|
|
|
beforeExitFns []func() // protected by lock
|
|
|
|
levelProcessors levelProcessors // protected by lock
|
|
|
|
levelProcessors levelProcessors // protected by lock
|
|
|
|
entry
|
|
|
|
entry
|
|
|
@ -99,7 +98,6 @@ func (logger *Logger) SetReportStackLevel(level Level) {
|
|
|
|
// Reset 把 Logger 重置到初始状态
|
|
|
|
// Reset 把 Logger 重置到初始状态
|
|
|
|
func (logger *Logger) Reset() {
|
|
|
|
func (logger *Logger) Reset() {
|
|
|
|
logger.lock.Lock()
|
|
|
|
logger.lock.Lock()
|
|
|
|
logger.exit = nil
|
|
|
|
|
|
|
|
logger.beforeExitFns = nil
|
|
|
|
logger.beforeExitFns = nil
|
|
|
|
logger.levelProcessors = levelProcessors{}
|
|
|
|
logger.levelProcessors = levelProcessors{}
|
|
|
|
logger.lock.Unlock()
|
|
|
|
logger.lock.Unlock()
|
|
|
@ -125,27 +123,13 @@ func (logger *Logger) BeforeExit() {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Exit 执行 BeforeExit 后退出程序, 执行的退出函数可以通过 SetExit 指定,
|
|
|
|
// Exit 执行 BeforeExit 后调用 [os.Exit]退出程序.
|
|
|
|
// 当没有通过 SetExit 指定退出函数时调用 [os.Exit].
|
|
|
|
|
|
|
|
func (logger *Logger) Exit(code int) {
|
|
|
|
func (logger *Logger) Exit(code int) {
|
|
|
|
logger.BeforeExit()
|
|
|
|
logger.BeforeExit()
|
|
|
|
logger.lock.RLock()
|
|
|
|
os.Exit(code)
|
|
|
|
exit := logger.exit
|
|
|
|
|
|
|
|
logger.lock.RUnlock()
|
|
|
|
|
|
|
|
if exit == nil {
|
|
|
|
|
|
|
|
exit = os.Exit
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(code)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetExit 指定退出程序时执行的函数
|
|
|
|
|
|
|
|
func (logger *Logger) SetExit(fn func(code int)) {
|
|
|
|
|
|
|
|
logger.lock.Lock()
|
|
|
|
|
|
|
|
logger.exit = fn
|
|
|
|
|
|
|
|
logger.lock.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddBeforeExit 增加 Exit 在调用 SetExit 指定的函数前执行的函数,
|
|
|
|
// AddBeforeExit 增加 Exit 在调用 [os.Exit] 前执行的函数,
|
|
|
|
// 先增加的后执行.
|
|
|
|
// 先增加的后执行.
|
|
|
|
func (logger *Logger) AddBeforeExit(fn ...func()) {
|
|
|
|
func (logger *Logger) AddBeforeExit(fn ...func()) {
|
|
|
|
logger.lock.Lock()
|
|
|
|
logger.lock.Lock()
|
|
|
|