-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* gorm test logger refactor * add env variable for gorm logs
- Loading branch information
1 parent
f89b63e
commit 152d43c
Showing
8 changed files
with
90 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,7 @@ | |
"--fast" | ||
], | ||
"go.lintOnSave": "workspace", | ||
"go.testEnvVars": { | ||
"JIMM_TEST_LOG_SQL": "false" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2024 Canonical. | ||
package logger | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
|
||
"github.com/juju/zaputil/zapctx" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
// A Tester is the test interface required by gorm for the logger. | ||
type Tester interface { | ||
Fatalf(format string, args ...interface{}) | ||
Logf(format string, args ...interface{}) | ||
Name() string | ||
Cleanup(f func()) | ||
} | ||
|
||
// A gormLogger is a gorm.Logger that is used in tests. It logs everything | ||
// to the test. The logs are flushed only if the test fails. | ||
type gormLogger struct { | ||
GormLogger | ||
t Tester | ||
} | ||
|
||
// NewGormLogger returns a gorm logger.Interface that can be used in a test | ||
// All output is logged to the test. | ||
func NewGormTestLogger(t Tester) logger.Interface { | ||
output := gormTesterZapWriter{t} | ||
|
||
devConfig := zap.NewDevelopmentEncoderConfig() | ||
devConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder | ||
devConfig.EncodeTime = shortTimeEncoder | ||
|
||
logger := zap.New( | ||
zapcore.NewCore( | ||
zapcore.NewConsoleEncoder(devConfig), | ||
output, | ||
zap.DebugLevel, | ||
)) | ||
zapctx.Default = logger | ||
logSQL, _ := strconv.ParseBool(os.Getenv("JIMM_TEST_LOG_SQL")) | ||
return &gormLogger{ | ||
t: t, | ||
GormLogger: GormLogger{ | ||
LogSQL: logSQL, | ||
}, | ||
} | ||
} | ||
|
||
// gormTesterZapWriter is a zap writer wrapping the Tester interface. | ||
// It is used to comply with the behaviour of flushing the logs only in case of failure. | ||
type gormTesterZapWriter struct { | ||
t Tester | ||
} | ||
|
||
func (w gormTesterZapWriter) Write(buf []byte) (int, error) { | ||
w.t.Logf(string(buf)) | ||
return len(buf), nil | ||
} | ||
|
||
func (w gormTesterZapWriter) Sync() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters