From 7bb09785f2f04d7eded58679c3361fb13bf81277 Mon Sep 17 00:00:00 2001 From: Ge Song Date: Sat, 6 May 2023 14:54:36 +0800 Subject: [PATCH] =?UTF-8?q?log/Panic=E6=94=B9=E5=9B=9E=E8=B0=83=E7=94=A8pa?= =?UTF-8?q?nic;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/log.go | 6 +++--- log/logsdk/entry.go | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/log/log.go b/log/log.go index 4ba5e44..9a2e536 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 不会调用 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...) } diff --git a/log/logsdk/entry.go b/log/logsdk/entry.go index 47d971d..d2e51da 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 不会调用 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