Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Log4j Logging with specific timezone

I want the log should contain date entries of specific timezone. Is there any way of forcing timezone in log4j.properties?

Now I am using JDK 1.5, As you already know that there is timezone bug in JDK 1.5 that is removed in JDK 1.5. In case of JDK 1.5 it by default shows "GMT" timezone. I want to configure in Log4j my specific timezone.

like image 840
Sunny Gupta Avatar asked Feb 02 '12 16:02

Sunny Gupta


2 Answers

This will allow you to see timezone information in each line of your logs:

%d{yyyy-MM-dd/HH:mm:ss.SSS/zzz}

The trick is to include 'zzz' in the pattern since according to Javadoc for java.text.SimpleDateFormat ( http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html ), that's the code for timezone. Log4J uses the same rules as SimpleDateFormat.

There are more details over the in the Log4J Javadoc:

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

Look for the row in the table where the 'Conversion Character' is the letter 'd'.

like image 123
Julius Musseau Avatar answered Oct 20 '22 00:10

Julius Musseau


The best way is to use the Apache Extras™ for Apache log4j™ And replace The normal PatternLayout by org.apache.log4j.EnhancedPatternLayout doing the following if using a property file:

//log4j.appender.xxx.layout = org.apache.log4j.PatternLayout
//Replaced by
log4j.appender.xxx.layout = org.apache.log4j.EnhancedPatternLayout

Then you can use %d{ISO8601}{GMT} instead of %d in the ConversionPattern to display your date in the GMT format. Any timezone can be specified instead of GMT

like image 22
Kim D. Avatar answered Oct 20 '22 00:10

Kim D.