chore: add database log

This commit is contained in:
andig 2022-10-09 14:57:32 +02:00
parent c0f2d5655f
commit 4963842e77
3 changed files with 39 additions and 4 deletions

View file

@ -44,9 +44,7 @@ func (s *DB) Session(meter float64) *Session {
// Persist creates or updates a transaction in the database
func (s *DB) Persist(session interface{}) {
s.log.TRACE.Printf("store: %+v", session)
if err := s.db.Save(session).Error; err != nil {
s.log.ERROR.Printf("store: %v", err)
s.log.ERROR.Printf("persist: %v", err)
}
}

View file

@ -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
View 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)
}