Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing date format in log4j.xml

Tags:

java

log4j

Can anyone help me out on how to change the date format in the console. Currently the date format is like this : Feb 1, 2014 8:36:02 PM. All i want to change is the date format to 24 hour format. I tried changing the format in the log4j.xml file. this is the log4j.xml file :

<?xml version="1.0" encoding="UTF-8" ?>

http://jakarta.apache.org/log4j/'>

<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
    <param name="LocationInfo" value="false"/>
    <appender-ref ref="RollFile"/>
    <appender-ref ref="CONSOLE"/>
</appender>

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="Threshold" value="INFO"/>
    <layout class="org.apache.log4j.PatternLayout">       
         <param name="ConversionPattern" value="%d{DATE} - %-5p [%c{1}] - %m%n"/>

    </layout>
</appender>

<appender name="RollFile" class="org.apache.log4j.RollingFileAppender">
    <param name="threshold" value="INFO"/>
    <param name="maxFileSize" value="10MB"/>
    <param name="File" value="RRT_Log.log"/>
    <param name="maxBackupIndex" value="20"/>
    <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{DATE} - %-5p [%c] - %m%n"/>
    </layout>
</appender>

<root>
    <appender-ref ref="RollFile"/>
    <appender-ref ref="CONSOLE"/>
</root>

like image 585
Kanak Avatar asked Dec 04 '22 07:12

Kanak


1 Answers

You can add patterns as defined here.

As per your requirement, pattern like - %d{yyyy-MM-dd HH:mm:ss} would be userful.

Documentation says the following

Pattern Example

%d{ISO8601}                   2012-11-02 14:34:02,781

%d{ISO8601_BASIC}             20121102 143402,781

%d{ABSOLUTE}                  14:34:02,781

%d{DATE}                      02 Nov 2012 14:34:02,781

%d{COMPACT}                   20121102143402781

%d{HH:mm:ss,SSS}              14:34:02,781

%d{dd MMM yyyy HH:mm:ss,SSS}  02 Nov 2012 14:34:02,781

%d{HH:mm:ss}{GMT+0}           18:34:02,781

%d{UNIX}                      1351866842

%d{UNIX_MILLIS}               1351866842781

[EDIT]

This configuration works... Add the classes to console and root appenders

<?xml version="1.0" encoding="UTF-8"?>

<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="XXX">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%c{1}] (%t) [%X] %m%n" />
    </layout>
</appender>

<logger name="XXX">
    <level value="info"/>
</logger>

<!-- Root Logger -->
<root>
    <priority value="warn" />
    <appender-ref ref="console" />
</root>

like image 111
Vinay Veluri Avatar answered Dec 19 '22 10:12

Vinay Veluri