|
|
@ -21,6 +21,7 @@ type entry = Entry
|
|
|
|
// 使用 Logger 处理日志.
|
|
|
|
// 使用 Logger 处理日志.
|
|
|
|
type Logger struct {
|
|
|
|
type Logger struct {
|
|
|
|
exit func(code int) // protected by lock
|
|
|
|
exit func(code int) // protected by lock
|
|
|
|
|
|
|
|
beforeExit []func(code int) // protected by lock
|
|
|
|
levelProcessors levelProcessors // protected by lock
|
|
|
|
levelProcessors levelProcessors // protected by lock
|
|
|
|
entry
|
|
|
|
entry
|
|
|
|
lock sync.RWMutex
|
|
|
|
lock sync.RWMutex
|
|
|
@ -98,6 +99,7 @@ func (logger *Logger) SetReportStackLevel(level Level) {
|
|
|
|
func (logger *Logger) Reset() {
|
|
|
|
func (logger *Logger) Reset() {
|
|
|
|
logger.lock.Lock()
|
|
|
|
logger.lock.Lock()
|
|
|
|
logger.exit = nil
|
|
|
|
logger.exit = nil
|
|
|
|
|
|
|
|
logger.beforeExit = nil
|
|
|
|
logger.levelProcessors = levelProcessors{}
|
|
|
|
logger.levelProcessors = levelProcessors{}
|
|
|
|
logger.lock.Unlock()
|
|
|
|
logger.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
@ -112,9 +114,14 @@ func (logger *Logger) Reset() {
|
|
|
|
func (logger *Logger) Exit(code int) {
|
|
|
|
func (logger *Logger) Exit(code int) {
|
|
|
|
logger.lock.RLock()
|
|
|
|
logger.lock.RLock()
|
|
|
|
exit := logger.exit
|
|
|
|
exit := logger.exit
|
|
|
|
|
|
|
|
beforeExit := make([]func(int), len(logger.beforeExit))
|
|
|
|
|
|
|
|
copy(beforeExit, logger.beforeExit)
|
|
|
|
logger.lock.RUnlock()
|
|
|
|
logger.lock.RUnlock()
|
|
|
|
|
|
|
|
for i := len(beforeExit) - 1; i >= 0; i-- {
|
|
|
|
|
|
|
|
beforeExit[i](code)
|
|
|
|
|
|
|
|
}
|
|
|
|
if exit == nil {
|
|
|
|
if exit == nil {
|
|
|
|
os.Exit(code)
|
|
|
|
exit = os.Exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(code)
|
|
|
|
exit(code)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -126,6 +133,14 @@ func (logger *Logger) SetExit(fn func(code int)) {
|
|
|
|
logger.lock.Unlock()
|
|
|
|
logger.lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AddBeforeExit 增加 Exit 在调用 SetExit 指定的函数前执行的函数,
|
|
|
|
|
|
|
|
// 先增加的后执行.
|
|
|
|
|
|
|
|
func (logger *Logger) AddBeforeExit(fn ...func(code int)) {
|
|
|
|
|
|
|
|
logger.lock.Lock()
|
|
|
|
|
|
|
|
logger.beforeExit = append(logger.beforeExit, fn...)
|
|
|
|
|
|
|
|
logger.lock.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (logger *Logger) getLevelProcessors(level Level) []EntryProcessor {
|
|
|
|
func (logger *Logger) getLevelProcessors(level Level) []EntryProcessor {
|
|
|
|
logger.lock.RLock()
|
|
|
|
logger.lock.RLock()
|
|
|
|
defer logger.lock.RUnlock()
|
|
|
|
defer logger.lock.RUnlock()
|
|
|
|