log4j 时区问题
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这意味着您已将 log4j 配置为监视配置文件的更改,该配置为 不推荐用于 Web 应用(另请参阅 不推荐 com/questions/1337742/log4j-configureandwatch-spawning-thousands-of-threads">这个相关的问题)。
发生的情况是这样的
TimeZone.getDefault()
返回机器的时区我在这里建议一些选项:
TimeZone getMyAppDefaultTimeZone()
。将 JVM 范围内的默认值更改为仅您的应用程序代码需要的内容可能会产生更大的意外后果。
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
TimeZone.getDefault()
returns the machine's timezoneI'd suggest a few options here:
TimeZone getMyAppDefaultTimeZone()
.Changing JVM-wide defaults for something only your application code needs can have much larger unintended consequences.
获取您正在使用的 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.
如果您使用的是log4cxx 0.9.7,那么默认时区将为GMT。获取服务器的时区并将以下行添加到配置中以打印香港时区(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)