How to use DailyRollingFileAppender in log4j.xml


<appender name=”abcWebServiceDAILY_ROLLING_FILE”
class=”org.apache.log4j.DailyRollingFileAppender“>
<param name=”File” value=”/mainlog/abcWebService_mainapp.log” />
<param name=”Append” value=”true” />
<param name=”DatePattern” value=”‘.’dd-MM-yyyy” />
<layout>
<param name=”ConversionPattern”
value=”[%d{ISO8601}] %-5p %m%n” />
</layout>
</appender>

<logger name=”com.blabla.abcWebServiceLogger” additivity=”false”>
<level value=”ERROR” />
<appender-ref ref=”abcWebServiceDAILY_ROLLING_FILE” />
</logger>

If you want to define a DailyRollingFile for your project’s logs, then the configuration above will be worked.
Log files will be named and stored in the following format

abcWebService_mainapp.log.27-04-2011
abcWebService_mainapp.log.28-04-2011
abcWebService_mainapp.log.29-04-2011
abcWebService_mainapp.log –> today you can see the log file in this format

Advertisements

7 thoughts on “How to use DailyRollingFileAppender in log4j.xml

  1. How do you change the log file format to put the date before the “.log”. Like this for example: abcWebService_mainapp.29-04-2011.log

    I’ve tried using the “FileNamePattern” param but I can’t find if its supported for the DailyRollingFileAppender.

    This is an example of what I’m trying to do:

    Thanks,
    Brian

    1. Have you tried like that,

      I guess it should work as you expected and keep us informed;

      <param name=”File” value=”/mainlog/abcWebService_mainapp” />
      <param name="DatePattern" value="'.'dd-MM-yyyy'.log'"/>

      I HAVE CHANGED THE FOLLOWING LINES in the XML FILE
      –> removed dot log for File
      –> add .log at the end for DatePattern

  2. I updated my jboss-log4j.xml file last night but it still wrote my rolled over log file with the date at the end. I’m still researching and have found another param called “scheduledFilename” that I’m trying to find an example of how its used.

    This is the example I tried to use (with begin and end tag’s removed):
    param name=”FileNamePattern” value=”${jboss.server.log.dir}/server.%d.log”

    1. I suggest you to remove param name=”FileNamePattern” value=”${jboss.server.log.dir}/server.%d.log”
      inside jboss-log4j.xml file and create the following lines inside jboss-log4j.xml

      <param name=”File” value=”${jboss.server.log.dir}/server” />
      <param name=”DatePattern” value=”‘.’dd-MM-yyyy’.log'”/>

      and restart server , you may try to change system date to see instant results.
      Regards
      Tuna

      1. You are the man!

        I added the ‘.log’ to the DatePattern param and it worked perfectly. I decided to leave the .log for the File param otherwise my active log file wouldn’t have a file extension.

        Thanks again!
        Brian

  3. Try like this

    I HAVE CHANGED THE FOLLOWING LINES in the XML FILE
    –> leave the file name as it is.
    –> add .log at the end for DatePattern

    Regards
    Sharat

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s