Skip to content

fix: tracelog truncation makes output longer than original#2588

Closed
xbrxr03 wants to merge 1 commit into
jackc:masterfrom
xbrxr03:fix/tracelog-truncation
Closed

fix: tracelog truncation makes output longer than original#2588
xbrxr03 wants to merge 1 commit into
jackc:masterfrom
xbrxr03:fix/tracelog-truncation

Conversation

@xbrxr03

@xbrxr03 xbrxr03 commented Jun 22, 2026

Copy link
Copy Markdown

Problem

logQueryArgs truncates long strings and byte slices, but the truncation suffix makes the output longer than the original value:

  • A 65-byte string truncated to 64 bytes + " (truncated 1 bytes)" = 84 bytes — 29 bytes longer than the original
  • A 65-byte slice shows 128 hex chars + " (truncated 1 bytes)" = 150+ chars — much longer than the 130-char full hex encoding

Reported in #2095.

Fix

Replace the verbose fmt.Sprintf suffix with an ellipsis () and ensure the truncated result is always strictly shorter than the original:

  • []byte: Show first 60 bytes as hex (120 chars) + = 123 chars, always shorter than the 130+ char full hex
  • string: Truncate at a rune boundary so that len(result) < len(original), using maxCut = len(v) - 4 to account for the 3-byte ellipsis

Also fixed the off-by-one: []byte threshold was < 64 (excluding 64-byte slices) but string was > 64 (excluding 65+ byte strings). Now both use <= 64 / > 64 consistently.

Tests

Added TestLogQueryArgsTruncation with 5 subtests:

  • Short strings and bytes pass through unchanged
  • Long strings truncate with and are strictly shorter than the original
  • Long bytes truncate with and are shorter than full hex
  • UTF-8 strings truncate at rune boundaries

Fixes #2095

logQueryArgs used fmt.Sprintf with a verbose suffix like '(truncated N
bytes)' that could make the output longer than the original value:
- A 65-byte string truncated to 64 bytes + ' (truncated 1 bytes)' = 84
  bytes (29 bytes longer than original)
- A 65-byte slice showed 128 hex chars + suffix = 150+ chars

Replace the verbose suffix with an ellipsis (…) and ensure the truncated
result is always strictly shorter than the original:
- []byte: show first 60 bytes as hex (120 chars) + '…'
- string: truncate at a rune boundary so that len(result) < len(original)

Fixes jackc#2095
@xbrxr03

xbrxr03 commented Jun 29, 2026

Copy link
Copy Markdown
Author

Closing this PR due to lack of maintainer engagement. I'm happy to reopen if there's interest. Thanks for the great project.

@xbrxr03 xbrxr03 closed this Jun 29, 2026
@jackc

jackc commented Jun 29, 2026

Copy link
Copy Markdown
Owner

Closing this PR due to lack of maintainer engagement. I'm happy to reopen if there's interest. Thanks for the great project.

🤷‍♂️

It's a non-commercial project. I maintain it on nights and weekends. So if it's not an emergency it may take a few weeks for me to take a look.

As far as this particular issue though, if anything is to be done, I would see it as two changes.

  1. Add a field to TraceLogConfig that controls the maximum length of the value before truncation. This would also resolve Feature Request: Remove Truncation from Args When Logging #998.
  2. Retain the number of bytes truncated notification. Knowing a query had 200 byte argument vs. a 200 MB argument is important information.

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.

Tracelog truncation can make output longer.

3 participants