Do Route-keys of Routing appender of Log4j2 need to use "$$" or "$" for unresolved expressions?

Thorsten Schöning <[email protected]> Tue, 6 May 2025 07:46:51 +0000
Newsgroups gmane.comp.jakarta.log4j.user
Message-ID <PR3P190MB09850D480487749E1B78BE18E2892@PR3P190MB0985.EURP190.PROD.OUTLOOK.COM>
Hi everyone,=0A=
=0A=
the docs contain the following warning, with the emphasizes taken from the =
official docs:=0A=
=0A=
> Lookups in the **children** of the Route component are not evaluated at c=
onfiguration time. The substitution is delayed until the Route element is e=
valuated. This means that ${...} expression **should not** be escaped as $$=
{...}.=0A=
=0A=
https://logging.apache.org/log4j/2.x/manual/appenders/delegating.html#Route=
=0A=
=0A=
I have the following two configs, which seem to behave the same:=0A=
=0A=
```xml=0A=
<Routing name=3D"[...]">=0A=
	<Routes pattern=3D"$${ctx:foo}">=0A=
		<Route key=3D"true"        ref=3D"RollingFileFoo" />=0A=
		<Route key=3D"$${ctx:foo}" ref=3D"RollingFile"    />=0A=
	</Routes>=0A=
</Routing>=0A=
```=0A=
=0A=
vs.=0A=
=0A=
```xml=0A=
<Routing name=3D"[...]">=0A=
	<Routes pattern=3D"$${ctx:foo}">=0A=
		<Route key=3D"true"       ref=3D"RollingFileFoo" />=0A=
		<Route key=3D"${ctx:foo}" ref=3D"RollingFile"    />=0A=
	</Routes>=0A=
</Routing>=0A=
```=0A=
=0A=
The behaviour from my understanding is that `pattern` gets an expression co=
nfigured and that expression is interpolated at config time, why `$$` is ne=
eded for escaping reasons. So at runtime the pattern actually is `${ctx:foo=
}` and evaluated for each and every log statement passing the appender.=0A=
=0A=
Step 2: If the expression doesn't find a key of the given name `foo`, then =
the result of the pattern is kept as the pattern itself. That's why there's=
 an additional `key` with the pattern itself as a value to match against. A=
nd this is where things become interesting:=0A=
=0A=
**Is that result now expected to be with `$$` like configured in the file o=
r `$` only like taken at runtime?**=0A=
=0A=
I'm a bit confused which the correct value is because both seem to work exa=
ctly the same. Which shouldn't happen if the `key` attribute implements an =
equals comparison to it's value.=0A=
=0A=
```java=0A=
    public Route getRoute(final String key) {=0A=
        for (final Route route : routes) {=0A=
            if (Objects.equals(route.getKey(), key)) {=0A=
                return route;=0A=
            }=0A=
        }=0A=
        return null;=0A=
    }=0A=
```=0A=
=0A=
https://github.com/apache/logging-log4j2/blob/9176b44193b107b1a6c1ca9efda22=
f79fa20db3a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender=
/routing/Routes.java#L198C1-L205C6=0A=
=0A=
From my understanding, the escaping using `$$` in `pattern` is necessary be=
cause that pattern otherwise gets resolved already at config time, but it n=
eeds to be forwarded into runtime. If the difference is that `key` is simpl=
y NEVER resolved at all at config time and really only taken as string as-i=
s, then I would have expected `$$` to fail, because that value is not avail=
able at runtime?=0A=
=0A=
Thanks for your help!=0A=
=0A=
PS: Kennen Sie schon die VEDA Care Services?=A0Interesse? Dann sprechen Sie=
 uns gerne pers=F6nlich an. Weitere Infos finden Sie hier.=0A=
____________________________________________________________=0A=
Thorsten Sch=F6ning=0A=
Software Developer=0A=
=0A=
VEDA GmbH=0A=
Carl-Zeiss-Stra=DFe 14=0A=
52477 Alsdorf=0A=
Tel.: +49 2404 5507-1345=0A=
[email protected]=0A=
www.veda.net=0A=
VEDA GmbH, Amtsgericht Aachen HRB 1501=0A=
Gesch=E4ftsf=FChrer: Dr. Ralf Gr=E4=DFler=0A=
Sitz der Gesellschaft: Alsdorf=