指定 log4j 日期的时区

发布于 2024-08-12 01:33:27 字数 402 浏览 3 评论 0原文

是否可以指定 log4j 将使用的时区?我需要日志文件中的日期与应用程序的时区不同。 log4j 的 PatternLayout 使用 SimpleDateFormat。不幸的是,似乎没有一种方法可以通过模式字符串控制 SimpleDateFormat 的时区(DateFormatsetTimeZone 方法,但这并没有没有帮助)。

我查看了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

感受沵的脚步 2024-08-19 01:33:27

如果您在类路径上使用 Log4J extras JAR 文件,则EnhancedPatternLayout 类支持此配置选项。请参阅此链接上的 Javadoc。它作为 %d 模式组件的一部分进行处理,如下所示:

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

您可以此处下载额外包

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

You can download the extras package here.

笙痞 2024-08-19 01:33:27

我的案例...
必须将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

我的痛♀有谁懂 2024-08-19 01:33:27

上面的日志模式有正确的想法,但不完全正确(您在日志中没有得到任何时间戳)。
请务必使用此模式:
%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

场罚期间 2024-08-19 01:33:27

最好使用 {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.

再可℃爱ぅ一点好了 2024-08-19 01:33:27

另外,如果您想动态获取默认时区,您可以扩展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);
}

Also if you whant to dinamicaly obtein the default time zone, you can extend EnhancedPatternLayout and overwrite the method "setConversionPattern" like this:

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

    super.setConversionPattern(conversionPatternModif);
}
第几種人 2024-08-19 01:33:27

只需将大括号括起来的时区附加到 日期中的 %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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文