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.

XML to HTML tranformation using XSLT

I demonstrate xsl transformation of an xml file to html using a Transformation engine.
The standalone java class ParseXml.java has the following code:


package com.mattiz.transform;

import java.io.*;
// Import for XML
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class ParseXml {
	public static StringBuffer getContent(BufferedInputStream in, String xsl)
			throws Exception {
		try {
			TransformerFactory tFactory = null;
			Transformer transformer = null;
			tFactory = TransformerFactory.newInstance();
			transformer = tFactory.newTransformer(new StreamSource(xsl));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			transformer.transform(new StreamSource(in), new StreamResult(baos));
			// Output content
			StringBuffer buf = new StringBuffer();
			buf.append(baos.toString());
			baos.close();
			return buf;
		} catch (Exception e) {
			System.out.println("Exception in getContent");
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String args[]) {
		BufferedInputStream xmlStream = null;
		/*
		 * Open the files and initialize streams
		 */
		try {
			// xml input file hard coded
			FileInputStream xmlFile = new FileInputStream("students.xml");
			xmlStream = new BufferedInputStream(xmlFile);
		} catch (Exception ex) {
			System.exit(-1);
		}
		String xsl = "students.xsl";// transformation xsl file hard coded
		StringBuffer output = null;

		try {
			output = getContent(xmlStream, xsl);
		} catch (Exception ex) {
			System.out.println("Exception during parsing");
		}
		try {
			// html output file hard coded

			PrintWriter out = new PrintWriter(new BufferedWriter(
					new FileWriter("students.html")));
			out.write(output.toString(), 0, output.toString().length());
			out.flush();
			out.close();
		} catch (Exception ex) {
			System.out.println("Exception during processing output stream");
		}
	}
}

The xml to be parsed, students.xml


<?xml version="1.0"?>
<root>
	<class_REPORT>
		<class_NAME>CLASS X</class_NAME>
		<entry>
			<student_NAME>ARUN</student_NAME>
			<english>20</english>
			<german>A+</german>
		</entry>
		<entry>
			<student_NAME>ANIL</student_NAME>
			<english>10</english>
			<german>C++</german>
		</entry>
		<entry>
			<student_NAME>SAMIR</student_NAME>
			<english>10</english>
			<german>A</german>
		</entry>
		<entry>
			<student_NAME>VIJAY</student_NAME>
			<english>21</english>
			<german>AB</german>
		</entry>
		<entry>
			<student_NAME>GEORGE</student_NAME>
			<english>11</english>
			<german>D</german>
		</entry>
	</class_REPORT>
	<class_REPORT>
		<class_NAME>CLASS XII</class_NAME>
		<entry>
			<student_NAME>JIM</student_NAME>
			<english>14</english>
			<german>A++</german>
		</entry>
	</class_REPORT>
</root>

The xsl file used for the transformation, students.xsl


<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">
	<xsl:template match="CLASS_REPORT">
		<table cellpadding="0" cellspacing="0" width="500" border="0">
			<tr bgcolor="iceblue">
				<td colspan="3" align="center">
					<b>
						<xsl:value-of select="CLASS_NAME" />
					</b>
				</td>
			</tr>
		</table>
		<table cellpadding="0" cellspacing="0" width="500" border="1">
			<xsl:for-each select="ENTRY">
				<xsl:if test="position() < 2">
					<tr bgcolor="cyan">
						<td width="25%">
							<xsl:text>STUDENT NAME</xsl:text>
						</td>
						<td width="25%">
							<xsl:text>ENGLISH</xsl:text>
						</td>
						<td width="25%">
							<xsl:text>GERMAN</xsl:text>
						</td>
					</tr>
				</xsl:if>
				<tr>
					<xsl:for-each select="STUDENT_NAME">
						<td width="25%">
							<xsl:if test="string(.)!='null'">
								<i>
									<xsl:value-of select="." />
								</i>
							</xsl:if>
						</td>
					</xsl:for-each>
					<xsl:for-each select="ENGLISH">
						<td width="25%">
							<xsl:if test="string(.)!='null'">
								<i>
									<xsl:value-of select="." />
								</i>
							</xsl:if>
						</td>
					</xsl:for-each>
					<xsl:for-each select="GERMAN">
						<td width="25%">
							<xsl:if test="string(.)!='null'">
								<i>
									<xsl:value-of select="." />
								</i>
							</xsl:if>
						</td>
					</xsl:for-each>
				</tr>
			</xsl:for-each>
			<tr bgcolor="pink">
				<td width="25%">TOTAL</td>
				<td width="25%">
					<xsl:value-of select="format-number(sum(ENTRY/ENGLISH),'###.00')" />
				</td>
				<td width="25%"></td>
			</tr>
		</table>
	</xsl:template>
</xsl:stylesheet>

The output html file produced by the tranformation, students.html

<?xml version="1.0" encoding="UTF-8"?>
<table cellpadding="0" cellspacing="0" width="500" border="0">
	<tr bgcolor="iceblue">
		<td colspan="3" align="center">
			<b>CLASS X</b>
		</td>
	</tr>
</table>
<table cellpadding="0" cellspacing="0" width="500" border="1">
	<tr bgcolor="cyan">
		<td width="25%">
			STUDENT NAME
</td>
		<td width="25%">
			ENGLISH
</td>
		<td width="25%">
			GERMAN
</td>
	</tr>
	<tr>
		<td width="25%">
			<i>ARUN</i>
		</td>
		<td width="25%">
			<i>20</i>
		</td>
		<td width="25%">
			<i>A+</i>
		</td>
	</tr>
	<tr>
		<td width="25%">
			<i>ANIL</i>
		</td>
		<td width="25%">
			<i>10</i>
		</td>
		<td width="25%">
			<i>C++</i>
		</td>
	</tr>
	<tr>
		<td width="25%">
			<i>SAMIR</i>
		</td>
		<td width="25%">
			<i>10</i>
		</td>
		<td width="25%">
			<i>A</i>
		</td>
	</tr>
	<tr>
		<td width="25%">
			<i>VIJAY</i>
		</td>
		<td width="25%">
			<i>21</i>
		</td>
		<td width="25%">
			<i>AB</i>
		</td>
	</tr>
	<tr>
		<td width="25%">
			<i>GEORGE</i>
		</td>
		<td width="25%">
			<i>11</i>
		</td>
		<td width="25%">
			<i>D</i>
		</td>
	</tr>
	<tr bgcolor="pink">
		<td width="25%">
			TOTAL
</td>
		<td width="25%">
			72.00
</td>
		<td width="25%" />
	</tr>
</table>
<table cellpadding="0" cellspacing="0" width="500" border="0">
	<tr bgcolor="iceblue">
		<td colspan="3" align="center">
			<b>CLASS XII</b>
		</td>
	</tr>
</table>
<table cellpadding="0" cellspacing="0" width="500" border="1">
	<tr bgcolor="cyan">
		<td width="25%">
			STUDENT NAME
</td>
		<td width="25%">
			ENGLISH
</td>
		<td width="25%">
			GERMAN
</td>
	</tr>
	<tr>
		<td width="25%">
			<i>JIM</i>
		</td>
		<td width="25%">
			<i>14</i>
		</td>
		<td width="25%">
			<i>A++</i>
		</td>
	</tr>
	<tr bgcolor="pink">
		<td width="25%">
			TOTAL
</td>
		<td width="25%">
			14.00
</td>
		<td width="25%" />
	</tr>
</table>

Run the standalone java application from the command line with “java ParseXml”

The html file produced has the following look: