Customizing Log4J

I l @ ve RuBoard

Now that you've seen Log4J in action, you can examine some of the ways that it can be customized to offer you just the information that you need. Take a look at that mysterious log4j.properties file, shown in listing 7.12:

Listing 7.12 log4j.properties
 # Set root category priority to DEBUG and its only appender to A1. log4j.rootCategory=DEBUG, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 

Looking at the file contents, you can begin to see that Log4J is more than just a simple console logging library. Log4J organizes everything around two basic concepts: categories and appenders.

Categories

Categories are used to sort the various log messages into a hierarchy. In the code you've seen so far (Listing 7.8), and by normal convention, categories are equivalent to classes with their packages, but this is by no means a requirement. You can pass any string into the Category.getInstance call.

You can then define various levels of logging for various categories, which gives you a great deal of flexibility. For example, you might frequently find that you need to turn on debugging in one specific file or set of files, but you don't want to open up the floodgates by enabling every debugging statement in the application.

Listing 7.13 shows a version of the properties file that enables logging for the com.bfg.demo classes but that shows only warnings or above for the rest of the application.

Listing 7.13 A Different log4j.properties
 # Set root category priority to WARN and its only appender to A1. log4j.rootCategory=WARN, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n # Print debug messages for package com.bfg.demo log4j.category.com.bfg.demo=DEBUG 

Appenders

The other half of the Log4J equation consists of appenders. The appender that you have been using is the console appender, which just sends its output to the console. But Log4J allows for much more interesting possibilities. For example, you can send log messages to the UNIX Syslog, and because many remote monitoring tools know how to read the syslog, you can use them to monitor your Tomcat application.

Other appenders exist for JMS, NT events, and remote sockets. You also can write your own appender, if you need to ”for example, to interface to a custom monitoring application.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

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