mrmadou mrmadou - 1 year ago 63
Java Question

customize the output log4j

I try to customize the output of log4j, for this I use org.apache.log4j.PatternLayout.

My current output is :

2016-11-30 10:04:42,928 INFO [org.mobicents.smsc.library.CdrGenerator] OrangeSmsc,18004,null,null,null,id:0000018004 sub:00,,21

I want it to look like this:

2016-11-30 10:04:42 OrangeSmsc,18004,null,null,null,id:0000018004 sub:00,,21

For this I use this pattern:

<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd} %m%n"/>

but it doesn't work.

The full appender description:

<appender name="CDR" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.home.dir}/log/cdr.log"/>
<param name="Append" value="true"/>
<param name="Threshold" value="INFO"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd} %m%n"/>

Answer Source

I was able to run the log4j log with the pattern you mentioned. There is no issue on that. Only thing is it doesn't work until I comment the following line

<param name="DatePattern" value="'.'yyyy-MM-dd" />

This is the final appender looks like. (Please note that I have removed the jboss logger error handler)

<appender name="CDR" class="org.apache.log4j.RollingFileAppender">
    <!-- <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler" /> -->
    <param name="File" value="cdr.log" />
    <param name="Append" value="true" />
    <param name="Threshold" value="DEBUG" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" /> -->
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd} %m%n" />

This is the log I put on java class.

logger.debug("Log4j appender configuration is successful !!");

This is the addepter I used and following is the output file content.

2016-11-30 Log4j appender configuration is successful !!
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download