diff --git a/log/kis_default_log.go b/log/kis_default_log.go index 94da4ee..f6504a3 100644 --- a/log/kis_default_log.go +++ b/log/kis_default_log.go @@ -2,66 +2,136 @@ package log import ( "context" - "fmt" + "io" + "log/slog" + "os" + "path/filepath" "sync" ) -// kisDefaultLog 默认提供的日志对象 -type kisDefaultLog struct { - debugMode bool - mu sync.Mutex -} - -func (log *kisDefaultLog) SetDebugMode(enable bool) { - log.mu.Lock() - defer log.mu.Unlock() - log.debugMode = enable -} - -func (log *kisDefaultLog) InfoF(str string, v ...interface{}) { - fmt.Printf(str, v...) - fmt.Printf("\n") -} - -func (log *kisDefaultLog) ErrorF(str string, v ...interface{}) { - fmt.Printf(str, v...) - fmt.Printf("\n") -} - -func (log *kisDefaultLog) DebugF(str string, v ...interface{}) { - log.mu.Lock() - defer log.mu.Unlock() - if log.debugMode { - fmt.Printf(str, v...) - fmt.Printf("\n") - } -} - -func (log *kisDefaultLog) InfoFX(ctx context.Context, str string, v ...interface{}) { - fmt.Println(ctx) - fmt.Printf(str, v...) - fmt.Printf("\n") -} - -func (log *kisDefaultLog) ErrorFX(ctx context.Context, str string, v ...interface{}) { - fmt.Println(ctx) - fmt.Printf(str, v...) - fmt.Printf("\n") -} - -func (log *kisDefaultLog) DebugFX(ctx context.Context, str string, v ...interface{}) { - log.mu.Lock() - defer log.mu.Unlock() - if log.debugMode { - fmt.Println(ctx) - fmt.Printf(str, v...) - fmt.Printf("\n") - } -} - func init() { - // 如果没有设置Logger, 则启动时使用默认的kisDefaultLog对象 + // 如果没有设置 Logger, 则启动时使用默认的 kisDefaultSlog 对象 if Logger() == nil { - SetLogger(&kisDefaultLog{}) + MustNewKisDefaultSlog() } } + +func MustNewKisDefaultSlog(opts ...KisLogOptions) { + initDefaultSlog(loadKisDefaultLog(opts...)) + SetLogger(&kisDefaultSlog{}) +} + +// kisDefaultSlog 默认提供的日志对象 +type kisDefaultSlog struct { + location bool + level slog.Level + jsonFormat bool + writer io.Writer + + mu sync.Mutex +} + +type KisLogOptions func(k *kisDefaultSlog) + +func WithLocation(location bool) KisLogOptions { + return func(k *kisDefaultSlog) { + k.location = location + } +} + +func WithLevel(level slog.Level) KisLogOptions { + return func(k *kisDefaultSlog) { + k.level = level + } +} + +func WithJSONFormat(jsonFormat bool) KisLogOptions { + return func(k *kisDefaultSlog) { + k.jsonFormat = jsonFormat + } +} + +func WithWriter(writer io.Writer) KisLogOptions { + return func(k *kisDefaultSlog) { + k.writer = writer + } +} + +var defaultKisLog = &kisDefaultSlog{ + location: true, + level: slog.LevelDebug, + jsonFormat: false, + writer: os.Stdout, +} + +func loadKisDefaultLog(opts ...KisLogOptions) *kisDefaultSlog { + kisLog := defaultKisLog + if opts == nil { + return kisLog + } + + for _, opt := range opts { + opt(kisLog) + } + + return kisLog +} + +func (k *kisDefaultSlog) InfoFX(ctx context.Context, str string, v ...interface{}) { + slog.InfoContext(ctx, str, v) +} + +func (k *kisDefaultSlog) ErrorFX(ctx context.Context, str string, v ...interface{}) { + slog.ErrorContext(ctx, str, v) +} + +func (k *kisDefaultSlog) DebugFX(ctx context.Context, str string, v ...interface{}) { + slog.DebugContext(ctx, str, v) +} + +func (k *kisDefaultSlog) InfoF(str string, v ...interface{}) { + slog.Info(str, v) +} + +func (k *kisDefaultSlog) ErrorF(str string, v ...interface{}) { + slog.Error(str, v) +} + +func (k *kisDefaultSlog) DebugF(str string, v ...interface{}) { + slog.Debug(str, v) +} + +func (k *kisDefaultSlog) SetDebugMode(enable bool) { + k.mu.Lock() + defer k.mu.Unlock() + + if enable { + k.level = slog.LevelDebug + } +} + +func initDefaultSlog(kisLog *kisDefaultSlog) { + replace := func(groups []string, a slog.Attr) slog.Attr { + if a.Key == slog.SourceKey { + source := a.Value.Any().(*slog.Source) + source.Function = "" + source.File = filepath.Base(source.File) + } + + return a + } + + ho := &slog.HandlerOptions{ + AddSource: kisLog.location, + Level: kisLog.level, + ReplaceAttr: replace, + } + + var logger *slog.Logger + if kisLog.jsonFormat { + logger = slog.New(slog.NewJSONHandler(kisLog.writer, ho)) + } else { + logger = slog.New(slog.NewTextHandler(kisLog.writer, ho)) + } + slog.SetDefault(logger) +}