Skip to content

fix: preserve literal '%' in non-format log messages#1334

Open
sueun-dev wants to merge 1 commit into
getsentry:masterfrom
sueun-dev:fix-log-literal-percent
Open

fix: preserve literal '%' in non-format log messages#1334
sueun-dev wants to merge 1 commit into
getsentry:masterfrom
sueun-dev:fix-log-literal-percent

Conversation

@sueun-dev

Copy link
Copy Markdown

The structured logger formats every log body with fmt.Sprintf(message, args...) in (*sentryLogger).log, including the non-format methods. Emit builds its message with fmt.Sprint(args...) and calls log with no remaining args, and Write goes through Emit, so a literal % in those messages is treated as a format verb.

l.Info().Emit("disk usage at 95% capacity")
// Body: "disk usage at 95%!c(MISSING)apacity"

This also affects messages routed through the logging integrations, which forward the user's message via Emit(...) with no args (e.g. slog, logrus, zap).

Only run fmt.Sprintf when there are args; otherwise use the message as-is. The same resolved body is used for the debug logger output.

Added Test_sentryLogger_EmitPreservesLiteralPercent covering Emit, Write, and a message with format-like verbs. It fails before the change and passes after.

Tested: go test -race ., go vet ., gofmt -s, and golangci-lint run ./ all clean.

The structured logger built every log body with fmt.Sprintf(message, args...)
in (*sentryLogger).log. Emit is the non-format method and calls log() with no
trailing args, and Write goes through Emit, so a literal % in those messages was
treated as a format verb and corrupted (e.g. Emit("disk usage at 95% capacity")
produced "disk usage at 95%!c(MISSING)apacity").

Only run fmt.Sprintf when args are present; otherwise use the message as-is. The
resolved body is also used for the debug logger output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


Bug Fixes 🐛

  • Preserve literal '%' in non-format log messages by sueun-dev in #1334

🤖 This preview updates automatically when you update the PR.

@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.16%. Comparing base (1ce3436) to head (f0d22fb).
⚠️ Report is 139 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (1ce3436) and HEAD (f0d22fb). Click for more details.

HEAD has 8 uploads less than BASE
Flag BASE (1ce3436) HEAD (f0d22fb)
9 1
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1334      +/-   ##
==========================================
- Coverage   86.04%   78.16%   -7.88%     
==========================================
  Files          62       86      +24     
  Lines        6090     8024    +1934     
==========================================
+ Hits         5240     6272    +1032     
- Misses        635     1453     +818     
- Partials      215      299      +84     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread log_test.go
Comment on lines +451 to +457
{
name: "Emit with format-like verbs",
logFunc: func(ctx context.Context, l Logger) {
l.Warn().WithCtx(ctx).Emit("got %s and %d, 100% done")
},
wantBody: "got %s and %d, 100% done",
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes more sense here, since users should escape % in Emitf cases. Also the first case already guarantees that % literals are not skipped.

Suggested change
{
name: "Emit with format-like verbs",
logFunc: func(ctx context.Context, l Logger) {
l.Warn().WithCtx(ctx).Emit("got %s and %d, 100% done")
},
wantBody: "got %s and %d, 100% done",
},
{
name: "Emit with format-like verbs",
logFunc: func(ctx context.Context, l Logger) {
l.Warn().WithCtx(ctx).Emitf("got %s and %d, 100%% done", "string", 10)
},
wantBody: "got %s and %d, 100% done",
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants