2022-10-22 22:08:02 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2024-01-01 16:52:18 +00:00
|
|
|
// LogWriter implements a logger for the gorm logging
|
2023-11-25 19:23:34 +00:00
|
|
|
LogWriter struct{ io.Writer }
|
2022-10-22 22:08:02 +00:00
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// NewLogrusLogWriterWithLevel creates a new LogWriter with the given
|
|
|
|
// logrus.Logger and the specified logrus.Level
|
2023-11-25 19:23:34 +00:00
|
|
|
func NewLogrusLogWriterWithLevel(logger *logrus.Logger, level logrus.Level, dbDriver string) LogWriter {
|
|
|
|
writer := logger.WithField("database", dbDriver).WriterLevel(level)
|
|
|
|
return LogWriter{writer}
|
2022-10-22 22:08:02 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Print implements the gorm.Logger interface
|
2023-11-25 19:23:34 +00:00
|
|
|
func (l LogWriter) Print(a ...any) {
|
2024-06-09 10:44:41 +00:00
|
|
|
fmt.Fprint(l.Writer, a...) //nolint:errcheck // Interface ignores this error
|
2022-10-29 13:01:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Printf implements the gorm.Logger interface
|
2023-11-25 19:23:34 +00:00
|
|
|
func (l LogWriter) Printf(format string, a ...any) {
|
2024-06-09 10:44:41 +00:00
|
|
|
fmt.Fprintf(l.Writer, format, a...) //nolint:errcheck // Interface ignores this error
|
2022-10-22 22:08:02 +00:00
|
|
|
}
|