From 5577322a3d0e30b30f529b35e7b47dd8cd9ee4be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=8D=A3=E6=98=8C?= Date: Mon, 8 Apr 2024 19:03:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=BC=E5=AE=B9=E4=B9=8B=E5=89=8D=20l?= =?UTF-8?q?ogFormat=20=E6=A0=BC=E5=BC=8F=E5=8C=96=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/kis_default_log.go | 26 ++++++++++++++++++++++++++ log/kis_log.go | 14 ++++++++++++++ test/kis_log_test.go | 11 +++++++++++ 3 files changed, 51 insertions(+) diff --git a/log/kis_default_log.go b/log/kis_default_log.go index 208994d..914e6ea 100644 --- a/log/kis_default_log.go +++ b/log/kis_default_log.go @@ -2,6 +2,7 @@ package log import ( "context" + "fmt" "io" "log/slog" "os" @@ -78,6 +79,31 @@ func getKisDefaultSLog(opts ...KisLogOptions) *kisDefaultSlog { return defaultKisSlog } +func (k *kisDefaultSlog) InfoFX(ctx context.Context, str string, v ...interface{}) { + slog.InfoContext(ctx, fmt.Sprintf(str, v...)) +} + +func (k *kisDefaultSlog) ErrorFX(ctx context.Context, str string, v ...interface{}) { + slog.ErrorContext(ctx, fmt.Sprintf(str, v...)) +} + +func (k *kisDefaultSlog) DebugFX(ctx context.Context, str string, v ...interface{}) { + slog.DebugContext(ctx, fmt.Sprintf(str, v...)) +} + +// InfoF 使用格式化格式(xxxF或xxxFX)要使用 fmt.Sprintf() 函数进行格式化包装 +func (k *kisDefaultSlog) InfoF(str string, v ...interface{}) { + slog.Info(fmt.Sprintf(str, v...)) +} + +func (k *kisDefaultSlog) ErrorF(str string, v ...interface{}) { + slog.Error(fmt.Sprintf(str, v...)) +} + +func (k *kisDefaultSlog) DebugF(str string, v ...interface{}) { + slog.Debug(fmt.Sprintf(str, v...)) +} + func (k *kisDefaultSlog) InfoX(ctx context.Context, str string, v ...interface{}) { slog.InfoContext(ctx, str, v...) } diff --git a/log/kis_log.go b/log/kis_log.go index 52b90c2..381c04e 100644 --- a/log/kis_log.go +++ b/log/kis_log.go @@ -3,6 +3,20 @@ package log import "context" type KisLogger interface { + // InfoFX 有上下文的Info级别日志接口, format字符串格式 + InfoFX(ctx context.Context, str string, v ...interface{}) + // ErrorFX 有上下文的Error级别日志接口, format字符串格式 + ErrorFX(ctx context.Context, str string, v ...interface{}) + // DebugFX 有上下文的Debug级别日志接口, format字符串格式 + DebugFX(ctx context.Context, str string, v ...interface{}) + + // InfoF 无上下文的Info级别日志接口, format字符串格式 + InfoF(str string, v ...interface{}) + // ErrorF 无上下文的Error级别日志接口, format字符串格式 + ErrorF(str string, v ...interface{}) + // DebugF 无上下文的Debug级别日志接口, format字符串格式 + DebugF(str string, v ...interface{}) + // InfoX 有上下文的Info级别日志接口, format字符串格式 InfoX(ctx context.Context, str string, v ...interface{}) // ErrorX 有上下文的Error级别日志接口, format字符串格式 diff --git a/test/kis_log_test.go b/test/kis_log_test.go index d9bd30e..d05a023 100644 --- a/test/kis_log_test.go +++ b/test/kis_log_test.go @@ -9,6 +9,17 @@ import ( func TestKisLogger(t *testing.T) { ctx := context.Background() + log.Logger().DebugF("TestKisLogger Format DebugF name = %s, age = %d", "kisFlow", 23) + log.Logger().ErrorF("TestKisLogger Format ErrorF name = %s, age = %d", "kisFlow", 12) + log.Logger().InfoF("TestKisLogger Format InfoF name = %s, stu =%+v", "kisFlow", + struct { + name string + age int + }{ + name: "kisName", + age: 12, + }) + log.Logger().InfoX(ctx, "TestKisLogger InfoX") log.Logger().ErrorX(ctx, "TestKisLogger ErrorX") log.Logger().DebugX(ctx, "TestKisLogger DebugX")