log4j 2 configuration for writing all the logs to a file

Here is an example of a configuration which would write all the logs to a file and once a size limit has been reached it will create a history file for the logs in a folder. So let us see the xml configuration then:

<Configuration status="DEBUG" monitorInterval="30">
  <Appenders>
     <RollingRandomAccessFile name="RollingRandomAccessFile"
      fileName="${sys:catalina.home}/logs/tomcat.log"
      filePattern="${sys:catalina.home}/logs/$${date:yyyy-MM}/tomcat-%d{MM-dd-yyyy}-%i.log"
      immediateFlush="true" append="true" ignoreExceptions="false">
      <PatternLayout>
        <Pattern>%d %-5p [%t] (%F:%L) - %m%n</Pattern>
      </PatternLayout>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="15MB"/>
      </Policies>
    </RollingRandomAccessFile>
  </Appenders>
  <Loggers>
    <Root level="DEBUG" includeLocation="false">
      <AppenderRef ref="RollingRandomAccessFile"/>
    </Root>
  </Loggers>
</Configuration>

So the above configuration will create a tomcat.log file in the logs folder of the tomcat installation. And once the size of the file has reached 15mb it was create a history file. The history file will be created in the logs folder. In logs folder a subfolder for the month will be created like 2015-10. in each subfolder a file will be created like tomcat-10-12-2015-1.log.
The message can be configured with a Pattern.
The level of logging would be DEBUG.
And the monitorInterval=”30″ means that this configuration will be monitored every 30 seconds for changes.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.