Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j: timezone formatting

I would like to format the timezone information in log4j messages in the format +hh:mm such that a complete timestamp looks like this:

2013-09-05T09:32:10.703+02:00

I know the date format specifier Z, but then the output format is +hhmm and not +hh:mm. So the colon is missing:

2013-09-10T15:55:34.123+0200

Is there any way to get what I want?

like image 656
user2772501 Avatar asked Dec 21 '22 00:12

user2772501


1 Answers

Uses the following pattern:

yyyy-MM-dd'T'HH:mm:ss.SSSXXX

In the javadoc of java.text.SimpleDateFormat, you can read that (for the X letter):

ISO 8601 Time zone: The number of pattern letters designates the format for both formatting and parsing as follows:

ISO8601TimeZone:
        OneLetterISO8601TimeZone
        TwoLetterISO8601TimeZone
        ThreeLetterISO8601TimeZone
OneLetterISO8601TimeZone:
        Sign TwoDigitHours
        Z
TwoLetterISO8601TimeZone:
        Sign TwoDigitHours Minutes
        Z
ThreeLetterISO8601TimeZone:
        Sign TwoDigitHours : Minutes
        Z

e.g.

X       -08; 
XX      -0800; 
XXX     -08:00

If your configuration file is the log4j.xml, can be as follows:

<!-- console -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <param name="threshold" value="TRACE" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="conversionPattern"
            value="%d{yyyy-MM-dd'T'hh:mm:ss.SSSXXX} %-5p (%c.java:%L).%M - %m%n" />
    </layout>
</appender>

And if you have a Java class with:

import org.apache.log4j.Logger;

public class Main {
    private static final Logger LOGGER = Logger.getLogger(Main.class);

    public static void main(String[] args) {
        LOGGER.info("Test");
    }
}

The output is as follows:

2013-09-12T08:08:18.532-05:00 INFO  (Main.java:8).main - Test
like image 85
Paul Vargas Avatar answered Dec 28 '22 07:12

Paul Vargas