Using your own instance of log4j, in your WAR, on JBoss AS 7.x

This article covers the following:

  • Using your own instance of log4j instead of the one included with JBoss
  • Using your own log4j.properties file
  • Placing your logs in the JBoss AS logs directory

Introduction

While developing my web application, I needed to use my own instance of log4j on my JBoss Application Server 7.1.1 Final.

I was eventually able to do this specifically for a WAR deployment. However, it took me a while to put all the information together from multiple sources (cited below). Hence this article covering the topic specifically…

Steps

By default, JBoss uses its own instance of log4j and its own log4j.properties file for a deployed application. Hence you need to do the following in order to use your own instance of log4j in your WAR.

  1. Create an XML file named jboss-deployment-structure.xml containing the following
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
  <deployment>
    <exclusions>
      <module name="org.apache.log4j" />
    </exclusions>
  </deployment>
</jboss-deployment-structure>

This file, when deployed with your application, configures JBoss NOT to use its own instance of log4j, for your application.

This file must be placed in the ~/WEB-INF directory in your WAR

  1. Create your log4j.properties file containing your logging properties, e.g.,
log4j.rootLogger=DEBUG, RootFileAppender, RootConsoleAppender
log4j.logger.com.ankitagarwal = DEBUG, PrivateLog4JOnJBossFileAppender
 
log4j.appender.RootConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.RootConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootConsoleAppender.layout.ConversionPattern=
Date: %d{dd MMM yyyy HH:mm:ss,SSS}\nSeverity: %p\nCategory: %c\nClass: %C [%M - %L]\nMessage: %m%n\n
 
log4j.appender.RootFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.RootFileAppender.File=${jboss.server.log.dir}/root.log
log4j.appender.RootFileAppender.maxBackupIndex=10
log4j.appender.RootFileAppender.maxFileSize=10MB
log4j.appender.RootFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootFileAppender.layout.ConversionPattern=
Date: %d{dd MMM yyyy HH:mm:ss,SSS}\nSeverity: %p\nCategory: %c\nClass: %C [%M - %L]\nMessage: %m%n\n
 
log4j.appender.PrivateLog4JOnJBossFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.PrivateLog4JOnJBossFileAppender.File=${jboss.server.log.dir}/PrivateLog4JOnJBoss.log
log4j.appender.PrivateLog4JOnJBossFileAppender.maxBackupIndex=10
log4j.appender.PrivateLog4JOnJBossFileAppender.maxFileSize=10MB
log4j.appender.PrivateLog4JOnJBossFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.PrivateLog4JOnJBossFileAppender.layout.ConversionPattern=
Date: %d{dd MMM yyyy HH:mm:ss,SSS}\nSeverity: %p\nCategory: %c\nClass: %C [%M - %L]\nMessage: %m%n\n

Notice the bold lines. The use of ${jboss.server.log.dir} substitutes the system property value that is the logs directory in JBoss. This system property is set by the JBoss server at startup and hence log4j is able to substitute it.

You can also set this programmatically during your application runtime, e.g., if you need to allow your users to change this. I will cover this in a future article as it took multiple references to get this done too.

This file must be placed in the ~/WEB-INF/classes directory in your WAR.

  1. Include the log4j.jar (log4j-1.2.16.jar in my case) in your WAR.

Place the file in the ~/WEB-INF/lib directory in your WAR.

That’s it! Deploy your WAR and JBoss will log your application’s logs using your own instance of log4j.

Test the Application

I have included an example below that includes the complete Eclipse project with the ANT build file, and the created WAR for deployment on JBoss.

NOTE: The ANT build file auto-deploys the WAR to JBoss. If you don’t want to do this, then don’t run the deploy task.

If you choose to build the example application yourself, then you will have to modify the following properties in the build.xml file depending on your system setup:

  • project.dir
  • jre.lib.dir
  • servlet.lib.dir
  • web.dir
  • jboss.deployment.dir

Source Code ZIP (includes complete Eclipse project and WAR file):

If you’ve installed JBoss AS 7.x using the default settings, then you should be able to execute the test Servlet from this URL: http://localhost:8080/log4jonjboss/Log4JOnJBoss

You will see two log files PrivateLog4JOnJBoss.log and root.log in the JBoss AS logs directory, e.g., C:\Program Files\JBoss\jboss-as-7.1.1.Final\standalone\log\

The logs will also be written to the console from where you run JBoss.

WAR Structure Reference

  • META-INF/
    • ...
  • WEB-INF/
    • classes/
      • com/
        • ...
      • log4j.properties
    • lib/
      • log4j-1.2.16.jar
    • jboss-deployment-structure.xml
    • web.xml (optional in the Servlet 3.0 standard / JBoss 7.x)
  • Index.html

The following articles helped me through this…