Comments (6)
@ngocnhan-tran1996, okay. I am closing the issue then.
Note that StrSubstitutor
bug will be addressed in the newly created #2679.
from logging-log4j2.
@ngocnhan-tran1996, thanks for the report, I've submitted #2667 fixing the issue. I would really appreciate it if you can
- Checkout the
fix/2.x/JTL-prop-subs
branch - Quickly build it using
./mvnw install -DskipTests
โ seeBUILDING.adoc
for details - And test the installed
2.24.0-SNAPSHOT
artifacts in your project
from logging-log4j2.
@ngocnhan-tran1996, thanks for the report, I've submitted #2667 fixing the issue. I would really appreciate it if you can
- Checkout the
fix/2.x/JTL-prop-subs
branch- Quickly build it using
./mvnw install -DskipTests
โ seeBUILDING.adoc
for details- And test the installed
2.24.0-SNAPSHOT
artifacts in your project
Thanks for the answer, I have test on cases with below EcsLayout.json
and log4j2.xml
. I type wrong timezone value, GMT instead UTC.
File EcsLayout.json
{
"message1": {
"$resolver": "pattern",
"pattern": "${env:TZ:-%d{yyyy-MM-dd HH:mm:ss}{GMT+00}}"
},
"message2": {
"$resolver": "pattern",
"pattern": "${env:TZ:-${TIMEZONE}}"
},
"message3": {
"$resolver": "pattern",
"pattern": "${env:TZ:-%d{yyyy-MM-dd HH:mm:ss}}"
}
}
File log4j2.xml
<Properties>
<Property name="TIMEZONE">%d{yyyy-MM-dd HH:mm:ss}{GMT+03}</Property>
</Properties>
1. Environment TZ
does not exists
The result
{"message1":"2024-06-17 21:49:18,263","message2":"2024-06-17 17:49:18","message3":"2024-06-17 21:49:18"}
message2 and message3 work as my expected
2. Environment TZ
do exists
The result
{"message1":"2024-06-17 14:53}","message2":"2024-06-17 21:53","message3":"2024-06-17 21:53}"}
message2 works as my expected
I dont know why message1 and message3 have brace }
at the end
Note: GMT in my local is setting to +07:00
File test in here
https://github.com/ngocnhan-tran1996/log4j2-demo
from logging-log4j2.
@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:
{
"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
- For performance reasons, it better be executed once instead of for every log event
- 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:
<JsonTemplateLayout eventTemplateUri="classpath:MyLayout.json">
<EventTemplateAdditionalField
key="instant"
format="JSON"
value='{"$resolver": "pattern", "pattern": "${env:TZ:-%d{yyyy-MM-dd HH:mm:ss}}"}'/>
</JsonTemplateLayout>
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
andmessage3
have brace}
at the end
It is a StrSubstitutor
bug, you can also reproduce it with PatternLayout
too:
<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
from logging-log4j2.
What I find peculiar about this example is that the reporter has the default value for the time zone as a time value. Normally I would expect something like "America/.New York".
from logging-log4j2.
About time zone
Sorry, this is my bad approach for configuring time zone.
I have difference timezone bewteen local and server, and my expect is application's console will print timezone lแปke my local.
And I added config timezone like %d{yyyy-MM-dd HH:mm:ss}{GMT+03}
But I realize I were shooting myself in the foot. I just only config timezone in application instead.
About JTL and property substitution
Thanks for the advice, I will try it in my project.
I am looking forward to the log4j2 next version
from logging-log4j2.
Related Issues (20)
- [MS16] Documentation revamp 3/5 HOT 1
- [MS17] Documentation revamp 4/5
- [MS18] Documentation revamp 5/5
- Revamp the `Web Applications` page
- Implement uniform password "encryption/decryption" mechanism HOT 4
- When logging from multiple threads, an appender can be broken until app restart if it rolls over HOT 10
- A problem with an abnormal log output location. HOT 11
- After a log file is deleted by mistake, a new rollover file cannot be created.
- Fix tests using `OnStartupTriggeringPolicy`
- When obtaining the LogContext through LogManager.getContext(getClass().getClassLoader(), false, uri), the shutdownHook in the XML configuration does not take effect. HOT 3
- JSON pretty print is not working in JsonTemplateLayout latest version log4j-layout-template-json 2.23.1 HOT 10
- OutOfMemoryException because WatchManager isn't garbage collected HOT 8
- RollingFileAppender with createOnDemand=true creates directories on a path eagerly (on initialization)
- Switch `Rfc5424Layout` to use builder pattern
- ProviderUtil.lazyInit hangs indefinitely HOT 10
- The `append` option in file appender doesn't work properly HOT 4
- log4j is supporting the Openjdk21?
- `StrSubstitutor` cannot parse nested braces
- Document isLog4jAutoShutdownDisabled to enable logging during a web application listener's contextDestroyed HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logging-log4j2.