Specify time zone of log4j#39;s date(指定 log4j 日期的时区)
问题描述
是否可以指定 log4j 将使用的时区?我需要日志文件中的日期与应用程序的时区不同.log4j 的 PatternLayout
使用 SimpleDateFormat
.不幸的是,似乎没有办法通过模式字符串来控制 SimpleDateFormat
的时区(DateFormat
有 setTimeZone
方法,但这并没有'没有帮助).
Is it possible to specify the time zone that log4j will use? I need the dates in the log file to be a different time zone than the application's. log4j's PatternLayout
uses SimpleDateFormat
. Unfortunately there doesn't appear to be a way to control SimpleDateFormat
's time zone via the pattern string (DateFormat
has setTimeZone
method but that doesn't help).
我查看了 log4j 的源代码,并且 SimpleDateFormat
在 PatternParser.finalizeConverter
中被实例化.不幸的是,没有一种简单的方法可以获取 DateFormat
来设置时区.
I looked at log4j's source and SimpleDateFormat
is being instiantiated in PatternParser.finalizeConverter
. Unfortunately there's not an easy way to get a hold of the DateFormat
to set the time zone.
推荐答案
如果在类路径中使用 Log4J extras JAR 文件,EnhancedPatternLayout 类支持此配置选项.请参阅 此链接上的 Javadoc.它作为 %d 模式组件的一部分处理,如下所示:
If you use the Log4J extras JAR file on your classpath, the EnhancedPatternLayout class supports this configuration option. See the Javadoc at this link. It's handled as part of the %d pattern component like this:
log4j.appender.stdout.layout.ConversionPattern=%d{}{America/New_York} %p [%c] - %m%n
您可以在这里下载附加包.
这篇关于指定 log4j 日期的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:指定 log4j 日期的时区


- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01