Monday, May 23, 2011

A Guide to Log4j in JBoss

Share it Please

Every application needs to log various levels of information when the application is running. We can do this either through the application or also by server to do the logging. In this article we will see how we can work with log4j in JBoss. We use JBoss 5 as our server.
The file for configuring the log4j in JBoss is available in JBOSS_HOME\server\<Server Instance>\conf\jboss-log4j.xml.the file defines a set of appender specifying the log files ,the category of messages to go to that file, format of the message and level of filtering.
This is an excerpt from the file
<log4j:configuration xmlns:log4j="" debug="false">
   <appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="File" value="${jboss.server.log.dir}/server.log"/>
      <param name="Append" value="true"/>
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
      <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
      <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
      <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>

<category name="org.apache">
      <priority value="INFO"/>
</log4j: configuration>

As we can the configuration is split into 2 parts, appender and category.

Appender: This is the actual class file which does the logging, whether it may be to a console of a file depending on the destination that we choose.

JBoss provides two appender, Console and File. The logging Threshold in JBoss is INFO which means we can see information messages, warning messages and error messages on the console. If we want to change the default logging level on the log4j, we can use
-Djboss.server.log.threshold=WARN with the startup command

Category: category is the one which helps in connecting the classes (classes from a package) to the appender to perform the actual logging.

Logging Level: Various levels of logging levels are available including FATEL, ERROR, INFO, DEBUG, TRACE and WARN. We can use any type of logging level depending on our requirement.

Faster Appender
The File appender is said to be the faster than console appender, since writing to a console takes log of OS activity. So it is important to disable logging to a console in production and use File logging.

JBoss uses org.jboss.logging.Log4jService MBean to handle Log4j configuration. The MBean is available in JBOSS_HOME\server\<Server Instance> \conf\jboss-service.xml.This is how the MBean looks

<mbean code="org.jboss.logging.Log4jService"
      <attribute name="ConfigurationURL">resource:jboss-log4j.xml</attribute>
      <attribute name="Log4jQuietMode">true</attribute>
      <attribute name="RefreshPeriod">60</attribute>
      <attribute name="DefaultJBossServerLogThreshold">INFO</attribute>

ConfigurationURL: URL of the log4J configuration file.
RefreshPeriod:the time for checking the file specified by the configurationURL for any changes. The default is 60 seconds.

Now let see how we can configure logging for our application. I wrote a sample web application which contains a sample Servlet that does the logging. Once we develop the application we need to make some changes to the jboss-log4j.xml file that meets our requirement.

So the first thing we need to add is the appender to the jboss-log4j.xml file like,

<appender name="MYSAMPLEAPPENDER" class="org.jboss.logging.appender.RollingFileAppender">
        <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
        <param name="File" value="C:/log/mySample.log"/>
        <param name="Append" value="false"/>
        <param name="MaxFileSize" value="500KB"/>
        <param name="MaxBackupIndex" value="1"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>

The appender was a simple one , we will use c:/log/mySample.log for all our logging purposes. We have
FILE:the destination of the log statements.
Append: append the contents
MaxFileSize:The maximum file size
MaxBackupIndex:backup when the max file size is reached

Once the Appender is configured , we need to create a category for out classes like

<category name="com.DemoSample">
        <priority value="INFO" />
        <appender-ref ref="MYSAMPLEAPPENDER"/>
We created a category for our package and the priority is the INFO. We made the category to refer the appender that we configured.

Just copy both the appender and the category and paste then in the jboss-log4j.xml file. We don’t need any restart of the server, since the changes to the file are checked for every 60 seconds.

Pattern Layout
JBoss also provides various pattern layouts for formatting the log messages, for example

<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>

This means that the current format date is first printed, then the priority of the message should be justified by 5 characters. The %C is used to output the category of the logging event. Finally the %M outputs the application supplied message along with a carriage return.

It is always better to use log4j specific date formatters.They can used by specifying ABSOLUTE and Date like

<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n"/>

More articles to Come, So Happy coding….

No comments :

Post a Comment