Repository navigation
Allow a PatternLayout maximum field width to truncate from the end - #846
Merged
swebb2066 merged 2 commits intoNov 30, 2026
Merged
Conversation
swebb2066
reviewed
Nov 29, 2026
| * @param leftTruncate if true, characters are removed from the beginning | ||
| * of a value longer than \c maxLength, otherwise they are removed from the end. | ||
| */ | ||
| FormattingInfo( |
Contributor
There was a problem hiding this comment.
Please make this the only constructor, by using leftTruncate = true as the default value.
The next release is an ABI-breaking release, so method removal is OK at this time.
Contributor
Author
There was a problem hiding this comment.
Done in 178eab6: the three parameter constructor is gone and leftTruncate now defaults to true, so existing three argument calls still truncate from the beginning.
A maximum field width (for example %.30c) removes the extra characters from the beginning of the value. That suits a logger name, but not a message or a level name: %.100m keeps the last 100 characters of a long message and drops its start, which is usually the most important part, and there is no way to output only the first character of the level. Support a minus sign right after the period of the maximum field width, as in Log4j 2. With it, the extra characters are removed from the end: %.-1p outputs the first character of the level name and %.-200m outputs at most the first 200 characters of the message. The minimum field width and left justification flag work as before (e.g. %-20.-30c). FormattingInfo has a new constructor with a leftTruncate parameter and a new isLeftTruncated() method. The existing constructor truncates from the beginning, so existing patterns produce the same output. Previously a minus sign after the period was a pattern error, so no valid pattern changes meaning. The message, NDC and single value MDC converters copy only the part of a long value that fits the maximum field width, so they now copy the start of the value when truncating from the end. The PatternLayout format modifier documentation describes the new modifier and has examples of it.
Replace the separate three and four parameter FormattingInfo constructors with a single constructor whose leftTruncate parameter defaults to true. Existing callers that pass three arguments keep truncating from the beginning of the value.
NopeCharon
force-pushed
the
truncate_from_end_format_modifier
branch
from
November 29, 2026 04:50
bcea953 to
178eab6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A PatternLayout maximum field width (for example
%.30c) removes the extra characters from the beginning of a value. That suits a logger name, but not a message or a level name:%.100mkeeps the last 100 characters of a long message and drops its start, which is usually the most important part, and there is no way to output only the first character of the level. A minus sign after the period is currently a pattern error.Change
As in Log4j 2, a minus sign right after the period of the maximum field width removes the extra characters from the end instead:
%.-1pWARNW%.-6ma long messagea long%.6m(unchanged)a long messageessage%-16.-20ma long messagea long messageFormattingInfoconstructor has a newleftTruncateparameter, which defaults totrue(truncate from the beginning), and there is a newisLeftTruncated()method. Existing patterns produce the same output. A second minus sign, or a minus sign that is not followed by a digit, is still a pattern error.%.-30cand%-20.-30c).Testing
New tests:
PatternParserTestCase::testTruncateFromEnd: truncation from the beginning (unchanged) and from the end for%c,%pand%m, with and without a minimum field widthPatternParserTestCase::testInvalidPatterns:%.--3c,%.-cand%6.-6666care still output as literal textPatternParserTestCase::testFormattingInfoTruncateFromEnd:FormattingInfo::adjustFieldandisLeftTruncated()PatternLayoutTest::testTruncateFromEnd: message and NDC values formatted by aPatternLayoutMDCTestCase::test5: a single MDC value truncated from either endpatternparsertestcase,patternlayouttestandmdctestcasepass on Ubuntu 24.04 (GCC 13, aarch64). Without the converter changes, all three fail; for exampletestTruncateFromEndoutputs[org.fo] [I] [g 1]instead of[org.fo] [I] [msg].