->Logging setup in your EJB module for JBOSS

This topic is intended to drive how to setup logging mechanism in your EJB module for JBOSS AS 4.2 without changing any config files in your server (don’t ever try to change JBOSS config files, since this server is drives all on configuration files. You can, but you need to make sure remember what changes you made and if in case if you forget anything, your server will screwed up) . Its very simple to enable and you don’t need to add extra libraries in your application like log4j.

By default JBOSS AS 4.x has some libraries in <JBOSS_HOME>\server\default\lib which includes commons logging libraries developed by JBOSS vendor.  This JBOSS application server is based on this logging and the logging statements are storing in ${jboss.server.log.dir}/server.log file which is under <JBOSS_HOME>\server\default\log folder.

All you need to do is observe how JBOSS has setup their logging and follow the things(don’t worry this post will give you direct solution) beloww steps.

step1) Create a service xml file which should be a unique name(name it as TEST-service.xml) and service name in the file should be unique from the default names in all jboss config files, named it as “jboss.system:type=Log4jService,service=TEST-Logging”

<?xml version=”1.0″ encoding=”UTF-8″?>
<server>
<mbean code=”org.jboss.logging.Log4jService”
name=”jboss.system:type=Log4jService,service=TEST-Logging”
xmbean-dd=”resource:xmdesc/Log4jService-xmbean.xml”>
<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>

</server>

Step2:  Now you need to create the log4j.xml file, which is like common as log4j.xml file but the API is should be different, need to use jboss commons logging.

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE log4j:configuration SYSTEM “log4j.dtd” >
<log4j:configuration debug=”true” xmlns:log4j=’http://jakarta.apache.org/log4j/’&gt;

<appender name=”DEBUG”>
<param name=”File” value=”${jboss.server.log.dir}/TEST_debug.log” />
<param name=”Append” value=”true” />
<param name=”MaxFileSize” value=”500KB” />
<param name=”MaxBackupIndex” value=”5″ />
<layout>
<param name=”ConversionPattern” value=”%d %-5p [%c] %m%n” />

</layout>
<filter>
<param name=”LevelMin” value=”debug” />
<param name=”LevelMax” value=”debug” />
</filter>

</appender>

<appender name=”ERROR”>
<param name=”File” value=”${jboss.server.log.dir}/TEST_error.log” />
<param name=”Append” value=”true” />
<param name=”MaxFileSize” value=”500KB” />
<param name=”MaxBackupIndex” value=”5″ />
<layout>
<param name=”ConversionPattern”
value=”%d %-5p [%c] %m%n” />
</layout>
<filter>
<param name=”LevelMin” value=”error” />
<param name=”LevelMax” value=”error” />
</filter>
</appender>

<appender name=”INFO”>
<param name=”File” value=”${jboss.server.log.dir}/TEST_info.log” />
<param name=”Append” value=”true” />
<param name=”MaxFileSize” value=”500KB” />
<param name=”MaxBackupIndex” value=”5″ />
<layout>
<param name=”ConversionPattern”
value=”%d %-5p [%c] %m%n” />
</layout>
<filter>
<param name=”LevelMin” value=”info” />
<param name=”LevelMax” value=”info” />
</filter>
</appender>

<appender name=”TRACE”>
<param name=”File” value=”${jboss.server.log.dir}/TEST_trace.log” />
<param name=”Append” value=”true” />
<param name=”MaxFileSize” value=”500KB” />
<param name=”MaxBackupIndex” value=”5″ />
<layout>
<param name=”ConversionPattern”
value=”%d %-5p [%c] %m%n” />
</layout>
<filter>
<param name=”LevelMin” value=”trace” />
<param name=”LevelMax” value=”trace” />
</filter>
</appender>

<appender name=”WARN”>
<param name=”File” value=”${jboss.server.log.dir}/TEST_warn.log” />
<param name=”Append” value=”true” />
<param name=”MaxFileSize” value=”500KB” />
<param name=”MaxBackupIndex” value=”5″ />
<layout>
<param name=”ConversionPattern”
value=”%d %-5p [%c] %m%n” />
</layout>
<filter>
<param name=”LevelMin” value=”warn” />
<param name=”LevelMax” value=”warn” />
</filter>
</appender>

<appender name=”FATAL”>
<param name=”File” value=”${jboss.server.log.dir}/TEST_fatal.log” />
<param name=”Append” value=”true” />
<param name=”MaxFileSize” value=”500KB” />
<param name=”MaxBackupIndex” value=”5″ />
<layout>
<param name=”ConversionPattern”
value=”%d %-5p [%c] %m%n” />
</layout>
<filter>
<param name=”LevelMin” value=”fatal” />
<param name=”LevelMax” value=”fatal” />
</filter>
</appender>

<logger name=”org.andor”>
<appender-ref ref=”DEBUG” />
<appender-ref ref=”INFO” />
<appender-ref ref=”ERROR” />
<appender-ref ref=”WARN” />
<appender-ref ref=”TRACE” />
<appender-ref ref=”FATAL” />

</logger>

<logger name=”edu”>
<appender-ref ref=”DEBUG” />
<appender-ref ref=”INFO” />
<appender-ref ref=”ERROR” />
<appender-ref ref=”WARN” />
<appender-ref ref=”TRACE” />
<appender-ref ref=”FATAL” />
</logger>

</log4j:configuration>

This log4j is creates all Logging level log files (debug, info, error, warn, trace, fatal) and stores the logging statements to appropriate files. i.e. debug statements are stores in TEST-debug.log file, etc.. And these log files are locates in JBOSS log folder.

Step 3: Drop these files in ejbmodule folder (src) under the EJB project and deploy this project in JBOSS . Server will takes care logging and overdrive the actual logging.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: