简体   繁体   English

"指定 log4j 日期的时区"

[英]Specify time zone of log4j's date

Is it possible to specify the time zone that log4j will use?是否可以指定 log4j 将使用的时区? I need the dates in the log file to be a different time zone than the application's.我需要日志文件中的日期与应用程序的时区不同。 log4j's PatternLayout<\/code> uses SimpleDateFormat<\/code> . log4j 的PatternLayout<\/code>使用SimpleDateFormat<\/code> 。 Unfortunately there doesn't appear to be a way to control SimpleDateFormat<\/code> 's time zone via the pattern string ( DateFormat<\/code> has setTimeZone<\/code> method but that doesn't help).不幸的是,似乎没有办法通过模式字符串控制SimpleDateFormat<\/code>的时区( DateFormat<\/code>有setTimeZone<\/code>方法,但这没有帮助)。

I looked at log4j's source and SimpleDateFormat<\/code> is being instiantiated in PatternParser.finalizeConverter<\/code> .我查看了 log4j 的源代码,并且SimpleDateFormat<\/code>正在PatternParser.finalizeConverter<\/code>中进行实例化。 Unfortunately there's not an easy way to get a hold of the DateFormat<\/code> to set the time zone.不幸的是,没有一种简单的方法可以获取DateFormat<\/code>来设置时区。

"

If you use the Log4J extras JAR file on your classpath, the EnhancedPatternLayout class supports this configuration option. 如果在类路径上使用Log4J extras JAR文件,则EnhancedPatternLayout类支持此配置选项。 See the Javadoc at this link . 请参阅此链接中的Javadoc。 It's handled as part of the %d pattern component like this: 它作为%d模式组件的一部分处理,如下所示:

log4j.appender.stdout.layout.ConversionPattern=%d{}{America/New_York} %p [%c] - %m%n

You can download the extras package here . 您可以在此处下载附加软件包。

My Case... must change the patternLayout to EnhancedPatternLayout. 我的案例......必须将patternLayout更改为EnhancedPatternLayout。 (log4j-1.2.17.jar) (log4j的-1.2.17.jar)

log4j.appender.logfile.layout=org.apache.log4j. log4j.appender.logfile.layout = org.apache.log4j。 EnhancedPatternLayout log4j.appender.logfile.layout.ConversionPattern=[%d{ISO8601}{GMT+9}]%-5p - %m%n EnhancedPatternLayout log4j.appender.logfile.layout.ConversionPattern = [%d {ISO8601} {GMT + 9}]% - 5p - %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

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. 最好使用{America / New_York}而不是{GMT-5}之类的东西,因为通过指定时区,如果夏令时可操作,将进行自动调整。 Specifying something like GMT-5 will simply adjust the GMT time zone by the specified amount of hours. 指定GMT-5之类的东西只会按指定的小时数调整GMT时区。

Also if you whant to dinamicaly obtein the default time zone, you can extend EnhancedPatternLayout and overwrite the method "setConversionPattern" like this: 此外,如果您想要使用默认时区,则可以扩展EnhancedPatternLayout并覆盖方法“setConversionPattern”,如下所示:

@Override
public void setConversionPattern(String conversionPattern) {
    String defaultTimeZoneId = TimeZone.getDefault().getID();
    String conversionPatternModif = conversionPattern.replaceAll(
        "\\%d\\{([^\\{\\}]*)\\}([ ]*[^\\{]*)", 
        "%d{$1}{"+defaultTimeZoneId+"}$2");

    super.setConversionPattern(conversionPatternModif);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM