->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

–>Enabling SSL on JBoss AS 4.2.x

Enabling SSL on JBoss AS 4.2.x: If you want get it in seperate document click here

In order to use the HTTPS on your application, we have to first enable JBoss AS to accept HTTP connections over SSL. This page describes the necessary steps using a self-signed certificate.

Here we are creating the self signed certificate, but the procedure would be the same even if you are going to use a certificate from a Certification Authority like Verisign Trust Network.

  1. Generate the keystore using the command below. For the most part, you can just make up stuff for the responses. However, remember the password you provide and use the same password for the keystore and the key. When it asks for your first and last name, you should enter the hostname used for JBoss AS (i.e., localhost).

a)      Either user the following command

keytool -genkey -alias serverkeys -keyalg RSA -keystore server.keystore -storepass changeit -keypass changeit -dname “CN=localhost, OU=UIS, O=GU, L=Washington, ST=DC, C=US”

From the above –dname, the names are described in details

CN : first name and last name (generally we need to give hostname used for JBoss AS)

OU : Organizational Unit

O : Organization

L : City or Locality

ST : State or Province

C : Country code for this Unit

server.keystore : is a Trusted file(self signed certificate) to store the above information and we need to place this file in the JBoss server location.

OR

b)      Use this command, and the prompt will ask you the above details mentioned in –dname

keytool -genkey -alias tc-ssl -keyalg RSA -keystore server.keystore -validity 3650

OR

keytool -genkey -keyalg RSA -keystore server.keystore -validity NUMBER_OF_DAYS

  1. Move the generated file the conf directory of the JBoss AS default domain (or the one you are using):

> mv server.keystore ${jboss.home}/server/default/conf/

  1. Open the file ${jboss.home}/server/default/deploy/jboss-web.deployer/server.xml in your editor, remove the XML comment around the SSL-connector, and modify the attributes to match the configuration shown here:

<Connector port=”443” protocol=”HTTP/1.1

SSLEnabled=”true” maxThreads=”150” scheme=”https

secure=”true” clientAuth=”false” sslProtocol=”TLS”                                           address=”${jboss.bind.address}

keystoreFile=”${jboss.server.home.dir}/conf/server.keystore

keystorePass=”changeit“/>

  1. Now you should be able to access your application through https. The URL will begin with https instead of http and you need to include the port number if the port you provided in the configuration is anything other than 443:
        https://localhost:443