指定log4j日期的时区 [英] Specify time zone of log4j's date

查看:243
本文介绍了指定log4j日期的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以指定log4j将使用的时区?我需要日志文件中的日期是与应用程序不同的时区。 log4j的 PatternLayout 使用 SimpleDateFormat 。不幸的是,似乎没有办法通过模式字符串( DateFormat 具有控制 SimpleDateFormat 的时区

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日期的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆