Add a gorm log formatter to adapt to hclog (#93)
This is also added in a test that otherwise output to stdout, which validates functionality.
This commit is contained in:
1 parent
5bdd8a46ea
commit
c175fcde8d
3 files changed
+42
-6
No files matched your search
@@ -30,10 +30,7 @@ import (
|
||||
"github.com/hashicorp/watchtower/version"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/mitchellh/cli"
|
||||
"golang.org/x/net/http/httpproxy"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -114,9 +111,16 @@ func (b *Server) SetupLogging(flagLogLevel, flagLogFormat, configLogLevel, confi
|
||||
b.LogLevel = logLevel
|
||||
|
||||
// log proxy settings
|
||||
proxyCfg := httpproxy.FromEnvironment()
|
||||
b.Logger.Info("proxy environment", "http_proxy", proxyCfg.HTTPProxy,
|
||||
"https_proxy", proxyCfg.HTTPSProxy, "no_proxy", proxyCfg.NoProxy)
|
||||
// TODO: It would be good to show this but Vault has, or will soon, address
|
||||
// the fact that this can log users/passwords if they are part of the proxy
|
||||
// URL. When they change things to address that we should update the below
|
||||
// logic and re-enable.
|
||||
/*
|
||||
proxyCfg := httpproxy.FromEnvironment()
|
||||
b.Logger.Info("proxy environment", "http_proxy", proxyCfg.HTTPProxy,
|
||||
"https_proxy", proxyCfg.HTTPSProxy, "no_proxy", proxyCfg.NoProxy)
|
||||
*/
|
||||
// Setup gorm logging
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -397,6 +401,9 @@ func (b *Server) CreateDevDatabase(dialect string) error {
|
||||
}
|
||||
b.Database = dbase
|
||||
|
||||
gorm.LogFormatter = db.GetGormLogFormatter(b.Logger)
|
||||
b.Database.LogMode(true)
|
||||
|
||||
rw := db.New(b.Database)
|
||||
repo, err := iam.NewRepository(rw, rw, b.ControllerKMS)
|
||||
if err != nil {
|
||||
|
||||
@@ -8,9 +8,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/hashicorp/watchtower/internal/db/migrations"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/lib/pq"
|
||||
"github.com/ory/dockertest"
|
||||
)
|
||||
|
||||
@@ -167,3 +169,16 @@ func cleanupDockerResource(pool *dockertest.Pool, resource *dockertest.Resource)
|
||||
}
|
||||
return fmt.Errorf("Failed to cleanup local container: %s", err)
|
||||
}
|
||||
|
||||
func GetGormLogFormatter(log hclog.Logger) func(values ...interface{}) (messages []interface{}) {
|
||||
return func(values ...interface{}) (messages []interface{}) {
|
||||
if len(values) > 2 && values[0].(string) == "log" {
|
||||
switch values[2].(type) {
|
||||
case *pq.Error:
|
||||
log.Trace("error from database adapter", "location", values[1], "error", values[2])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/go-uuid"
|
||||
"github.com/hashicorp/vault/sdk/helper/base62"
|
||||
"github.com/hashicorp/watchtower/internal/db/db_test"
|
||||
@@ -655,6 +657,18 @@ func TestDb_SearchWhere(t *testing.T) {
|
||||
}()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
log := hclog.New(&hclog.LoggerOptions{
|
||||
Output: buf,
|
||||
Level: hclog.Trace,
|
||||
})
|
||||
gorm.LogFormatter = GetGormLogFormatter(log)
|
||||
db.LogMode(true)
|
||||
defer func() {
|
||||
assert.True(strings.Contains(buf.String(), "syntax error at or near"))
|
||||
}()
|
||||
|
||||
t.Run("simple", func(t *testing.T) {
|
||||
w := Db{underlying: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
|
||||
Reference in new issue
Block a user