Log SQL with placeholders, never bound values (closes #207) (#222)
Some checks failed
check / check (push) Superseded by a newer commit; never tested

This commit was merged in pull request #222.
This commit is contained in:
2026-08-20 07:20:59 +02:00
parent 4cc83b2326
commit 5af161ef60
6 changed files with 807 additions and 17 deletions

View File

@@ -232,7 +232,7 @@ func TestSlowRecordNotFound_IsStillReportedSlow(t *testing.T) {
require.ErrorIs(t, err, gorm.ErrRecordNotFound)
assert.Contains(
t, buf.String(), "slow sql statement",
t, buf.String(), slowLine,
"a slow statement that missed was not "+
"reported as slow",
)
@@ -284,9 +284,11 @@ func TestRecordNotFoundFlood_DoesNotGrowWithInput(t *testing.T) {
}
// TestStatementError_LineIsBounded covers the branch that does log.
// A driver error is not ErrRecordNotFound, so the interpolated
// statement is written and on an insert the interpolated value is
// still whatever the client supplied.
// A driver error is not ErrRecordNotFound, so the statement is
// written, and the driver's own error text can quote what the client
// supplied. The statement's parameters are no longer part of that —
// see TestBoundValues_NeverReachTheLog — but the budget is what holds
// the line when the statement itself, or the error, is the long part.
func TestStatementError_LineIsBounded(t *testing.T) {
t.Parallel()
@@ -313,7 +315,7 @@ func TestStatementError_LineIsBounded(t *testing.T) {
require.Error(t, err)
assert.Contains(
t, buf.String(), "sql statement failed",
t, buf.String(), errorLine,
)
assertBounded(t, buf.String())
})
@@ -329,22 +331,22 @@ func TestStatementError_LineIsBounded(t *testing.T) {
// and would have cost this report, which is the one thing GORM's
// logger gave an operator that nothing else in this service does.
// - routine. The branch an operator reaches by turning the level
// down to DEBUG: every statement is reported, so every
// statement's interpolated parameters have to be bounded too.
// down to DEBUG: every statement is reported, so every statement
// has to be bounded too.
func TestSucceedingStatement_LineIsBoundedOnEitherArm(t *testing.T) {
t.Parallel()
// "sql statement" is a substring of "slow sql statement", so the
// routine arm carries notWant as well: Contains alone cannot tell
// the two arms apart in that direction.
// routineLine is a substring of slowLine, so the routine arm
// carries notWant as well: Contains alone cannot tell the two arms
// apart in that direction.
arms := []struct {
name string
slow time.Duration
want string
notWant string
}{
{"slow", alwaysSlow, "slow sql statement", ""},
{"routine", neverSlow, "sql statement", "slow sql statement"},
{"slow", alwaysSlow, slowLine, ""},
{"routine", neverSlow, routineLine, slowLine},
}
for _, a := range arms {