Log4J crash course


Log4J is the logging implementation available from Apache ‚ s Jakarta project and has been around long before JDK Logging appeared and quite naturally has a larger developer base. Lot of material is freely available online if you want to dig deeper into Log4J and we have held back from such a detailed treatment here. As with any Logging mechanisms, this library provides powerful capabilities to declaratively control logging and the level of logging.

In Log4J, all the logging occurs through the Logger class in org.apache.log4j package. The Logger class supports five levels for logging. They are FATAL, ERROR, WARNING, INFO , DEBUG. Without Log4J, you would perhaps use a Boolean flag to control the logging. With such a boolean flag, there are only two states ‚ logging or no logging. In Log4J the levels are defined to fine tune the amount of logging. Here is how you would user the Log4J.

 Logger logger = Logger.getLogger (foo.bar);            logger.debug (This is a debug message); 

The code above first obtains the Logger instance named foo.bar and logs a message at DEBUG level. You can declaratively turn off the logging for messages at lower level than WARNING. This means the messages logged at INFO and DEBUG level will not be logged.

Logged messages always end up in a destination like file, database table etc. The destination of the log message is specified using the Appender . The Appender can represent a file, console, email address or as exotic as a JMS channel. If you need a destination that is not supported by the classes out of the box you can write a new class that implements the Appender interface. Appenders can be configured at startup in a variety of ways. One way to configure them is through an XML file. A XML file is shown below.

 <appender name="Mybank-Warn"          class="org.apache.log4j.FileAppender">   <param name="Threshold" value="WARN" />   <param name="File"   value="./logs/mybank-warnings.log" />   <param name="Append" value="false" />   <layout class="org.apache.log4j.PatternLayout">      <param name="ConversionPattern"              value="%d [%x][%t] %-5p %c{2} - %m%n"/>   </layout> </appender> <category name="foo.bar" additivity="false">   <appender-ref ref="Mybank-Warn" />   <appender-ref ref="Developer-Console" /> </category> 

The above XML when translated to simple English reads as follows : The Appender named Mybank-Warn logs the messages to a file mybank-warnings.log . Only messages with a threshold of WARN or higher are logged. The format of the message is as specified by the PatternLayout .

The format of the output message is specified using Layout . Standard classes for specifying the layout like PatternLayout are used most of the times and the format is declaratively specified using symbols like %d which instructs Log4J to include date time in the log and %m ‚ the actual message itself and so on.

As you saw earlier, the logging is performed through a named Logger instance. If you are wondering how the Logger would know which Appender to log to, it is the < category > element in the above XML that provides the link between the two. The Logger uses the < category > setting in the XML to get this information. The < category > in the above XML is called foo.bar . Recall that we tried to log using a Logger named foo.bar . The foo.bar Logger gets the FileAppender Mybank-Warn appender through the foo.bar category setting in the XML. And then the messages end up in the file mybank-warnings.log .

There can be more than one appenders associated with a category. This implies that the messages logged with a Logger can potentially end up in multiple locations if needed.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net