You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Logger precision formats in PatternLayout do not match documentation #2522
In the docs at https://logging.apache.org/log4j/2.x/manual/layouts.html#patterns it gives an example pattern %c{1.2.*} that should transform something like org.apache.commons.test.Foo to o.a.c.test.Foo, but I'm seeing it transform into o.ap.commons.test.Foo instead.
In general, the configuration I want here is to collapse all the parts of the package name to single letters except for the last package name part before the class name which is what that example precision looks to do.
Configuration
Version: 2.23.1
Operating system: macOS
JDK: Java 21
Logs
[Stacktraces, errors, etc. relevant applications logs.]
Reproduction
Use %c{1.2.*} in a pattern layout with a logger name such as org.apache.logging.log4j.core.config.AbstractConfiguration which should output o.a.l.l.c.config.AbstractConfiguration. Sample config snippet:
I reproduced this, but I think the root cause is a bit different from what's assumed here, and it affects more patterns than just 1.2.*. Posting to confirm the intended semantics before attempting a fix, because the docs seem to be internally inconsistent here.
What actually happens
On 2.x (current main, commit 38a9240 for NameAbbreviator.java), with org.apache.commons.test.Foo:
Pattern
Actual output
Docs say
%c{1.}
o.a.c.Foo
o.a.c.Foo ✅
%c{1.1.1.*}
o.a.c.test.Foo
o.a.c.test.Foo ✅
%c{1.2.*}
o.ap.commons.test.Foo
o.a.c.test.Foo ❌
%c{1.3.*}
o.apa.commons.test.Foo
o.a.commons.test.Foo ❌
%c{1.8.*}
o.apache.commons.test.Foo
org.apache.commons.test.Foo ❌
So 1.3.* and 1.8.* are affected by exactly the same problem — I don't see them mentioned anywhere, which suggests they've been silently wrong since the pattern support was added. 1.1.1.* only looks correct by coincidence: all three counts happen to be 1.
Root cause
NameAbbreviator.getAbbreviator() (the pattern-parsing loop around line 99) treats the digit before * as a per-segment character count:
if (trimmed.charAt(pos) == '*') {
charCount = Integer.MAX_VALUE;
} elseif (trimmed.charAt(pos) >= '0' && trimmed.charAt(pos) <= '9') {
charCount = trimmed.charAt(pos) - '0'; // <-- also consumes the digit before '*'
...
}
For 1.2.* this produces the fragment list [1 char, 2 chars, keep-all], so org → o, apache → ap, and everything after that is preserved verbatim. Hence the extra p in o.ap.….
If the number after the first period is followed by an asterisk it indicates how many of the rightmost tokens will be printed in full.
Read literally, that would make %c{1.2.*} = "1 char for leading segments + the 2 rightmost tokens in full" = o.a.commons.test.Foo, i.e. the current implementation output would be wrong for 1.2.*.
But the example table in that same section lists %c{1.3.*} → o.a.commons.test.Foo, which is only correct under the "digit = char count" reading that the code implements. So the table and the prose disagree with each other, and I can't tell which one is intended.
Also worth noting: this report's expected output for 1.2.* (o.a.c.test.Foo) is actually the documented result for 1.1.1.*, so I don't want to assume which reading is being requested.
Two possible directions
Change the implementation so that N.* means "keep the N rightmost tokens in full". This would align the code with the prose, but it changes existing behavior for patterns like 1.2* and 1.3* and would break current assertions in NameAbbreviatorTest (e.g. 1.2* → o.a.l.l.c.pattern.NameAbbreviatorTest). That's a backward-incompatible change, so it would need a deprecation discussion.
Correct the documentation — fix the example table so 1.2.*/1.3.* show what the code actually produces, and clarify the prose to describe the digit-before-* semantics explicitly. Zero behavior change, and it would make the docs match the (heavily used) existing behavior.
Could you confirm which semantics is intended for %c{N.*}? I'm happy to prepare a PR either way — I just don't want to pick the breaking option unilaterally.
For reference, my analysis above is based on reading log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/NameAbbreviator.java and reproducing the parse + abbreviate logic; I haven't yet run it against a built jar.
@jvz@wylovelyi, this may be fixed: #4394 (95d4e6d) corrected the examples to %c{1.2*}, %c{1.3*} and %c{1.8*}. Could you confirm, so this can be closed?
Activity
wylovelyi commentedon Oct 8, 2026
I reproduced this, but I think the root cause is a bit different from what's assumed here, and it affects more patterns than just
1.2.*. Posting to confirm the intended semantics before attempting a fix, because the docs seem to be internally inconsistent here.What actually happens
On
2.x(currentmain, commit38a9240forNameAbbreviator.java), withorg.apache.commons.test.Foo:%c{1.}o.a.c.Fooo.a.c.Foo✅%c{1.1.1.*}o.a.c.test.Fooo.a.c.test.Foo✅%c{1.2.*}o.ap.commons.test.Fooo.a.c.test.Foo❌%c{1.3.*}o.apa.commons.test.Fooo.a.commons.test.Foo❌%c{1.8.*}o.apache.commons.test.Fooorg.apache.commons.test.Foo❌So
1.3.*and1.8.*are affected by exactly the same problem — I don't see them mentioned anywhere, which suggests they've been silently wrong since the pattern support was added.1.1.1.*only looks correct by coincidence: all three counts happen to be1.Root cause
NameAbbreviator.getAbbreviator()(the pattern-parsing loop around line 99) treats the digit before*as a per-segment character count:For
1.2.*this produces the fragment list[1 char, 2 chars, keep-all], soorg→o,apache→ap, and everything after that is preserved verbatim. Hence the extrapino.ap.….Why I'd like maintainer input before patching
The prose in https://logging.apache.org/log4j/2.x/manual/pattern-layout.html says:
Read literally, that would make
%c{1.2.*}= "1 char for leading segments + the 2 rightmost tokens in full" =o.a.commons.test.Foo, i.e. the current implementation output would be wrong for1.2.*.But the example table in that same section lists
%c{1.3.*}→o.a.commons.test.Foo, which is only correct under the "digit = char count" reading that the code implements. So the table and the prose disagree with each other, and I can't tell which one is intended.Also worth noting: this report's expected output for
1.2.*(o.a.c.test.Foo) is actually the documented result for1.1.1.*, so I don't want to assume which reading is being requested.Two possible directions
N.*means "keep the N rightmost tokens in full". This would align the code with the prose, but it changes existing behavior for patterns like1.2*and1.3*and would break current assertions inNameAbbreviatorTest(e.g.1.2*→o.a.l.l.c.pattern.NameAbbreviatorTest). That's a backward-incompatible change, so it would need a deprecation discussion.1.2.*/1.3.*show what the code actually produces, and clarify the prose to describe the digit-before-*semantics explicitly. Zero behavior change, and it would make the docs match the (heavily used) existing behavior.Could you confirm which semantics is intended for
%c{N.*}? I'm happy to prepare a PR either way — I just don't want to pick the breaking option unilaterally.For reference, my analysis above is based on reading
log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/NameAbbreviator.javaand reproducing the parse + abbreviate logic; I haven't yet run it against a built jar.Correct %c precision documentation to match the actual behaviour
Correct %c precision documentation to match the actual behaviour
docs: use %c{1.2*} in precision examples to match DynamicWordAbbrevia…
docs: use %c{1.2*} in %c precision examples so output matches the table
Correct %c precision documentation to match the actual behaviour (#4394)
ramanathan1504 commentedon Nov 7, 2026
@jvz @wylovelyi, this may be fixed: #4394 (95d4e6d) corrected the examples to
%c{1.2*},%c{1.3*}and%c{1.8*}. Could you confirm, so this can be closed?