Skip to content

Allow a PatternLayout maximum field width to truncate from the end - #846

Merged
swebb2066 merged 2 commits into
apache:masterfrom
NopeCharon:truncate_from_end_format_modifier
Nov 30, 2026
Merged

swebb2066 merged 2 commits into
apache:masterfrom
NopeCharon:truncate_from_end_format_modifier

Conversation

@NopeCharon

@NopeCharon NopeCharon commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

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: %.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. 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:

Pattern Value Output
%.-1p WARN W
%.-6m a long message a long
%.6m (unchanged) a long message essage
%-16.-20m a long message a long message
  • The FormattingInfo constructor has a new leftTruncate parameter, which defaults to true (truncate from the beginning), and there is a new isLeftTruncated() 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.
  • The message, NDC and single value MDC converters copy only the part of a long value that fits the maximum field width. They now copy the start of the value when truncating from the end.
  • The PatternLayout format modifier documentation describes the new modifier, and its example table has two new rows (%.-30c and %-20.-30c).

Testing

New tests:

  • PatternParserTestCase::testTruncateFromEnd: truncation from the beginning (unchanged) and from the end for %c, %p and %m, with and without a minimum field width
  • PatternParserTestCase::testInvalidPatterns: %.--3c, %.-c and %6.-6666c are still output as literal text
  • PatternParserTestCase::testFormattingInfoTruncateFromEnd: FormattingInfo::adjustField and isLeftTruncated()
  • PatternLayoutTest::testTruncateFromEnd: message and NDC values formatted by a PatternLayout
  • MDCTestCase::test5: a single MDC value truncated from either end

patternparsertestcase, patternlayouttest and mdctestcase pass on Ubuntu 24.04 (GCC 13, aarch64). Without the converter changes, all three fail; for example testTruncateFromEnd outputs [org.fo] [I] [g 1] instead of [org.fo] [I] [msg].

* @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(

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
NopeCharon force-pushed the truncate_from_end_format_modifier branch from bcea953 to 178eab6 Compare November 29, 2026 04:50
@swebb2066
swebb2066 merged commit 05d3080 into apache:master Nov 30, 2026
15 checks passed
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