FileDocCategorySizeDatePackage
MyLoggerTest.javaAPI DocApache log4j 1.2.152904Sat Aug 25 00:09:44 BST 2007examples.subclass

MyLoggerTest

public class MyLoggerTest extends Object
A simple example showing logger subclassing.

The example should make it clear that subclasses follow the hierarchy. You should also try running this example with a bad and good configuration file samples.

See source code for more details.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
When called wihtout arguments, this program will just print
DEBUG [main] some.cat - Hello world.
and exit. However, it can be called with a configuration file in XML or properties format.

    
    if(args.length == 0) {
      // Note that the appender is added to root but that the log
      // request is made to an instance of MyLogger. The output still
      // goes to System.out.
      Logger root = Logger.getRootLogger();
      Layout layout = new PatternLayout("%p [%t] %c (%F:%L) - %m%n");
      root.addAppender(new ConsoleAppender(layout, ConsoleAppender.SYSTEM_OUT));
    }
    else if(args.length == 1) {
      if(args[0].endsWith("xml")) {
	DOMConfigurator.configure(args[0]);
      } else {
	PropertyConfigurator.configure(args[0]);
      }
    } else {
      usage("Incorrect number of parameters.");
    }
    try {
      MyLogger c = (MyLogger) MyLogger.getLogger("some.cat");    
      c.trace("Hello");
      c.debug("Hello");
    } catch(ClassCastException e) {
      LogLog.error("Did you forget to set the factory in the config file?", e);
    }
  
static voidusage(java.lang.String errMsg)

    System.err.println(errMsg);
    System.err.println("\nUsage: "+MyLogger.class.getName() + "[configFile]\n"
                + " where *configFile* is an optional configuration file, "+
		       "either in properties or XML format.");
    System.exit(1);