View RSS Feed

My Java Tips

JBoss - Logging Service

Rate this Entry
by , 11-06-2011 at 05:13 PM (1644 Views)
Jboss uses Apache logging application called log4j for logging. You can find log4j.jar under lib director. The logging service is enabled from jboss.service.xml.


The file will read like this:

<!-- ================================================== ================== -->
<!-- Log4j Initialization -->
<!-- ================================================== ================== -->

<mbean code="org.jboss.logging.Log4jService"> name="jboss.system:type=Log4jService,service=Loggi ng"
xmbean-dd="resource:xmdesc/Log4jService-xmbean.xml"&gt;
<attribute name="ConfigurationURL">resource:log4j.xml</attribute>
<!-- Set the org.apache.log4j.helpers.LogLog.setQuiteMode. As of log4j1.2.8
this needs to be set to avoid a possible deadlock on exception at the
appender level. See bug#696819.
-->
<attribute name="Log4jQuietMode">true</attribute>
<!-- How frequently in seconds the ConfigurationURL is checked for changes -->
<attribute name="RefreshPeriod">60</attribute>
</mbean>

Logging is controlled from a central conf/log4j.xml file. This file defines a set of appenders, specifying the log files, what categories of messages should go there, the message format and the level of filtering. By default, JBoss produces output to both the console and a log file (server.log in the log directory). You may change the log file names/path, logging levels from here.

In case you are new to log4J, let me tell you that there are 4 basic log levels used which are: DEBUG, INFO, WARN and ERROR. You should know that only INFO, WARN and ERROR messages will be shown if you are logging on the console. On file, you may even see DEBUG log messages. So remember that if you donít find any useful info on the console, then read the log files to learn that actually happened.




Lets take an example:

<!-- Limit JBoss categories to INFO -->
<category name="org.jboss">
<priority value="INFO"></priority>
</category>
This means that logging level is set to INFO for all JBoss classes, apart from those which have more specific overrides provided. DEBUG will produce detailed output but remember, logging means disk write operations and it takes time and resources.

Let take an interesting example. We want to set the output from the container-managed persistence engine to DEBUG level and to redirect it to a separate file, mylog.log. Then code in log4j.xml file should read like this:

<!--more-->

<appender name="CMP" class="org.jboss.logging.appender.RollingFileAppen der">
<errorhandler class="org.jboss.logging.util.OnlyOnceErrorHandler ">
<param name="File" value="${jboss.server.home.dir}/log/mylog.log"></param>
<param name="Append" value="false"></param>
<param name="MaxFileSize" value="500KB"></param>
<param name="MaxBackupIndex" value="1"></param>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"></param>
</layout>
</errorhandler></appender>
<category name="org.jboss.ejb.plugins.cmp">
<priority value="DEBUG"></priority>
<appender-ref ref="CMP">
</appender-ref></category>
The code above creates a new file appender and specifies that it should be used by the logger for the package org.jboss.ejb.plugins.cmp. The file appender (shown above) is set up to produce a new log file every day rather than producing a new one every time you restart
the server or writing to a single file indefinitely. Older files have the date they
were written added to the name.

Submit "JBoss - Logging Service" to Facebook Submit "JBoss - Logging Service" to Digg Submit "JBoss - Logging Service" to del.icio.us Submit "JBoss - Logging Service" to StumbleUpon Submit "JBoss - Logging Service" to Google

Tags: None Add / Edit Tags
Categories
JBoss , XML

Comments