Repository navigation
Support a negative PatternLayout logger precision - #855
Open
NopeCharon wants to merge 1 commit into
Open
NopeCharon wants to merge 1 commit into
NopeCharon wants to merge 1 commit into
Conversation
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
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.
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::getAbbreviatoronly recognised an all-digit option as a precision, so-2went to the abbreviation pattern parser, which treated the-as an ellipsis character for zero-length elements:org.apache.logging.Foo)%c{-1}-.-.-.Fooapache.logging.Foo%c{-2}-.-.-.Foologging.Foo%c{-3}-.-.-.FooFoo%c{-4}-.-.-.Fooorg.apache.logging.FooChange
getAbbreviatornow recognises a minus sign followed by digits and returns a newDropElementAbbreviator(private tonameabbreviator.cpp, likeMaxElementAbbreviator). 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-0leaves the name unchanged. A count that is out of range is reported and clamped to 256, the same way a positive precision is handled.-or-2., are parsed as before.crow 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.}giveso.a.l.Foo,%c{1.3~}giveso.apa~.log~.Foo) and lists theloggeralias. TheNameAbbreviator::getAbbreviatordescription 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
PatternLayoutTesttests:testAbbreviateDropLeadingSegments:%c{-1},%logger{-2}and%c{ -3 }, and dots in literal text before the name are left alonetestAbbreviateDropTooManySegments: 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 unchangedBoth tests fail without the change (they print
-.-.-.PatternLayoutand-.-.log4cxx).On macOS (Apple clang 21, arm64),
patternlayouttest,patternparsertestcaseandfilenamepatterntestcasepass.nameabbreviator.cppcompiles without warnings with-Wall -Wextra -Wpedantic -Wshadow -Wconversion, and also compiles with-DLOG4CXX_CHAR=wchar_tand with-DCMAKE_CXX_STANDARD=11. I checked the examples in the documentation against the output of a program linked with the changed library.