mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 08:40:01 +00:00
[core] Configure logger for mysql driver
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
064c7432ed
commit
231704f8b3
2 changed files with 9 additions and 10 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
mysqlDriver "github.com/go-sql-driver/mysql"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
|
@ -35,6 +36,7 @@ func New(driverName, connString, encryptionSecret string) (Connector, error) {
|
|||
|
||||
switch driverName {
|
||||
case "mysql":
|
||||
mysqlDriver.SetLogger(newLogrusLogWriterWithLevel(logrus.ErrorLevel, driverName))
|
||||
innerDB = mysql.Open(connString)
|
||||
|
||||
case "postgres":
|
||||
|
@ -53,7 +55,7 @@ func New(driverName, connString, encryptionSecret string) (Connector, error) {
|
|||
}
|
||||
|
||||
db, err := gorm.Open(innerDB, &gorm.Config{
|
||||
Logger: gormLogger(),
|
||||
Logger: logger.New(newLogrusLogWriterWithLevel(logrus.TraceLevel, driverName), logger.Config{}),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "connecting database")
|
||||
|
@ -85,13 +87,6 @@ func (c connector) applyCoreSchema() error {
|
|||
return errors.Wrap(c.db.AutoMigrate(&coreKV{}), "applying coreKV schema")
|
||||
}
|
||||
|
||||
func gormLogger() logger.Interface {
|
||||
return logger.New(
|
||||
newLogrusLogWriterWithLevel(logrus.TraceLevel),
|
||||
logger.Config{},
|
||||
)
|
||||
}
|
||||
|
||||
func patchSQLiteConnString(connString string) (string, error) {
|
||||
u, err := url.Parse(connString)
|
||||
if err != nil {
|
||||
|
|
|
@ -11,11 +11,15 @@ type (
|
|||
logWriter struct{ io.Writer }
|
||||
)
|
||||
|
||||
func newLogrusLogWriterWithLevel(level logrus.Level) logWriter {
|
||||
writer := logrus.StandardLogger().WriterLevel(level)
|
||||
func newLogrusLogWriterWithLevel(level logrus.Level, dbDriver string) logWriter {
|
||||
writer := logrus.WithField("database", dbDriver).WriterLevel(level)
|
||||
return logWriter{writer}
|
||||
}
|
||||
|
||||
func (l logWriter) Print(a ...any) {
|
||||
fmt.Fprint(l.Writer, a...)
|
||||
}
|
||||
|
||||
func (l logWriter) Printf(format string, a ...any) {
|
||||
fmt.Fprintf(l.Writer, format, a...)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue