Fix gorm newlines issue (#108)
Even if you made a custom formatter for gorm, it would blithely println anything returned, including nils, resulting in lots of new lines when we were ignoring output. This also creates a custom logger that doesn't do anything when newlines are found.
This commit is contained in:
1 parent
6804c98b0b
commit
5333482ff8
3 files changed
+20
No files matched your search
@@ -402,6 +402,7 @@ func (b *Server) CreateDevDatabase(dialect string) error {
|
||||
b.Database = dbase
|
||||
|
||||
gorm.LogFormatter = db.GetGormLogFormatter(b.Logger)
|
||||
b.Database.SetLogger(db.GetGormLogger(b.Logger))
|
||||
b.Database.LogMode(true)
|
||||
|
||||
rw := db.New(b.Database)
|
||||
|
||||
@@ -182,3 +182,21 @@ func GetGormLogFormatter(log hclog.Logger) func(values ...interface{}) (messages
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type gormLogger struct {
|
||||
logger hclog.Logger
|
||||
}
|
||||
|
||||
func (g gormLogger) Print(values ...interface{}) {
|
||||
formatted := gorm.LogFormatter(values...)
|
||||
if formatted == nil {
|
||||
return
|
||||
}
|
||||
// Our formatter should elide anything we don't want so this should never
|
||||
// happen, panic if so so we catch/fix
|
||||
panic("unhandled error case")
|
||||
}
|
||||
|
||||
func GetGormLogger(log hclog.Logger) gormLogger {
|
||||
return gormLogger{logger: log}
|
||||
}
|
||||
@@ -684,6 +684,7 @@ func TestDb_SearchWhere(t *testing.T) {
|
||||
Level: hclog.Trace,
|
||||
})
|
||||
gorm.LogFormatter = GetGormLogFormatter(log)
|
||||
db.SetLogger(GetGormLogger(log))
|
||||
db.LogMode(true)
|
||||
defer func() {
|
||||
assert.True(strings.Contains(buf.String(), "syntax error at or near"))
|
||||
|
||||
Reference in new issue
Block a user