Re: Do Route-keys of Routing appender of Log4j2 need to use "$$" or "$" for unresolved expressions?
"Piotr P. Karwasz" <[email protected]> Tue, 6 May 2025 15:29:14 +0200
| Newsgroups | gmane.comp.jakarta.log4j.user |
|---|---|
| Message-ID | <[email protected]> |
--------------fVj158aieEP8Vv00WMSTcVoJ
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Hi Thorsten,
On 6.05.2025 09:46, Thorsten Schöning wrote:
> I have the following two configs, which seem to behave the same:
>
> ```xml
> <Routing name="[...]">
> <Routes pattern="$${ctx:foo}">
> <Route key="true" ref="RollingFileFoo" />
> <Route key="$${ctx:foo}" ref="RollingFile" />
> </Routes>
> </Routing>
> ```
>
> vs.
>
> ```xml
> <Routing name="[...]">
> <Routes pattern="$${ctx:foo}">
> <Route key="true" ref="RollingFileFoo" />
> <Route key="${ctx:foo}" ref="RollingFile" />
> </Routes>
> </Routing>
> ```
>
> I'm a bit confused which the correct value is because both seem to work exactly the same. Which shouldn't happen if the `key` attribute implements an equals comparison to it's value.
There is subtle difference between the two configurations:
* In the first case the value of `key` will always be a literal
`${ctx:foo}`.
* In the second case Log4j Core will expand `${ctx:foo}` at
configuration time. Since the configuration of Log4j Core happens
very early in the startup sequence, `foo` will almost certainly be
undefined, so `key` will be a literal `${ctx:foo}`. In the very
unlikely case `foo` is defined at configuration time, however, the
value of `foo` will be substituted and your configuration will not
work as expected.
To see the difference, try adding:
static {
ThreadContext.put("foo", "true");
}
private static final Logger logger = LogManager.getLogger();
at the very beginning of the main class of your application.
Piotr
--------------fVj158aieEP8Vv00WMSTcVoJ--