Using log4j for logging

You can use your own log4j.xml for logging other than the one provided by provided by jboss.
In that case you need to use log4j classes for that purpose and hence need to have log4j.jar on the classpath of your application.
I am using the following version of log4j.xml in ..server/default/conf to log to a log file called mattiz.log in ..server/default/log.



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
	debug="false">

	<!-- ================================= -->
	<!-- Preserve messages in a local file -->
	<!-- ================================= -->

	<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${jboss.server.home.dir}/log/mattiz.log" />
		<param name="Append" value="true" />
		<param name="MaxFileSize" value="100MB" />
		<param name="MaxBackupIndex" value="3" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p [%c] %m%n" />
		</layout>
	</appender>

	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
		<param name="Target" value="System.out" />
		<param name="Threshold" value="INFO" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] %m%n" />
		</layout>
	</appender>

	<category name="javax.faces">
		<priority value="ERROR" />
		<appender-ref ref="FILE" />
	</category>

	<category name="org.jboss.axis">
		<priority value="ERROR" />
		<appender-ref ref="FILE" />
	</category>

	<category name="org.jboss.management">
		<priority value="ERROR" />
		<appender-ref ref="FILE" />
	</category>

	<category name="org.apache">
		<priority value="ERROR" />
		<appender-ref ref="FILE" />
	</category>

	<category name="org.hibernate">
		<priority value="ERROR" />
		<appender-ref ref="FILE" />
	</category>
	<category name="org.springframework">
		<priority value="ERROR" />
		<appender-ref ref="FILE" />
	</category>
	<category name="org.jboss">
		<priority value="INFO" />
		<appender-ref ref="FILE" />
	</category>
	<category name="com.mattiz">
		<priority value="DEBUG" />
	</category>
	<category name="com.mattiz.web.managedBeans">
		<priority value="INFO" />
	</category>
	<root>
		<appender-ref ref="CONSOLE" />
		<appender-ref ref="FILE" />
	</root>
	<!--error -> warn -> info -> debug -->
</log4j:configuration>

I make following changes to two methods from the “Web Design Patterns at Work with Pagination” example; changes are in bold.
Following method is in AuthorBean.java:

public String addAuthor() {
    logger.debug("This is a debug from AddAuthor");
    logger.info("This is info from AddAuthor");
    logger.warn("This is an warning from AddAuthor");
    logger.error("This is an error from AddAuthor");

    AuthorDTO authorDTO = new AuthorDTO(getIsbnCode());
    authorDTO.setAuthor(getAuthor());
    MattizDelegate mattizDelegate = new MattizDelegate();
    try {
        mattizDelegate.addAuthor(authorDTO);
    } catch (MattizException me) { }
    return "authorAdded";
}

Following method is in MattizDelegate.java:

public void addAuthor(AuthorDTO author) throws MattizException {
       logger.debug("This is debug from MattizDelegate");
       logger.info("This is info from MattizDelegate");
       logger.warn("This is a warning from MattizDelegate");
       logger.error("This is an error from MattizDelegate");
       mattizDAO.addAuthor(author);
}

I add the following instance variable to AuthorBean.java:

private static Logger logger = Logger.getLogger(com.mattiz.web.managedBeans.AuthorBean.class);

and this line to MattizDelegate.java

private static Logger logger = Logger.getLogger(com.mattiz.businessDelegates.MattizDelegate.class);

The following change in the log4j.xml gives the following logging messages in mattiz.log as shown below:

<category name="com.mattiz">
	<priority value="DEBUG" />
</category>
<category name="com.mattiz.web.managedBeans">
	<priority value="INFO" />
</category>

From mattiz.log

2007-06-21 21:58:35,781 INFO  [com.mattiz.web.managedBeans.AuthorBean] This is info from AddAuthor
2007-06-21 21:58:35,781 WARN  [com.mattiz.web.managedBeans.AuthorBean] This is an warning from AddAuthor
2007-06-21 21:58:35,781 ERROR [com.mattiz.web.managedBeans.AuthorBean] This is an error from AddAuthor
2007-06-21 21:58:35,797 DEBUG [com.mattiz.businessDelegates.MattizDelegate] This is debug from MattizDelegate
2007-06-21 21:58:35,797 INFO  [com.mattiz.businessDelegates.MattizDelegate] This is info from MattizDelegate
2007-06-21 21:58:35,797 WARN  [com.mattiz.businessDelegates.MattizDelegate] This is a warning from MattizDelegate
2007-06-21 21:58:35,797 ERROR [com.mattiz.businessDelegates.MattizDelegate] This is an error from MattizDelegate

This change in the log4j.xml gives the following logging messages in mattiz.log as shown below:

<category name="com.mattiz">
	<priority value="INFO" />
</category>
<category name="com.mattiz.web.managedBeans">
	<priority value="DEBUG" />
</category>

From mattiz.log

2007-06-21 21:51:27,847 DEBUG [com.mattiz.web.managedBeans.AuthorBean] This is a debug from AddAuthor
2007-06-21 21:51:27,847 INFO  [com.mattiz.web.managedBeans.AuthorBean] This is info from AddAuthor
2007-06-21 21:51:27,847 WARN  [com.mattiz.web.managedBeans.AuthorBean] This is an warning from AddAuthor
2007-06-21 21:51:27,847 ERROR [com.mattiz.web.managedBeans.AuthorBean] This is an error from AddAuthor
2007-06-21 21:51:27,863 INFO  [com.mattiz.businessDelegates.MattizDelegate] This is info from MattizDelegate
2007-06-21 21:51:27,863 WARN  [com.mattiz.businessDelegates.MattizDelegate] This is a warning from MattizDelegate
2007-06-21 21:51:27,863 ERROR [com.mattiz.businessDelegates.MattizDelegate] This is an error from MattizDelegate

As you can see logging levels of classes can be set for each package or subpackage by configuring log4j.xml.
The heirarchy for logging levels is error -> warn -> info -> debug
In case you select debug, you get error, warn, info and debug messages as well.
In case you just select info, you just get error,warn and info messages.

A hands on is the best way to try this out.
Also note that there is a different logging scheme for the console as can be seen from the output.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: