Skip to content

Commit 0af085c

Browse files
authored
avoid executing costly With if noop logger (#1015)
Prior to that change, Noop logger would execute With function for no benefits.
1 parent 9184eee commit 0af085c

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

internal/corazawaf/rule.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,14 @@ func (r *Rule) Evaluate(phase types.RulePhase, tx plugintypes.TransactionState,
171171
// collectiveMatchedValues lives across recursive calls of doEvaluate
172172
var collectiveMatchedValues []types.MatchData
173173

174-
var logger debuglog.Logger
174+
logger := tx.DebugLogger()
175175

176-
if r.ID_ == noID {
177-
logger = tx.DebugLogger().With(debuglog.Str("rule_ref", fmt.Sprintf("%s#L%d", r.File_, r.Line_)))
178-
} else {
179-
logger = tx.DebugLogger().With(debuglog.Int("rule_id", r.ID_))
176+
if logger.Debug().IsEnabled() {
177+
if r.ID_ == noID {
178+
logger = logger.With(debuglog.Str("rule_ref", fmt.Sprintf("%s#L%d", r.File_, r.Line_)))
179+
} else {
180+
logger = logger.With(debuglog.Int("rule_id", r.ID_))
181+
}
180182
}
181183

182184
r.doEvaluate(logger, phase, tx.(*Transaction), &collectiveMatchedValues, chainLevelZero, cache)
@@ -245,7 +247,10 @@ func (r *Rule) doEvaluate(logger debuglog.Logger, phase types.RulePhase, tx *Tra
245247

246248
values = tx.GetField(v)
247249

248-
vLog := logger.With(debuglog.Str("variable", v.Variable.Name()))
250+
vLog := logger
251+
if logger.Debug().IsEnabled() {
252+
vLog = logger.With(debuglog.Str("variable", v.Variable.Name()))
253+
}
249254
vLog.Debug().Msg("Expanding arguments for rule")
250255

251256
for i, arg := range values {

0 commit comments

Comments
 (0)