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 />

This configuration file defines <strong>the</strong> configuration for <strong>the</strong> logger test. The test logger has a threshold<br />

level of DEBUG and uses a RollingFileAppender object to print logging information to a file named<br />

"logtags.log". Notice that <strong>the</strong> conversionPattern to format <strong>the</strong> logging information includes <strong>the</strong> name<br />

of <strong>the</strong> logger (%c).<br />

<strong>Using</strong> this configuration file for <strong>Log</strong> will write information to <strong>the</strong> "logtags.log" file in <strong>the</strong> "%<br />

TOMCAT_HOME%\bin" directory. If we execute <strong>the</strong> example JSP with this modified configuration<br />

file, we will see <strong>the</strong> following output in <strong>the</strong> "logtags.log" file:<br />

DEBUG Thread-11 root - Message embedded within open and close tags.<br />

DEBUG Thread-11 root - Message passed as an attribute to <strong>the</strong> tag<br />

INFO Thread-11 test - Hello how are you?<br />

This is how we can use different loggers with custom configuration to handle logging information. If we<br />

specify <strong>the</strong> category attribute in <strong>the</strong> JSP but do not specify a configuration for it, <strong>Log</strong> will use <strong>the</strong> root<br />

logger configuration to handle <strong>the</strong> logging request.<br />

Description of <strong>Log</strong> <strong>Tag</strong>s<br />

The <strong>Log</strong> tag library provides tags corresponding to <strong>the</strong> levels declared in log4j. Table 9-1 summarizes all<br />

<strong>the</strong> <strong>Log</strong> tag library tags and <strong>the</strong>ir attributes.<br />

Table 9-1: Summary of <strong>Log</strong> <strong>Tag</strong>s<br />

<strong>Tag</strong> Name <strong>Tag</strong> Description Attribute Name Required<br />

debug Displays a DEBUG-level message category message No<br />

No<br />

info Displays an INFO-level message category message No<br />

No<br />

warn Displays a WARN-level message category message No<br />

No<br />

error Displays a ERROR-level message category message No~<br />

No<br />

fatal Displays a FATAL-level message category message No<br />

No<br />

dump Displays all <strong>the</strong> variables in a specified scope scope Yes<br />

Page 6 of 11<br />

As you can see, <strong>the</strong> use of <strong>the</strong>se <strong>Log</strong> tags is very simple. All <strong>the</strong> tags except dump have two optional<br />

attributes, category and message. It is possible to specify <strong>the</strong> logging information as a value of <strong>the</strong><br />

message attribute or by enclosing it within <strong>the</strong> tag. For example, we can use <strong>the</strong> error tag in following<br />

two ways:<br />

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

6/14/2010

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

Saved successfully!

Ooh no, something went wrong!