Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Write custom appenders for log4j

Extend log4j to support lightweight over-the-network logging

  • Print
  • Feedback

Page 2 of 6

Using log4J

Listing 1 demonstrates how to use log4j. You create a Logger object associated with the current class. (The string argument to getLogger() is actually arbitrary, but the class name is by far the most useful name for the logger.)

Then, when you want to log a message, you just send it to the logger. Logged messages typically fall into one of five categories: debug, info, warn, error, or fatal, and methods named debug(), info(), and so on, handle each of these. When you're done logging, it's good style to shut down the logging subsystem with a call to shutdown() (at the bottom of main()). This call is particularly important for the example I'm about to cover because shutdown() indirectly causes socket connections to remote clients to shut down in an orderly way.

Listing 1. Test.java: Using the log4j classes

  1  import org.apache.log4j.Logger;
   2  import org.apache.log4j.LogManager;
   3  
   4  public class Test
   5  {
   6      private static final Logger log = Logger.getLogger( "com.holub.log4j.Test");
   7  
   8      public static void main(String[] args) throws Exception
   9      {
  10          // For testing, give the client that will display the
  11          // logged messages a moment to connect.
  12          // (It's in a 50-ms wait loop, so pausing for
  13          // 100 ms should do it).
  14          Thread.currentThread().sleep( 100 );
  15  
  16          log.debug("Debug   Message");
  17          log.warn ("Warning Message");
  18          log.error("Error   Message");
  19  
  20          Thread.currentThread().sleep( 100 );
  21          LogManager.shutdown();
  22      }
  23  }



The only other piece of the puzzle is a simple configuration file, which (thankfully) is not in XML format. It's a simple properties file, like the one in Listing 2.

To understand the file, you need to know a little about the logger architecture. Loggers form a runtime hierarchy of objects, organized by name. The "root" logger is at the root of the hierarchy, and the loggers you create are beneath the root (and each other), depending on their names. For example, a logger named a.b is beneath the logger named a, which is beneath the root.

Loggers write strings using two main helper classes called appenders and layouts. An appender object does the actual writing, and a layout object formats the message. Appenders are bound to a logger at runtime using information in the configuration file—this way, you can change them without recompiling. A particular logger can use several appenders, in which case, each appender sends the message somewhere, thus duplicating messages in several places. Log4j comes with several appenders that do things like console and file output and send logging messages using email or JMS (Java Message Service). Log4j also includes a socket-based appender similar to the one I illustrate in this article.

Layout objects, which control message formatting, are bound to appenders at runtime in a manner similar to loggers and appenders. Log4J comes with several layout classes, which format in XML, HTML, and by means of a printf-like format string. I've found these to be adequate for most of my needs.

  • Print
  • Feedback

Resources