Package subclass

Source Code of subclass.MyLoggerTest

/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software
* License version 1.1, a copy of which has been included with this
* distribution in the LICENSE.txt file.  */

package subclass;

import org.apache.log4j.*;

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.joran.JoranConfigurator;

/**
   A simple example showing logger subclassing.

   <p>The example should make it clear that subclasses follow the
   hierarchy. You should also try running this example with a <a
   href="doc-files/mycat.bad">bad</a> and <a
   href="doc-files/mycat.good">good</a> configuration file samples.

   <p>See <b><a
   href="doc-files/MyLogger.java">source code</a></b> for more details.
*/
public class MyLoggerTest {

  /**
     When called wihtout arguments, this program will just print
     <pre>
       DEBUG [main] some.cat - Hello world.
     </pre>
     and exit.
    
     <b>However, it can be called with a configuration file in XML or
     properties format.

   */
  static public void main(String[] args) {
   
    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")) {
        JoranConfigurator jc = new JoranConfigurator();
         jc.doConfigure(args[0], LogManager.getLoggerRepository());
      } 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) {
      System.err.println("Did you forget to set the factory in the config file?");
      e.printStackTrace(System.err);
    }
  }

  static
  void usage(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);
  }
}
TOP

Related Classes of subclass.MyLoggerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.