chore: add database log
This commit is contained in:
parent
c0f2d5655f
commit
4963842e77
3 changed files with 39 additions and 4 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"gorm.io/gorm"
|
||||
|
|
@ -34,7 +35,9 @@ func New(driver, dsn string) (*gorm.DB, error) {
|
|||
return nil, fmt.Errorf("invalid database type: %s not in [sqlite]", driver)
|
||||
}
|
||||
|
||||
return gorm.Open(dialect, &gorm.Config{})
|
||||
return gorm.Open(dialect, &gorm.Config{
|
||||
Logger: &Logger{util.NewLogger("db")},
|
||||
})
|
||||
}
|
||||
|
||||
func NewInstance(driver, dsn string) (err error) {
|
||||
|
|
|
|||
34
server/db/log.go
Normal file
34
server/db/log.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
log *util.Logger
|
||||
}
|
||||
|
||||
func (l *Logger) LogMode(logger.LogLevel) logger.Interface {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *Logger) Info(_ context.Context, msg string, val ...interface{}) {
|
||||
l.log.INFO.Printf(msg, val...)
|
||||
}
|
||||
|
||||
func (l *Logger) Warn(_ context.Context, msg string, val ...interface{}) {
|
||||
l.log.WARN.Printf(msg, val...)
|
||||
}
|
||||
|
||||
func (l *Logger) Error(_ context.Context, msg string, val ...interface{}) {
|
||||
l.log.ERROR.Printf(msg, val...)
|
||||
}
|
||||
|
||||
func (l *Logger) Trace(_ context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
|
||||
sql, rows := fc()
|
||||
l.log.TRACE.Println(sql, rows, err)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue