Skip to content

Support a negative PatternLayout logger precision - #855

Open
NopeCharon wants to merge 1 commit into
apache:masterfrom
NopeCharon:negative_logger_precision
Open

NopeCharon wants to merge 1 commit into
apache:masterfrom
NopeCharon:negative_logger_precision

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

Closes #854

Problem

In the Log4j 2 PatternLayout, a negative logger precision removes that many leading elements of the logger name. Log4cxx accepted the same pattern but printed broken output. NameAbbreviator::getAbbreviator only recognised an all-digit option as a precision, so -2 went to the abbreviation pattern parser, which treated the - as an ellipsis character for zero-length elements:

Pattern (logger org.apache.logging.Foo) Before After (same as Log4j 2)
%c{-1} -.-.-.Foo apache.logging.Foo
%c{-2} -.-.-.Foo logging.Foo
%c{-3} -.-.-.Foo Foo
%c{-4} -.-.-.Foo org.apache.logging.Foo

Change

  • getAbbreviator now recognises a minus sign followed by digits and returns a new DropElementAbbreviator (private to nameabbreviator.cpp, like MaxElementAbbreviator). It removes that many leading elements, starting at the position where the name begins in the output buffer. As in Log4j 2, a name that does not have more elements than the count is left unchanged, and -0 leaves the name unchanged. A count that is out of range is reported and clamped to 256, the same way a positive precision is handled.
  • Options that are not a minus sign followed only by digits, such as - or -2., are parsed as before.
  • Documentation: the c row of the PatternLayout conversion table now describes the negative precision. It also describes the abbreviation patterns that were already supported but not documented there (%c{1.} gives o.a.l.Foo, %c{1.3~} gives o.apa~.log~.Foo) and lists the logger alias. The NameAbbreviator::getAbbreviator description mentions the negative form, and the bracket in its %logger{1~.2~} example is fixed. A change log entry is added under the 2.0.0 new features.

Testing

New PatternLayoutTest tests:

  • testAbbreviateDropLeadingSegments: %c{-1}, %logger{-2} and %c{ -3 }, and dots in literal text before the name are left alone
  • testAbbreviateDropTooManySegments: a count equal to or above the number of elements, -0, an out-of-range count, and a name without dots all give the name unchanged

Both tests fail without the change (they print -.-.-.PatternLayout and -.-.log4cxx).

On macOS (Apple clang 21, arm64), patternlayouttest, patternparsertestcase and filenamepatterntestcase pass. nameabbreviator.cpp compiles without warnings with -Wall -Wextra -Wpedantic -Wshadow -Wconversion, and also compiles with -DLOG4CXX_CHAR=wchar_t and with -DCMAKE_CXX_STANDARD=11. I checked the examples in the documentation against the output of a program linked with the changed library.

In the Log4j 2 PatternLayout a negative logger precision removes that
number of leading elements of the logger name, e.g. %c{-2} outputs
"logging.Foo" for the logger "org.apache.logging.Foo". Log4cxx accepted
such a pattern but NameAbbreviator::getAbbreviator only recognised an
all-digit option as a precision, so "-2" was parsed as an abbreviation
pattern whose '-' became the ellipsis of zero length elements and the
output was "-.-.-.Foo".

Recognise a minus sign followed by digits and use a new
DropElementAbbreviator that removes that number of leading elements. As
in Log4j 2, a name that does not have more elements than the requested
number is output unchanged, and %c{-0} outputs the full name. The same
option applies to %logger and %C, which share NameAbbreviator.

Document the negative precision in the "c" row of the PatternLayout
conversion table, together with the abbreviation patterns that were
already supported but not described there (%c{1.} outputs "o.a.l.Foo",
%c{1.3~} outputs "o.apa~.log~.Foo") and the "logger" alias. Mention the
negative precision in the NameAbbreviator::getAbbreviator description,
correcting the bracket in its "%logger{1~.2~}" example, and add a change
log entry.

PatternLayoutTest::testAbbreviateDropLeadingSegments checks -1, -2 and
-3 (with spaces around the value) and that dots in output preceding the
logger name are not affected. testAbbreviateDropTooManySegments checks
that the name is unchanged for a count equal to or above its number of
elements, for -0, for an out of range count and for a name without dots.

Fixes apache#854
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.

Support a negative PatternLayout logger precision to remove leading name elements

1 participant