Compare commits
2 Commits
log/v0.12.
...
log/v0.14.
| Author | SHA1 | Date | |
|---|---|---|---|
| a698a8135d | |||
| 7bb09785f2 |
@@ -54,8 +54,7 @@ log.Logger()
|
||||
- `SetReportStack` 设置生成调用栈;
|
||||
- `SetReportStackLevel` 当日志等级小于设定值时强制生成调用栈;
|
||||
- `Reset` 把 Logger 恢复到初始状态;
|
||||
- `AddBeforeExit` 新增调用 `Logger.Exit` 时优先执行的函数, 先增加的后执行;
|
||||
- `SetExit` 设置 `Logger` 的退出函数(`Logger.Exit`), 当日志等级为 `LevelFatal` 时调用这个函数;
|
||||
- `AddBeforeExit` 增加 Exit 在调用 [os.Exit] 前执行的函数, 先增加的后执行;
|
||||
|
||||
### 日志生成
|
||||
|
||||
|
||||
@@ -104,8 +104,8 @@ func Fatal(ctx context.Context, args ...any) {
|
||||
globalLogger.AddCallerSkip(1).Fatal(ctx, args...)
|
||||
}
|
||||
|
||||
// Panic 输出 LevelPanic 等级的日志,
|
||||
// Panic 不会调用 panic 函数.
|
||||
// Panic 输出 LevelPanic 等级的日志后执行 panic,
|
||||
// 即使 Logger 日志等级高于 LevelPanic 也会 panic.
|
||||
func Panic(ctx context.Context, args ...any) {
|
||||
globalLogger.AddCallerSkip(1).Panic(ctx, args...)
|
||||
}
|
||||
@@ -146,7 +146,7 @@ func Fatalf(ctx context.Context, format string, args ...any) {
|
||||
}
|
||||
|
||||
// Panicf 格式化输出 LevelPanic 等级的日志,
|
||||
// Panicf 不会调用 panic 函数.
|
||||
// 即使 Logger 日志等级高于 LevelPanic 也会 panic.
|
||||
func Panicf(ctx context.Context, format string, args ...any) {
|
||||
globalLogger.AddCallerSkip(1).Panicf(ctx, format, args...)
|
||||
}
|
||||
|
||||
@@ -127,8 +127,8 @@ func (entry Entry) Fatal(ctx context.Context, args ...any) {
|
||||
entry.logger.Exit(1)
|
||||
}
|
||||
|
||||
// Panic 输出 LevelPanic 等级的日志,
|
||||
// Panic 不会调用 panic 函数.
|
||||
// Panic 输出 LevelPanic 等级的日志后执行 panic,
|
||||
// 即使 Logger 日志等级高于 LevelPanic 也会 panic.
|
||||
func (entry Entry) Panic(ctx context.Context, args ...any) {
|
||||
entry.log(ctx, LevelPanic, fmt.Sprint(args...))
|
||||
}
|
||||
@@ -170,12 +170,17 @@ func (entry Entry) Fatalf(ctx context.Context, format string, args ...any) {
|
||||
}
|
||||
|
||||
// Panicf 格式化输出 LevelPanic 等级的日志,
|
||||
// Panicf 不会调用 panic 函数.
|
||||
// 即使 Logger 日志等级高于 LevelPanic 也会 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) {
|
||||
defer func() {
|
||||
if level == LevelPanic {
|
||||
panic(message)
|
||||
}
|
||||
}()
|
||||
newEntry := entry.copy()
|
||||
if newEntry.logger.GetLevel() < level {
|
||||
return
|
||||
|
||||
@@ -20,7 +20,6 @@ type entry = Entry
|
||||
// Logger 中保存了日志所需的全局配置,
|
||||
// 使用 Logger 处理日志.
|
||||
type Logger struct {
|
||||
exit func(code int) // protected by lock
|
||||
beforeExitFns []func() // protected by lock
|
||||
levelProcessors levelProcessors // protected by lock
|
||||
entry
|
||||
@@ -99,7 +98,6 @@ func (logger *Logger) SetReportStackLevel(level Level) {
|
||||
// Reset 把 Logger 重置到初始状态
|
||||
func (logger *Logger) Reset() {
|
||||
logger.lock.Lock()
|
||||
logger.exit = nil
|
||||
logger.beforeExitFns = nil
|
||||
logger.levelProcessors = levelProcessors{}
|
||||
logger.lock.Unlock()
|
||||
@@ -125,27 +123,13 @@ func (logger *Logger) BeforeExit() {
|
||||
})
|
||||
}
|
||||
|
||||
// Exit 执行 BeforeExit 后退出程序, 执行的退出函数可以通过 SetExit 指定,
|
||||
// 当没有通过 SetExit 指定退出函数时调用 [os.Exit].
|
||||
// Exit 执行 BeforeExit 后调用 [os.Exit]退出程序.
|
||||
func (logger *Logger) Exit(code int) {
|
||||
logger.BeforeExit()
|
||||
logger.lock.RLock()
|
||||
exit := logger.exit
|
||||
logger.lock.RUnlock()
|
||||
if exit == nil {
|
||||
exit = os.Exit
|
||||
}
|
||||
exit(code)
|
||||
os.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()) {
|
||||
logger.lock.Lock()
|
||||
|
||||
Reference in New Issue
Block a user