log4j in jboss
log4j is a superb, easy-to-use logging mechanism that is widely used, however, its usage with JBoss is
somewhat problematic because JBoss itself uses log4j for logging. If you try and use log4j in a traditional
manner, i.e., define a log4j.properties file and try and configure log4j by using this property file you
will find that your logging will endup in the JBoss server log and you will get a few stack traces in
the log for your trouble, whilist your own application log file wil l remain uncreated. The problem is
caused by the class loader not being able to distinguish between your incarnation of log4j versus that
being used by JBoss.
There are several ways to use log4j with JBoss and we will eventually discuss them all here but in this
article we will discuss the easy option which involves sharing lo4j.xml with JBoss and defining an Appender
for your application logfile.
1. Edit server/default/all/minimal/conf/log4j.xml. Add the following snippet:
Replace TechiLog and Techie.log with your own names.
2. Add the category name:
TechieLog is your appender described in step 1 above. com.techie is the name you will use in your application
to configure the logger. Change INFO to the level you want, i.e. DEBUG. If you leave the priority token
out it will default to DEBUG.
That is it. Restart JBoss and now here is the way to log from your application:
private static final Logger logger = Logger.getLogger("com.techie");
public DBUtil(String jndiName)
{
ds = null;
conn = null;
stmt = null;
rs = null;
ctx = null;
this.jndiName = jndiName;
pstmt = null;
logger.info("DbUtil class Created");
}
This is the easy option. In another article we will show you how to sepearte the class loader problem
and to describe your own log4j.properties.
Labels: jboss log4j


0 Comments:
Post a Comment
<< Home