log/扩展Exit功能;
This commit is contained in:
@@ -54,6 +54,7 @@ log.Logger()
|
|||||||
- `SetReportStack` 设置生成调用栈;
|
- `SetReportStack` 设置生成调用栈;
|
||||||
- `SetReportStackLevel` 当日志等级小于设定值时强制生成调用栈;
|
- `SetReportStackLevel` 当日志等级小于设定值时强制生成调用栈;
|
||||||
- `Reset` 把 Logger 恢复到初始状态;
|
- `Reset` 把 Logger 恢复到初始状态;
|
||||||
|
- `AddBeforeExit` 新增调用 `Logger.Exit` 时优先执行的函数, 先增加的后执行;
|
||||||
- `SetExit` 设置 `Logger` 的退出函数(`Logger.Exit`), 当日志等级为 `LevelFatal` 时调用这个函数;
|
- `SetExit` 设置 `Logger` 的退出函数(`Logger.Exit`), 当日志等级为 `LevelFatal` 时调用这个函数;
|
||||||
|
|
||||||
### 日志生成
|
### 日志生成
|
||||||
|
|||||||
@@ -150,6 +150,11 @@ func Panicf(ctx context.Context, format string, args ...any) {
|
|||||||
globalLogger.AddCallerSkip(1).Panicf(ctx, format, args...)
|
globalLogger.AddCallerSkip(1).Panicf(ctx, format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exit 调用全局 logger 的 Exit
|
||||||
|
func Exit(code int) {
|
||||||
|
globalLogger.Exit(code)
|
||||||
|
}
|
||||||
|
|
||||||
// Logger 返回全局 logger, 通常用来在程序启动时对全局 logger 进行配置,
|
// Logger 返回全局 logger, 通常用来在程序启动时对全局 logger 进行配置,
|
||||||
// 业务代码处理日志时直接使用这个包里定义的日志函数.
|
// 业务代码处理日志时直接使用这个包里定义的日志函数.
|
||||||
func Logger() *logsdk.Logger {
|
func Logger() *logsdk.Logger {
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
Reference in New Issue
Block a user