Redmage/db/sql_logger.go

30 lines
610 B
Go
Raw Normal View History

2024-04-08 15:48:45 +07:00
package db
import (
"context"
"log/slog"
"strings"
sqldblogger "github.com/simukti/sqldb-logger"
"github.com/tigorlazuardi/redmage/pkg/log"
)
type sqlLogger struct{}
func (sqlLogger) Log(ctx context.Context, level sqldblogger.Level, msg string, data map[string]interface{}) {
var lvl slog.Level
switch level {
2024-05-13 11:19:54 +07:00
case sqldblogger.LevelDebug, sqldblogger.LevelTrace, sqldblogger.LevelInfo:
2024-04-08 15:48:45 +07:00
lvl = slog.LevelDebug
case sqldblogger.LevelError:
lvl = slog.LevelError
}
2024-05-13 13:38:13 +07:00
entry := log.New(ctx)
if entry.Accept(lvl) {
msg = strings.TrimSpace(msg)
entry.With("sql", data).Level(lvl).Log(msg)
}
2024-04-08 15:48:45 +07:00
}