Skip to content

Commit acbbe75

Browse files
committed
refactor: add optional maxLength parameter to SanitizeForLog
Defaults to 100 characters. Pass 0 to disable truncation. Signed-off-by: Dorin Geman <dorin.geman@docker.com>
1 parent 2e1f8c0 commit acbbe75

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

‎pkg/inference/backends/runner.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func RunBackend(ctx context.Context, config RunnerConfig) error {
6060
// Sanitize args for safe logging
6161
sanitizedArgs := make([]string, len(config.Args))
6262
for i, arg := range config.Args {
63-
sanitizedArgs[i] = utils.SanitizeForLog(arg)
63+
sanitizedArgs[i] = utils.SanitizeForLog(arg, 0)
6464
}
6565
config.Logger.Infof("%s args: %v", config.BackendName, sanitizedArgs)
6666

‎pkg/internal/utils/log.go‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77

88
// SanitizeForLog sanitizes a string for safe logging by removing or escaping
99
// control characters that could cause log injection attacks.
10+
// The optional maxLength parameter controls truncation (default: 100).
11+
// Pass 0 or negative to disable truncation.
1012
// TODO: Consider migrating to structured logging which
1113
// handles sanitization automatically through field encoding.
12-
func SanitizeForLog(s string) string {
14+
func SanitizeForLog(s string, maxLength ...int) string {
1315
if s == "" {
1416
return ""
1517
}
@@ -42,9 +44,15 @@ func SanitizeForLog(s string) string {
4244
}
4345
}
4446

45-
const maxLength = 100
46-
if result.Len() > maxLength {
47-
return result.String()[:maxLength] + "...[truncated]"
47+
// Default maxLength is 100, or use provided value.
48+
// Pass 0 or negative to disable truncation.
49+
maxLen := 100
50+
if len(maxLength) > 0 {
51+
maxLen = maxLength[0]
52+
}
53+
54+
if maxLen > 0 && result.Len() > maxLen {
55+
return result.String()[:maxLen] + "...[truncated]"
4856
}
4957

5058
return result.String()

0 commit comments

Comments
 (0)