vy commented on issue #2666:
URL:
https://github.com/apache/logging-log4j2/issues/2666#issuecomment-2176884248
@ngocnhan-tran1996, I collaborated on this issue with @ppkarwasz, and I
decided to not update JTL (JSON Template Layout) to perform property
substitution on resolver configuration objects. Yet the issue you reported
about the trailing `}` is indeed a `StrSubstitutor` bug, which I will address
in another ticket.
### JTL and property substitution
JTL supports property substitution on string members of a JSON object,
granted they are not part of a resolver configuration. Let me make this clear
with an example:
```json
{
"x": "${bar}",
"y": {
"$resolver": "...",
"z": "${bar}"
}
}
```
`${bar}` for `x` will be substituted at *configuration-time* (i.e., while
compiling JTL templates), whereas `${bar}` of `z` will not be substituted,
since it is part of a *template configuration object*. In #2667, I tried
various approaches to improve the situation, but it always caused other issues.
It is important to understand property substitution gets performed at three
levels in your example:
* For literals in `log4j2.xml` (at configuration-time)
* For literals in a JTL template (at configuration-time)
* By Pattern Layout wired by `pattern` resolver (at runtime)
When you use `${env:TZ:-${TIMEZONE}}`, you actually want this to happen at
configuration-time, since
1. For performance reasons, it better be executed once instead of for every
log event
2. It accesses to a *configuration* property: `TIMEZONE`
Hence, it doesn't make sense to access to a Log4j configuration property
(`TIMEZONE`) from an external JSON file, i.e., event template provided by the
`eventTemplateUri` attribute. That said, as I tried to explain, if you would
have inlined your template into `log4j2.xml` using the `eventTemplate`
attribute, `${env:TZ:-${TIMEZONE}}` would have just worked fine.
I advise you to inline your _"JTL template fields accessing to configuration
properties"_ using [event template additional
fields](https://logging.staged.apache.org/log4j/2.x/manual/json-template-layout.html#plugin-element-EventTemplateAdditionalField):
```json
<JsonTemplateLayout eventTemplateUri="classpath:MyLayout.json">
<EventTemplateAdditionalField
key="instant"
format="JSON"
value='{"$resolver": "pattern", "pattern": "${env:TZ:-%d{yyyy-MM-dd
HH:mm:ss}}"}'/>
```
All `${foo}` in the above example will be subject to property substitution
at configuration-time.
I will repurpose #2667 to improve JTL docs on this subject.
### Notes on time zone
It is also peculiar that you need to change the time zone in your
application depending on an environment variable. Would you mind sharing a
little bit more detail about your use case, please? Where do you eventually
persist/forward these logs? Why not sticking to UTC doesn't work for you?
### Notes on trailing `}`
You stated
> I dont know why `message1` and `message3` have brace `}` at the end
It is a `StrSubstitutor` bug, you can also reproduce it with `PatternLayout`
too:
```xml
<PatternLayout pattern="${env:TZ:-%d{yyyy-MM-dd HH:mm:ss}{GMT+00}}"/>
```
I will create a separate issue for this.
### Conclusion
In short,
* We don't think JTL should be fixed
* I will update JTL docs on this subject
* You can/should use `EventTemplateAdditionalField`s
* Trailing `}` is a bug, which I will address in another ticket/PR
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]