package logger import ( "fmt" "log/slog" ) // stdLoggerish provides a bridge between legacy log interfaces and slog. // It implements common logging methods (Println, Printf, Fatalf) that // delegate to structured logging with a consistent key prefix. type stdLoggerish struct { key string // Prefix key for all log messages log *slog.Logger // Underlying structured logger f func(string, ...any) // Log function (Info or Debug level) } // NewStdLog creates a legacy-compatible logger that bridges to structured logging. // This is useful for third-party libraries that expect a standard log.Logger interface. // // Parameters: // - key: Prefix added to all log messages for identification // - debug: If true, logs at debug level; otherwise logs at info level // - log: Underlying slog.Logger (uses Setup() if nil) // // The returned logger implements Println, Printf, and Fatalf methods. func NewStdLog(key string, debug bool, log *slog.Logger) *stdLoggerish { if log == nil { log = Setup() } sl := &stdLoggerish{ key: key, log: log, } sl.f = log.Info if debug { sl.f = log.Debug } return sl } // Println logs the arguments using the configured log level with the instance key. func (l stdLoggerish) Println(msg ...any) { l.f(l.key, "msg", msg) } // Printf logs a formatted message using the configured log level with the instance key. func (l stdLoggerish) Printf(msg string, args ...any) { l.f(l.key, "msg", fmt.Sprintf(msg, args...)) } // Fatalf logs a formatted error message and panics. // Note: This implementation panics instead of calling os.Exit for testability. func (l stdLoggerish) Fatalf(msg string, args ...any) { l.log.Error(l.key, "msg", fmt.Sprintf(msg, args...)) panic("fatal error") // todo: does this make sense at all? }