指定 log4j 日期的时区
是否可以指定 log4j 将使用的时区?我需要日志文件中的日期与应用程序的时区不同。 log4j 的 PatternLayout
使用 SimpleDateFormat
。不幸的是,似乎没有一种方法可以通过模式字符串控制 SimpleDateFormat
的时区(DateFormat
有 setTimeZone
方法,但这并没有没有帮助)。
我查看了 log4j 的源代码,并且 SimpleDateFormat
正在 PatternParser.finalizeConverter
中实例化。不幸的是,没有一种简单的方法可以获取 DateFormat
来设置时区。
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).
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您在类路径上使用 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:
You can download the extras package here.
我的案例...
必须将patternLayout更改为EnhancedPatternLayout。
(log4j-1.2.17.jar)
log4j.appender.logfile.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%d{ISO8601}{GMT+9}]%-5p - %m%n
My Case...
must change the patternLayout to EnhancedPatternLayout.
(log4j-1.2.17.jar)
log4j.appender.logfile.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%d{ISO8601}{GMT+9}]%-5p - %m%n
上面的日志模式有正确的想法,但不完全正确(您在日志中没有得到任何时间戳)。
请务必使用此模式:
%d{ISO8601}{美国/纽约} %p [%c] - %m%n
或
%d{ISO8601}{GMT-5} %p [%c] - %m%n
The log pattern above has right idea but not fully correct (you don't get any timestamp in log).
Use this pattern for sure:
%d{ISO8601}{America/New_York} %p [%c] - %m%n
or
%d{ISO8601}{GMT-5} %p [%c] - %m%n
最好使用 {America/New_York} 而不是 {GMT-5},因为通过指定时区,如果夏令时有效,将会自动进行调整。指定诸如 GMT-5 之类的内容只会将 GMT 时区调整指定的小时数。
It is preferable to use something like {America/New_York} rather than {GMT-5} because by specifying a timezone an automatic adjustment will be made if daylight savings is operational. Specifying something like GMT-5 will simply adjust the GMT time zone by the specified amount of hours.
另外,如果您想动态获取默认时区,您可以扩展EnhancedPatternLayout并覆盖方法“setConversionPattern”,如下所示:
Also if you whant to dinamicaly obtein the default time zone, you can extend EnhancedPatternLayout and overwrite the method "setConversionPattern" like this:
只需将大括号括起来的时区附加到 日期中的 %d模式。
例如 %d{DEFAULT}{America/New_York} 而不是 %d{DEFAULT}
(在 log4j 2 中,您可以使用常规 PatternLayout。自 1.2.16 起,在 log4j 1 中,包含并推荐使用EnhancedPatternLayout。)
Just append the timezone surrounded by curly braces to %d in your date pattern.
E.g. %d{DEFAULT}{America/New_York} instead of %d{DEFAULT}
(In log4j 2 you can use regular PatternLayout. In log4j 1 since 1.2.16, EnhancedPatternLayout is included and recommended.)