Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify time zone of log4j's date

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.

like image 286
Steve Kuo Avatar asked Nov 23 '09 20:11

Steve Kuo


3 Answers

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.

like image 129
khill Avatar answered Oct 16 '22 06:10

khill


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

like image 36
10bosal Avatar answered Oct 16 '22 06:10

10bosal


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

like image 4
Ivan Digital Avatar answered Oct 16 '22 06:10

Ivan Digital