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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

� Changing <strong>the</strong> threshold level for <strong>the</strong> root logger or o<strong>the</strong>r custom loggers used with any of <strong>the</strong> tags<br />

can cause changes in <strong>the</strong> logging output.<br />

Note If no separate logger (Category) is explicitly specified as an attribute of any of <strong>the</strong> <strong>Log</strong> tags, <strong>the</strong><br />

tags rely on <strong>the</strong> root logger of log4j to publish logging information. Hence, it is always safe to keep<br />

<strong>the</strong> root logger configuration even though we may specify custom logger (Category) names within<br />

<strong>the</strong> tag attributes and configure <strong>the</strong>m separately in <strong>the</strong> same configuration file.<br />

Setting <strong>the</strong> Environment<br />

Having followed <strong>the</strong> instructions in <strong>the</strong> "Installing <strong>the</strong> <strong>Log</strong> <strong>Tag</strong> <strong>Library</strong>" section, we are all ready to use<br />

<strong>the</strong> <strong>Log</strong> tag library from within a JSP. Before we can see <strong>the</strong> example JSP in Listing 9-1 in action, we<br />

need to configure <strong>the</strong> Web server to use <strong>the</strong> JSP and <strong>the</strong> log4j configuration file as described in next few<br />

steps:<br />

1. First, put <strong>the</strong> JPS described in Listing 9-1 into a folder named "Test<strong>Log</strong><strong>Tag</strong>s" (or any folder of<br />

your choice) under <strong>the</strong> "webapps" directory of <strong>the</strong> Tomcat installation.<br />

2. In Tomcat, any Web application typically contains a "WEB-INF" directory and one "WEB-<br />

INF/classes" directory. Place <strong>the</strong> "log4j.properties" file in one of <strong>the</strong>se locations. For example,<br />

you may decide to put it in <strong>the</strong> "WEB-INF/classes" directory.<br />

3. Specify where Tomcat can find <strong>the</strong> configuration file shown in <strong>the</strong> preceding section for log4j.<br />

4. Pass <strong>the</strong> "log4j.properties" file as a system variable to <strong>the</strong> execution environment of Tomcat.<br />

5. Go to <strong>the</strong> "tomcat.bat" file in "%TOMCAT_HOME%\bin".<br />

6. Add an entry to set <strong>the</strong> classpath variable to point to <strong>the</strong> directory containing <strong>the</strong> "log4j.properties"<br />

file. For this example, <strong>the</strong> entry may read as follows:<br />

set CP=%CP%;C:\Jakarta-tomcat-3.2.1\webapps\<br />

Test<strong>Log</strong><strong>Tag</strong>s\WEB-INF\classes<br />

7. Add <strong>the</strong> following entry to <strong>the</strong> "tomcat.bat" file:<br />

set TOMCAT_OPTS=-Dlog4j.configuration=log4j.properties<br />

Note We can also configure log4j through an initialization servlet. Consult <strong>Chapter</strong> 5 for more on this<br />

topic.<br />

This is all we need to do before we can run <strong>the</strong> example, which we will see how to do next.<br />

The <strong>Log</strong> Example in Action<br />

Now start up Tomcat and access <strong>the</strong> JSP page, as shown in Figure 9-1. Tomcat will load <strong>the</strong><br />

"log4j.properties" file, and <strong>Log</strong> will use this configuration file to print <strong>the</strong> logging information.<br />

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

Page 4 of 11<br />

6/14/2010

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

Saved successfully!

Ooh no, something went wrong!