Java 指定 log4j 日期的时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1785725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:39:06  来源:igfitidea点击:

Specify time zone of log4j's date

javadatetimetimezonelog4j

提问by Steve Kuo

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 PatternLayoutuses SimpleDateFormat. Unfortunately there doesn't appear to be a way to control SimpleDateFormat's time zone via the pattern string (DateFormathas setTimeZonemethod but that doesn't help).

是否可以指定 log4j 将使用的时区?我需要日志文件中的日期与应用程序的时区不同。log4j 的PatternLayout用途SimpleDateFormat。不幸的是,似乎没有办法SimpleDateFormat通过模式字符串来控制的时区(DateFormathassetTimeZone方法,但这无济于事)。

I looked at log4j's source and SimpleDateFormatis being instiantiated in PatternParser.finalizeConverter. Unfortunately there's not an easy way to get a hold of the DateFormatto set the time zone.

我查看了 log4j 的源代码并SimpleDateFormatPatternParser.finalizeConverter. 不幸的是,没有一种简单的方法DateFormat来设置时区。

采纳答案by khill

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 extras JAR 文件,则 EnhancedPatternLayout 类支持此配置选项。请参阅此链接中的 Javadoc 。它作为 %d 模式组件的一部分进行处理,如下所示:

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

You can download the extras package here.

您可以在此处下载附加包。

回答by Ivan Digital

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

上面的日志模式有正确的想法,但并不完全正确(日志中没有任何时间戳)。
确定使用此模式:
%d{ISO8601}{America/New_York} %p [%c] - %m%n

%d{ISO8601}{GMT-5} %p [%c] - %m%n

回答by 10bosal

My Case... must change the patternLayout to EnhancedPatternLayout. (log4j-1.2.17.jar)

我的案例...必须将 patternLayout 更改为 EnhancedPatternLayout。(log4j-1.2.17.jar)

log4j.appender.logfile.layout=org.apache.log4j.EnhancedPatternLayoutlog4j.appender.logfile.layout.ConversionPattern=[%d{ISO8601}{GMT+9}]%-5p - %m%n

log4j.appender.logfile.layout=org.apache.log4j。EnhancedPatternLayoutlog4j.appender.logfile.layout.ConversionPattern=[%d{ISO8601}{GMT+9}]%-5p - %m%n

回答by avallone

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{}{"+defaultTimeZoneId+"}");

    super.setConversionPattern(conversionPatternModif);
}

回答by B5A7

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.

最好使用类似 {America/New_York} 而不是 {GMT-5} 的东西,因为通过指定时区,如果夏令时生效,将进行自动调整。指定 GMT-5 之类的内容将简单地按指定的小时数调整 GMT 时区。