diff --git a/log/README.md b/log/README.md index 24c5005..3016c8c 100644 --- a/log/README.md +++ b/log/README.md @@ -85,11 +85,13 @@ log.Logger() ### 其他用法 -关闭日志生成 `log.Logger().SetLevel(logsdk.LevelDisabled)` +关闭日志生成, `log.Logger().SetLevel(logsdk.LevelDisabled)`; -关闭强制生成调用栈 `log.Logger().SetReportStackLevel(logsdk.LevelDisabled)` +关闭强制生成调用栈, `log.Logger().SetReportStackLevel(logsdk.LevelDisabled)`; -mock 实现 mock 日志处理器对生成的日志进行处理 +记录 panic, 在 `defer v = recover()` 后执行 `log.Z(context.Background(), v)`, 其中 `Z` 需要是附加调用栈的等级; + +mock, 实现 mock 日志处理器对生成的日志进行处理; --- diff --git a/log/log.go b/log/log.go index 9884674..4ba5e44 100644 --- a/log/log.go +++ b/log/log.go @@ -104,8 +104,8 @@ func Fatal(ctx context.Context, args ...any) { globalLogger.AddCallerSkip(1).Fatal(ctx, args...) } -// Panic 输出 LevelPanic 等级的日志后执行 panic, -// 即使 Logger 日志等级高于 LevelPanic 也会 panic. +// Panic 输出 LevelPanic 等级的日志, +// Panic 不会调用 panic 函数. func Panic(ctx context.Context, args ...any) { globalLogger.AddCallerSkip(1).Panic(ctx, args...) } @@ -145,7 +145,8 @@ func Fatalf(ctx context.Context, format string, args ...any) { globalLogger.AddCallerSkip(1).Fatalf(ctx, format, args...) } -// Panicf 格式化输出 LevelPanic 等级的日志 +// Panicf 格式化输出 LevelPanic 等级的日志, +// Panicf 不会调用 panic 函数. func Panicf(ctx context.Context, format string, args ...any) { globalLogger.AddCallerSkip(1).Panicf(ctx, format, args...) } diff --git a/log/logsdk/entry.go b/log/logsdk/entry.go index 29a280e..47d971d 100644 --- a/log/logsdk/entry.go +++ b/log/logsdk/entry.go @@ -127,8 +127,8 @@ func (entry Entry) Fatal(ctx context.Context, args ...any) { entry.logger.Exit(1) } -// Panic 输出 LevelPanic 等级的日志后执行 panic, -// 即使 Logger 日志等级高于 LevelPanic 也会 panic. +// Panic 输出 LevelPanic 等级的日志, +// Panic 不会调用 panic 函数. func (entry Entry) Panic(ctx context.Context, args ...any) { entry.log(ctx, LevelPanic, fmt.Sprint(args...)) } @@ -169,19 +169,14 @@ func (entry Entry) Fatalf(ctx context.Context, format string, args ...any) { entry.logger.Exit(1) } -// Panicf 格式化输出 LevelPanic 等级的日志 -// 即使 Logger 日志等级高于 LevelPanic 也会 panic. +// Panicf 格式化输出 LevelPanic 等级的日志, +// Panicf 不会调用 panic 函数. func (entry Entry) Panicf(ctx context.Context, format string, args ...any) { entry.log(ctx, LevelPanic, fmt.Sprintf(format, args...)) } func (entry Entry) log(ctx context.Context, level Level, message string) { newEntry := entry.copy() - defer func() { - if level == LevelPanic { - panic(message) - } - }() if newEntry.logger.GetLevel() < level { return } diff --git a/log/logsdk/level.go b/log/logsdk/level.go index ae56306..368d811 100644 --- a/log/logsdk/level.go +++ b/log/logsdk/level.go @@ -64,14 +64,14 @@ const ( // LevelCount 日志等级的个数 LevelCount = 7 - // LevelOffset 是把 LevelInfo 等级的日志值偏移到零值的偏移量 - LevelOffset = 4 + // levelOffset 是把 LevelInfo 等级的日志值偏移到零值的偏移量 + levelOffset = 4 ) const ( // LevelDisabled 表示不处理任何等级的日志, // 其他日志等级的值越小表示日志严重程度越高. - LevelDisabled Level = iota - LevelOffset - 1 + LevelDisabled Level = iota - levelOffset - 1 LevelPanic LevelFatal LevelError diff --git a/log/logsdk/logger.go b/log/logsdk/logger.go index a9804a8..e147a73 100644 --- a/log/logsdk/logger.go +++ b/log/logsdk/logger.go @@ -20,11 +20,12 @@ type entry = Entry // Logger 中保存了日志所需的全局配置, // 使用 Logger 处理日志. type Logger struct { - exit func(code int) // protected by lock - beforeExit []func(code int) // protected by lock - levelProcessors levelProcessors // protected by lock + exit func(code int) // protected by lock + beforeExitFns []func() // protected by lock + levelProcessors levelProcessors // protected by lock entry lock sync.RWMutex + exitOnce sync.Once level atomic.Int32 reportStackLevel atomic.Int32 callerSkip atomic.Int32 @@ -36,7 +37,7 @@ type Logger struct { func (logger *Logger) AddProcessor(levels []Level, processor EntryProcessor) { logger.lock.Lock() for _, level := range levels { - logger.levelProcessors[level+LevelOffset] = append(logger.levelProcessors[level+LevelOffset], processor) + logger.levelProcessors[level+levelOffset] = append(logger.levelProcessors[level+levelOffset], processor) } logger.lock.Unlock() } @@ -99,7 +100,7 @@ func (logger *Logger) SetReportStackLevel(level Level) { func (logger *Logger) Reset() { logger.lock.Lock() logger.exit = nil - logger.beforeExit = nil + logger.beforeExitFns = nil logger.levelProcessors = levelProcessors{} logger.lock.Unlock() @@ -110,16 +111,27 @@ func (logger *Logger) Reset() { logger.SetReportStack(false) } -// Exit 退出程序, 执行的具体过程可以通过 SetExit 指定 +// BeforeExit 按照先添加后执行的顺序执行 AddBeforeExit 添加的函数, +// AddBeforeExit 只会执行1次. +func (logger *Logger) BeforeExit() { + logger.exitOnce.Do(func() { + logger.lock.RLock() + beforeExitFns := make([]func(), len(logger.beforeExitFns)) + copy(beforeExitFns, logger.beforeExitFns) + logger.lock.RUnlock() + for i := len(beforeExitFns) - 1; i >= 0; i-- { + beforeExitFns[i]() + } + }) +} + +// Exit 执行 BeforeExit 后退出程序, 执行的退出函数可以通过 SetExit 指定, +// 当没有通过 SetExit 指定退出函数时调用 [os.Exit]. func (logger *Logger) Exit(code int) { + logger.BeforeExit() logger.lock.RLock() exit := logger.exit - beforeExit := make([]func(int), len(logger.beforeExit)) - copy(beforeExit, logger.beforeExit) logger.lock.RUnlock() - for i := len(beforeExit) - 1; i >= 0; i-- { - beforeExit[i](code) - } if exit == nil { exit = os.Exit } @@ -135,16 +147,16 @@ func (logger *Logger) SetExit(fn func(code int)) { // AddBeforeExit 增加 Exit 在调用 SetExit 指定的函数前执行的函数, // 先增加的后执行. -func (logger *Logger) AddBeforeExit(fn ...func(code int)) { +func (logger *Logger) AddBeforeExit(fn ...func()) { logger.lock.Lock() - logger.beforeExit = append(logger.beforeExit, fn...) + logger.beforeExitFns = append(logger.beforeExitFns, fn...) logger.lock.Unlock() } func (logger *Logger) getLevelProcessors(level Level) []EntryProcessor { logger.lock.RLock() defer logger.lock.RUnlock() - return logger.levelProcessors[level+LevelOffset] + return logger.levelProcessors[level+levelOffset] } func (logger *Logger) newEntry() Entry { diff --git a/log/logsdk/runtime.go b/log/logsdk/runtime.go index ab92a1c..8ea3fca 100644 --- a/log/logsdk/runtime.go +++ b/log/logsdk/runtime.go @@ -8,6 +8,8 @@ const ( maximumFrames = 32 getCallerSkipOffset = 2 entrySkipOffset = 2 + + runtimeMain = "runtime.main" ) // Frame 调用相关信息 @@ -44,6 +46,9 @@ func getStack(skip, maximumFrames int) []Frame { for { frame, more := frames.Next() if frame.PC != 0 { + if frame.Function == runtimeMain { + break + } stack = append(stack, Frame{ Function: frame.Function, File: frame.File,