Apache Log4j Registration with a specific time zone

I want the log to contain date records for a specific time zone. Is there a way to force the timezone in log4j.properties ?

Now I am using JDK 1.5, as you already know that there is a time zone error in JDK 1.5 that is removed in JDK 1.5. In the case of JDK 1.5, it defaults to GMT. I want to configure my specific timezone in Log4j.

+8
java timezone log4j
source share
6 answers

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

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

The trick is to include 'zzz' in the template, as according to the Javadoc for java.text.SimpleDateFormat ( http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html ), this code is for the time zone. Log4J uses the same rules as SimpleDateFormat.

Log4J Javadoc has more details:

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

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

+16
source share

The best way is to use Apache Extras ™ for Apache log4j ™ and replace Normal PatternLayout with org.apache.log4j.EnhancedPatternLayout by following these steps when using the properties 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 ConversionPattern to display the date in GMT format. Any time zone can be specified instead of GMT

+4
source share

You can add the following line

 log4j.appender.S.layout.ConversionPattern= %d{yyyy-MM-dd HH:mm:ss zzz}{GMT} %-5p [%t][%c:%M(%L)] %m%n 
+1
source share

Use org.apache.log4j.helpers.DateLayout as the layout class and timeZone property in it.

0
source share

Include the date argument in your ConversionPattern . From the PatternLayout document :

date -

Used to display the date of the registration event in the local time zone. To display the date in universal time, use the %utcdate . A date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %date{HH:mm:ss,fff} or %date{dd MMM yyyy HH:mm:ss,fff} . If a date format specifier is not specified, then the ISO8601 format ( Iso8601DateFormatter ) is Iso8601DateFormatter .

The date format specifier allows the same syntax as the ToString string time pattern.

For best results, it is recommended that you use the date log4net formatters. They can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" to indicate AbsoluteTimeDateFormatter , DateTimeDateFormatter and accordingly Iso8601DateFormatter . For example, %date{ISO8601} or %date{ABSOLUTE} .

These highlighted date formats perform significantly better than ToString .

0
source share

There are three steps to this:

1) Add log4j-extras dependency here

2) Set the layout in EnhancedPatternLayout: log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout (change stdout to whatever you use)

3) Add your time zone in braces after your date template log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}{IST} %-5p %c{1}:%L - %m%n (Here IST in my case)

You can refer to the list of timezone identifiers available in java here or here

0
source share

All Articles