log4j 时区问题

发布于 2024-09-08 21:24:52 字数 263 浏览 1 评论 0原文

我有一个 Web 应用程序,它在启动时将服务器本地时区存储在全局静态成员中,然后将 JVM 的 DefaultTimeZone 设置为 GMT。

在启动期间,log4j 正在服务器默认时区中记录消息。

现在,当我更改 log4j.properties 中的日志级别时,日志级别会动态更改,现在 log4j 以 GMT 时间记录消息。

我的要求是始终在服务器本地时区记录消息。

在我的场景中如何实现这一目标?任何指示将不胜感激。

谢谢

I have a web app which upon startup stores the server local timezone in a global static member and then sets the DefaultTimeZone of JVM to GMT.

During startup log4j is logging messages in server default timezone.

Now when i change my log level in log4j.properties the log level is changed dynamically and now the log4j logs messages in GMT time .

My requirement is to log messages always in server local time zone.

How can i achieve this in my scenario? any pointers will be really appreciated.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

枉心 2024-09-15 21:24:52

现在,当我更改 log4j.properties 中的日志级别时,日志级别会动态更改

这意味着您已将 log4j 配置为监视配置文件的更改,该配置为 不推荐用于 Web 应用(另请参阅 不推荐 com/questions/1337742/log4j-configureandwatch-spawning-thousands-of-threads">这个相关的问题)。

发生的情况是这样的

  1. 您的应用程序启动,log4j 被初始化,TimeZone.getDefault() 返回机器的时区
  2. 您更改了 JVM 范围内的默认时区
  3. 您对 log4j 配置进行了更改, log4j 观察程序线程注意到更改,重新初始化记录器层次结构,看到新的默认时区

我在这里建议一些选项:

  • 不要在 servlet 环境中使用 configureAndWatch
  • 不要更改 JVM 范围内的默认时区,因为这可能会影响许多不相关的组件(例如 log4j,如您所见)。相反,如果您的代码需要输出 GMT 日期,请确保所有代码都通过相同的代码路径来获取您要使用的默认区域,例如,确保所有与时间相关的代码调用类似TimeZone getMyAppDefaultTimeZone()

将 JVM 范围内的默认值更改为仅您的应用程序代码需要的内容可能会产生更大的意外后果。

Now when i change my log level in log4j.properties the log level is changed dynamically

This implies that you've configured log4j to watch your configuration file for changes, a configuration which is not recommended for webapps (see also this related SO question).

What is occuring is something like this

  1. Your app starts up, log4j is initialized, TimeZone.getDefault() returns the machine's timezone
  2. You change the default Timezone JVM-wide
  3. You make a change to the log4j config, the log4j watcher thread notices the change, re-initializes the logger hierarchy, sees the new default Timezone

I'd suggest a few options here:

  • Don't use configureAndWatch in a servlet environment
  • Don't change the default TimeZone JVM-wide, as this can impact lots of unrelated components (such as log4j, as you've seen). Instead, if you have code that has a requirement to output dates in GMT, make sure all of the code goes through the same code path to obtain the default zone you want to use, for example make sure all of your time-related code calls something like TimeZone getMyAppDefaultTimeZone().

Changing JVM-wide defaults for something only your application code needs can have much larger unintended consequences.

魂ガ小子 2024-09-15 21:24:52

获取您正在使用的 log4j 版本的源代码,并研究您正在使用的 Appender,以找到您看到的时间戳的生成位置。然后找出您需要在该位置实际完成哪些代码,然后更新您的问题。

Get the source code for the log4j version you are using and investigate the Appender you are using to locate where the timestamp you see is generated. Then figure out what code you need to actually have done at that location, and then update your question.

只是偏爱你 2024-09-15 21:24:52

如果您使用的是log4cxx 0.9.7,那么默认时区将为GMT。获取服务器的时区并将以下行添加到配置中以打印香港时区(GMT+8:00)的日志

log4j.appender.R.layout.TimeZone=GMT-8:00

If you are using log4cxx 0.9.7 then default timezone will be GMT. Get timezone of server and add following line to configuration to print logs in HK timezone (GMT+8:00)

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