Skip to content

Commit da4b681

Browse files
author
johnlanni
committed
refactor: UnsafeInfof downgrades to Debug with preserved newlines
- Remove UnsafeDebug/UnsafeDebugf (not needed) - UnsafeInfo/UnsafeInfof now downgrades to Debug level when safe log is enabled - Preserve newlines (convert escaped \n back to real newlines) so that line-based log collectors cannot capture complete sensitive data - Update README to explain the behavior
1 parent a248505 commit da4b681

2 files changed

Lines changed: 35 additions & 24 deletions

File tree

examples/safe-log-http-call/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,22 @@ func init() {
4242
}
4343
```
4444

45-
## What Gets Suppressed
45+
## How It Works
4646

47-
With safe log enabled, the following logs will NOT be printed:
47+
With safe log enabled, the following logs will be **downgraded from Info to Debug level**:
4848

4949
- `http call start` - request headers, body, URL, cluster info
5050
- `http call end` - response headers, body, status code
5151
- `route call start` - request headers, body
5252
- `route call end` - response headers, body
5353

54+
Additionally, newlines in the log messages are preserved (not escaped), so that line-based log collectors cannot capture the complete sensitive information in a single log entry.
55+
56+
**Why downgrade to Debug?**
57+
- Info is the default log level in production
58+
- Debug logs are only visible when explicitly enabled by system administrators
59+
- Even if Debug is enabled, the multi-line output prevents log collectors from capturing complete sensitive data
60+
5461
## Build
5562

5663
```bash

pkg/log/log.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
package log
1616

17+
import (
18+
"fmt"
19+
"strings"
20+
)
21+
1722
type Log interface {
1823
Trace(msg string)
1924
Tracef(format string, args ...interface{})
@@ -100,34 +105,33 @@ func Criticalf(format string, args ...interface{}) {
100105
pluginLog.Criticalf(format, args...)
101106
}
102107

103-
// UnsafeInfo logs a message at Info level only if safe log mode is disabled.
104-
// Use this for sensitive information that should not be logged in production.
108+
// UnsafeInfo logs a message at Info level when safe log mode is disabled.
109+
// When safe log mode is enabled, the message is downgraded to Debug level
110+
// with newlines preserved (not escaped), so that line-based log collectors
111+
// cannot capture the complete sensitive information.
105112
func UnsafeInfo(msg string) {
106-
if !safeLogEnabled {
113+
if safeLogEnabled {
114+
// In safe mode, downgrade to Debug level and preserve newlines
115+
// to prevent log collectors from capturing complete sensitive data
116+
msg = strings.ReplaceAll(msg, `\n`, "\n")
117+
pluginLog.Debug(msg)
118+
} else {
107119
pluginLog.Info(msg)
108120
}
109121
}
110122

111-
// UnsafeInfof logs a formatted message at Info level only if safe log mode is disabled.
112-
// Use this for sensitive information that should not be logged in production.
123+
// UnsafeInfof logs a formatted message at Info level when safe log mode is disabled.
124+
// When safe log mode is enabled, the message is downgraded to Debug level
125+
// with newlines preserved (not escaped), so that line-based log collectors
126+
// cannot capture the complete sensitive information.
113127
func UnsafeInfof(format string, args ...interface{}) {
114-
if !safeLogEnabled {
115-
pluginLog.Infof(format, args...)
116-
}
117-
}
118-
119-
// UnsafeDebug logs a message at Debug level only if safe log mode is disabled.
120-
// Use this for sensitive information that should not be logged in production.
121-
func UnsafeDebug(msg string) {
122-
if !safeLogEnabled {
128+
if safeLogEnabled {
129+
// In safe mode, downgrade to Debug level and preserve newlines
130+
// to prevent log collectors from capturing complete sensitive data
131+
msg := fmt.Sprintf(format, args...)
132+
msg = strings.ReplaceAll(msg, `\n`, "\n")
123133
pluginLog.Debug(msg)
124-
}
125-
}
126-
127-
// UnsafeDebugf logs a formatted message at Debug level only if safe log mode is disabled.
128-
// Use this for sensitive information that should not be logged in production.
129-
func UnsafeDebugf(format string, args ...interface{}) {
130-
if !safeLogEnabled {
131-
pluginLog.Debugf(format, args...)
134+
} else {
135+
pluginLog.Infof(format, args...)
132136
}
133137
}

0 commit comments

Comments
 (0)