Using Log4j with JBoss 6

This tutorial has been written for a deprecated version of JBoss AS. We recommend checking the list of JBoss Logging tutorials for an updated list of articles.

In this short tutorial we will show how to use log4j on JBoss AS 6 to produce custom application logging.

In order to configure your application to use log4j on JBoss 6 you have to follow this schema:

Place log4j.xml file in a place which is visible to the application class-path. In a Web application for example, place the log4j.xml file into the “src” folder so that it will be moved into the WEB-INF/classes of your Web application.

The following file configures two appenders, a CONSOLE appender and a FILE appender which rolls logs into ${jboss.server.home.dir}/log/application.log:

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
 debug="false">
   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
      </layout>
   </appender>
   <appender name="FILE" class="org.apache.log4j.RollingFileAppender">
     <param name="File" value="${jboss.server.home.dir}/log/application.log" />
     <param name="MaxFileSize" value="1MB" />
     <param name="MaxBackupIndex" value="100" />


     <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
     </layout>
   </appender>


   <root>

       <priority value="info" />
       <appender-ref ref="CONSOLE" />
       <appender-ref ref="FILE" />
     </root>

</log4j:configuration>

Place log4j.jar library into the WEB-INF/lib (or into the EAR’s “lib” folder). This step is essential otherwise JBoss 6 will revert to its native logging implementation.

This is a sample Eclipse Project view which uses log4j:

log4j jboss 6 tutorial

Found the article helpful? if so please follow us on Socials