英文:
log4j microsecond precision
问题
我正在使用log4j将消息发送到ElasticSearch,使用Socket appender和以下PatternLayout:
<PatternLayout pattern="{"time": "%d{DEFAULT_MICROS}", "message": "%msg"}%n"/
使用%d{DEFAULT_MICROS}
来获取微秒精确的日期,根据文档的说明。
但这只会给我毫秒精度,如下所示:2023-07-27 15:13:11,848000
(最后3位数字始终为0)
这个类似的问题建议,从Java 9和log4j 2.11.0开始,我们可以获得微秒精度,但我正在使用Java 11和最新的log4j(2.20.0),运行在Windows Server 2019上,仍然只获得毫秒精度。我漏掉了什么吗?
看起来log4j仍然在使用SimpleDateFormat?我们可以让它使用更现代的时间框架吗?
英文:
I am using log4j to send messages to ElasticSearch, with a Socket appender and a PatternLayout like this:
<PatternLayout pattern="{&quot;time&quot;: &quot;%d{DEFAULT_MICROS}&quot;, &quot;message&quot;: &quot;%msg&quot;}%n"/>
using %d{DEFAULT_MICROS}
to get microsecond precise dates as per the documentation.
But this gives me only millisecond precision like so: 2023-07-27 15:13:11,848000
(the last 3 digits are always 0)
This similar question suggests that starting Java 9 and log4j 2.11.0, we can get microsecond precision, but I am using Java 11 and the latest log4j (2.20.0), running on Windows Server 2019 and getting only millisecond precision. What am I missing ?
It seems log4j is still using SimpleDateFormat? Can we get it to use the more modern time framework?
答案1
得分: 1
我被你的问题搞糊涂了,实际上看起来你正在获得微秒级精度的时间戳 848000(6 位数字)?
- 一到三位小数位(毫秒)。
- 四到六位小数位(微秒)。
- 七到九位小数位(纳秒)。
Log4j 看起来对你来说工作正常。如果你想要更高的精度,你可以切换到 DEFAULT_NANO。
Pattern Layout
PatternLayout 对于日期格式说明符 %d 有额外的选项:
%d{DEFAULT_MICROS} - 例如 2012-11-02 14:34:02,123456
%d{DEFAULT_NANOS} - 例如 2012-11-02 14:34:02,123456789
%d{ABSOLUTE_MICROS} - 例如 14:34:02,123456
%d{ABSOLUTE_NANOS} - 例如 14:34:02,123456789
%d{HH:mm:ss,nnnn} 到 %d{HH:mm:ss,nnnnnnnnn} - 例如 14:34:02,1234 到 14:34:02,123456789
%d{dd MMM yyyy HH:mm:ss,nnnn} 到 %d{dd MMM yyyy HH:mm:ss,nnnnnnnnn} - 例如 02 Nov 2012 14:34:02,1234 到 02 Nov 2012 14:34:02,123456789
英文:
I am confused by your question it actually looks like you are getting microsecond precision 848000 (6 digits)?
- One to three decimal digits (milliseconds).
- Four to six decimal digits (microseconds).
- Seven to nine decimal digits (nanoseconds).
Log4j appears to be working correctly for you. If you want more precision you could change to DEFAULT_NANO.
Pattern Layout
PatternLayout has additional options for the %d date format specifier:
%d{DEFAULT_MICROS} - e.g. 2012-11-02 14:34:02,123456
%d{DEFAULT_NANOS} - e.g. 2012-11-02 14:34:02,123456789
%d{ABSOLUTE_MICROS} - e.g. 14:34:02,123456
%d{ABSOLUTE_NANOS} - e.g. 14:34:02,123456789
%d{HH:mm:ss,nnnn} to %d{HH:mm:ss,nnnnnnnnn} - e.g. 14:34:02,1234 to
14:34:02,123456789
%d{dd MMM yyyy HH:mm:ss,nnnn} to %d{dd MMM yyyy HH:mm:ss,nnnnnnnnn} -
e.g. 02 Nov 2012 14:34:02,1234 to 02 Nov 2012 14:34:02,123456789
答案2
得分: 1
log4j-core
项目是一个多版本 JAR 文件,而在 JDK 8 和 JDK 9+ 版本中提供的唯一类是 PreciseClock
实现。JDK 8 版本基于 System.currentTimeMillis()
,具有毫秒精度。而改进后的 JDK 9+ 版本则基于 Clock.instant()
,具有更高的精度。
您应该检查您的系统是否支持多版本 JAR。失去多版本支持的最常见方式是将所有 JAR 文件合并成一个,但问题也可能来自应用程序服务器:较早的应用程序服务器(2017 年之前)在其类加载器中不支持多版本。
英文:
The log4j-core
artifact is a Multi-release JAR file and the only class provided in both a JDK 8 and JDK 9+ version is a PreciseClock
implementation. The JDK 8 version is based on System.currentTimeMillis()
and has a millisecond precision. The improved JDK 9+ version is based on Clock.instant()
and has a much better precision.
You should check if your system supports multi-release jars. The most common way to lose MR support is to shade all jars into one, but the problem can also come from the application server: older application servers (pre-2017) lack multi-release support in their classloaders.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论