Saturday, 17 May 2014

JBoss 6.x AS and log4j

This post is a result of spending quite a bit of time working on trying to get a custom log4j (1.2) appender to work on JBoss 6.x. The purpose of this article is not to explain how to get log4j working in a WAR/JAR/EAR, if that is what you are looking for rather have a look here, or here. If you are looking to add a log4j appender to the default JBoss server logging then carry on reading.

The first thing to be aware of is that log4j in JBoss 6.0 is broken, as is also the case in JBoss 6.1. The steps below are required to fix this and are based on clean JBoss 6.x AS installations.

1. Add and configure a log4j appender

This is done by editing the jboss-logging.xml file located in the server deployment directory (jboss-6.x.0.Final/server/default/deploy)

First add an appender named "LOG4J" (or whatever you would like to call it):

   <log4j-appender name="LOG4J" class="org.apache.log4j.FileAppender">

         <property name="file">${jboss.server.log.dir}/log4j.log</property>
         <property name="append">true</property>

         <pattern-formatter pattern="%d %-5p [%c] (%t) %m%n"/>

This configuration is for a standard log4j file appender writing to a file named "log4j.log".

Next you need to add your new appender to the root-logger section in the same file:

      <!-- Set the root logger priority via a system property, with a default value. -->
      <level name="${jboss.server.log.threshold:INFO}"/>
         <handler-ref name="CONSOLE"/>
         <handler-ref name="FILE"/>
         <handler-ref name="LOG4J"/>

If you were to start JBoss now it would be reasonable to expect a new log file named "log4j.log" to have been created in the server/default/log/ directory, unfortunately this is not the case. And instead you are given no errors by JBoss 6.0 while JBoss 6.1 spews the only slightly more useful message below for each line it should be writing to the log file:

ERROR [STDERR] log4j:ERROR No output stream or file set for the appender named [null].

2. Update log4j.jar packaged with JBoss installation

Download the latest version of the log4j 1.2 package from Apache log4j and replace the JBoss version in jboss-6.x.0/common/lib. Using 1.2.17:

cp apache-log4j-1.2.17/log4j-1.2.17.jar jboss-6.x.0.Final/common/lib/log4j.jar

3. Update the jboss-logging packages

Download the patch (zip) from the bug report and unzip it. There is unfortunately no directory structure so you will need to manually copy the replacement JAR files as follows:

cp jboss-logmanager-log4j.jar jboss-6.x.0.Final/common/lib
cp jboss-logmanager.jar jboss-6.x.0.Final/lib
cp logging-service-metadata.jar jboss-6.x.0.Final/server/default/deployers/jboss-logging.deployer

4. Start JBoss and enjoy your new logging framework