14.01.2013 Views

Chapter 9: Using the Apache Log Tag Library - BeKnowledge

Chapter 9: Using the Apache Log Tag Library - BeKnowledge

Chapter 9: Using the Apache Log Tag Library - BeKnowledge

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Chapter</strong> 9: <strong>Using</strong> <strong>the</strong> <strong>Apache</strong> <strong>Log</strong> <strong>Tag</strong> <strong>Library</strong><br />

Figure 9-2: The Simple<strong>Log</strong>Dump.jsp page<br />

Creating Custom <strong>Tag</strong>s with <strong>the</strong> <strong>Log</strong> <strong>Tag</strong> <strong>Library</strong> to Use a<br />

Custom Level<br />

In <strong>Chapter</strong> 8, we saw how to write a custom Level class, TRACE, and use it with log4j. If we want to<br />

use <strong>the</strong> TRACE custom level along with <strong>the</strong> <strong>Log</strong> tag library, we have to extend <strong>the</strong> <strong>Log</strong> tag library<br />

framework. Typically, we can do so by performing <strong>the</strong>se tasks:<br />

� Write a new <strong>Tag</strong> class to represent <strong>the</strong> custom level TRACE.<br />

� Make <strong>the</strong> new <strong>Tag</strong> and Level classes available to <strong>the</strong> application.<br />

� Modify <strong>the</strong> "taglib-descriptor" file to describe <strong>the</strong> new tag.<br />

In <strong>the</strong> next sections, we will examine how we can achieve <strong>the</strong>se tasks to use <strong>the</strong> TRACE level with <strong>the</strong><br />

<strong>Log</strong> tag library.<br />

Creating a New <strong>Tag</strong><br />

In <strong>the</strong> <strong>Log</strong> tag library world, all tag classes subclass from <strong>the</strong> abstract class <strong>Log</strong>ger<strong>Tag</strong>. The<br />

<strong>Log</strong>ger<strong>Tag</strong> class itself is a subclass of <strong>the</strong> Body<strong>Tag</strong>Support class and does <strong>the</strong> entire job of handling<br />

tag-related operations. It also defines an abstract method, getPriority(), that is implemented in all<br />

o<strong>the</strong>r subclasses of <strong>the</strong> <strong>Log</strong>ger<strong>Tag</strong> class. All o<strong>the</strong>r tag classes such as Error<strong>Tag</strong>, Info<strong>Tag</strong>, etc., are<br />

subclasses of <strong>Log</strong>ger<strong>Tag</strong> and provide implementations for <strong>the</strong> getPriority() method. Thus, to define<br />

a new tag, we simply need to extend <strong>the</strong> <strong>Log</strong>ger<strong>Tag</strong> class and provide an implementation of <strong>the</strong><br />

getPriority() method that returns <strong>the</strong> custom level TRACE. Listing 9-5, Trace<strong>Tag</strong>.java,<br />

demonstrates how to write a custom tag using <strong>the</strong> custom level TRACE.<br />

Listing 9-5: Trace<strong>Tag</strong>.java<br />

package com.apress.logging.log4j.customtag;<br />

import org.apache.taglibs.log.<strong>Log</strong>ger<strong>Tag</strong>;<br />

import org.apache.log4j.Priority;<br />

import com.apress.logging.log4j.CustomLevel;<br />

public class Trace<strong>Tag</strong> extends <strong>Log</strong>ger<strong>Tag</strong><br />

{<br />

protected Priority getPriority()<br />

{<br />

return CustomLevel.TRACE;<br />

}<br />

}<br />

file://C:\Documents and Settings\deepak\Local Settings\Temp\~hh572B.htm<br />

Page 8 of 11<br />

6/14/2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!