fix: 兼容之前 logFormat 格式化输出

This commit is contained in:
王荣昌 2024-04-08 19:03:20 +08:00
parent d7d1383a94
commit 5577322a3d
3 changed files with 51 additions and 0 deletions

View File

@ -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...)
}

View File

@ -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字符串格式

View File

@ -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")