tags:

views:

36

answers:

3

Hi,

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

A: 

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.

Thorbjørn Ravn Andersen
+2  A: 

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.

matt b
holy cows then what i have here is complete anti-pattern.
JCH
A: 

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

Meenu