diff --git a/logger/httplog/body_logger.go b/logger/httplog/body_logger.go index 1a59e5c5b..653ea3b11 100644 --- a/logger/httplog/body_logger.go +++ b/logger/httplog/body_logger.go @@ -6,6 +6,7 @@ import ( "fmt" "sort" "strings" + "unicode" ) type bodyLogger struct { @@ -122,7 +123,15 @@ func (b bodyLogger) redactedDump(prefix string, body []byte) string { sb.WriteString("\n") } line = strings.Trim(line, "\r") - sb.WriteString(fmt.Sprintf("%s%s", prefix, line)) + sb.WriteString(prefix) + for _, c := range line { + // Convert non-ASCII and non-printable characters to '.' + // But preserve tabs and spaces for indentation + if c > unicode.MaxASCII || (!unicode.IsPrint(c) && c != '\t') { + c = '.' + } + sb.WriteRune(c) + } } return sb.String() } diff --git a/logger/httplog/body_logger_test.go b/logger/httplog/body_logger_test.go index 0eafac055..0e12510ae 100644 --- a/logger/httplog/body_logger_test.go +++ b/logger/httplog/body_logger_test.go @@ -69,6 +69,11 @@ func TestBodyLoggerNotJson(t *testing.T) { assert.Equal(t, dump, "") } +func TestBodyLoggerNotAscii(t *testing.T) { + dump := bodyLogger{debugTruncateBytes: 20}.redactedDump("", []byte("\x01 🚀 🚀")) + assert.Equal(t, dump, ". . .") +} + func TestBodyLoggerNonJSONNewline(t *testing.T) { dump := bodyLogger{debugTruncateBytes: 100}.redactedDump("< ", []byte(`