You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-cvs@jakarta.apache.org by ce...@apache.org on 2002/01/21 12:20:58 UTC

cvs commit: jakarta-log4j/examples/subclass MyCategory.java MyCategoryFactory.java MyCategoryTest.java

ceki        02/01/21 03:20:58

  Modified:    examples Sort.java SortAlgo.java Trivial.java
  Added:       examples/subclass MyCategory.java MyCategoryFactory.java
                        MyCategoryTest.java
  Removed:     examples MyCategory.java MyCategoryFactory.java
                        MyCategoryTest.java
  Log:
  Reorganizing examples directory.
  
  Revision  Changes    Path
  1.5       +3 -3      jakarta-log4j/examples/Sort.java
  
  Index: Sort.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/examples/Sort.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Sort.java	1 Oct 2001 21:16:37 -0000	1.4
  +++ Sort.java	21 Jan 2002 11:20:58 -0000	1.5
  @@ -1,5 +1,5 @@
   
  -package org.apache.log4j.examples;
  +package examples;
   
   import org.apache.log4j.PropertyConfigurator;
   import org.apache.log4j.Logger;
  @@ -7,7 +7,7 @@
   
   /**
      Example code for log4j to viewed in conjunction with the {@link
  -   org.apache.log4j.examples.SortAlgo SortAlgo} class.
  +   examples.SortAlgo SortAlgo} class.
      
      <p>This program expects a configuration file name as its first
      argument, and the size of the array to sort as the second and last
  @@ -71,7 +71,7 @@
     static
     void usage(String errMsg) {
       System.err.println(errMsg);
  -    System.err.println("\nUsage: java org.apache.log4j.examples.Sort " +
  +    System.err.println("\nUsage: java org.apache.examples.Sort " +
   		       "configFile ARRAY_SIZE\n"+
         "where  configFile is a configuration file\n"+
         "      ARRAY_SIZE is a positive integer.\n");
  
  
  
  1.5       +3 -4      jakarta-log4j/examples/SortAlgo.java
  
  Index: SortAlgo.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/examples/SortAlgo.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SortAlgo.java	1 Oct 2001 21:16:37 -0000	1.4
  +++ SortAlgo.java	21 Jan 2002 11:20:58 -0000	1.5
  @@ -1,19 +1,18 @@
   
  -package org.apache.log4j.examples;
  +package examples;
   
   import org.apache.log4j.Category;
   import org.apache.log4j.NDC;
   
   /**
      Example code for log4j to viewed in conjunction with the {@link
  -   org.apache.log4j.examples.Sort Sort} class.
  +   examples.Sort Sort} class.
         
      <p>SortAlgo uses the bubble sort algorithm to sort an integer
      array. See also its <b><a href="doc-files/SortAlgo.java">source
      code</a></b>.
   
  -   @author Ceki G&uuml;lc&uuml;
  -*/
  +   @author Ceki G&uuml;lc&uuml; */
   public class SortAlgo {
   
     final static String className = SortAlgo.class.getName();
  
  
  
  1.3       +7 -8      jakarta-log4j/examples/Trivial.java
  
  Index: Trivial.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/examples/Trivial.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Trivial.java	14 Dec 2000 21:07:35 -0000	1.2
  +++ Trivial.java	21 Jan 2002 11:20:58 -0000	1.3
  @@ -1,5 +1,5 @@
   
  -package org.apache.log4j.examples;
  +package examples;
   
   
   import org.apache.log4j.Category;
  @@ -8,15 +8,14 @@
   
   /**
      View the <a href="doc-files/Trivial.java">source code</a> of this a
  -   trivial usage example. Running <code>java
  -   org.apache.log4j.examples.Trivial</code> should output something
  -   similar to:
  +   trivial usage example. Running <code>java examples.Trivial</code>
  +   should output something similar to:
   
      <pre>
  -      0    INFO  [main] org.apache.log4j.examples.Trivial (Client #45890) - Awake awake. Put on thy strength.
  -      15   DEBUG [main] org.apache.log4j.examples.Trivial (Client #45890 DB) - Now king David was old.
  -      278  INFO  [main] org.apache.log4j.examples.Trivial$InnerTrivial (Client #45890) - Entered foo.
  -      293  INFO  [main] org.apache.log4j.examples.Trivial (Client #45890) - Exiting Trivial.   
  +      0    INFO  [main] examples.Trivial (Client #45890) - Awake awake. Put on thy strength.
  +      15   DEBUG [main] examples.Trivial (Client #45890 DB) - Now king David was old.
  +      278  INFO  [main] examples.Trivial$InnerTrivial (Client #45890) - Entered foo.
  +      293  INFO  [main] examples.Trivial (Client #45890) - Exiting Trivial.   
      </pre>
      
      <p> The increasing numbers at the beginning of each line are the
  
  
  
  1.1                  jakarta-log4j/examples/subclass/MyCategory.java
  
  Index: MyCategory.java
  ===================================================================
  /*
   * 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.APL file.  */
  
  package org.apache.log4j.examples;
  
  import org.apache.log4j.*;
  import org.apache.log4j.spi.LoggerFactory;
  import org.apache.log4j.xml.DOMConfigurator;
  import org.apache.log4j.xml.examples.XLevel;
  import org.apache.log4j.PropertyConfigurator;
  import org.apache.log4j.helpers.LogLog;
  
  /**
     A simple example showing category subclassing. 
  
     <p>See <b><a href="doc-files/MyCategory.java">source code</a></b>
     for more details.
  
     <p>See {@link MyCategoryTest} for a usage example.
     
   */
  public class MyCategory extends Logger {
  
    // It's usually a good idea to add a dot suffix to the fully
    // qualified class name. This makes caller localization to work
    // properly even from classes that have almost the same fully
    // qualified class name as MyCategory, e.g. MyCategoryTest.
    static String FQCN = MyCategory.class.getName() + ".";
  
    // It's enough to instantiate a factory once and for all.
    private static MyCategoryFactory myFactory = new MyCategoryFactory();
  
    /**
       Just calls the parent constuctor.
     */
    public MyCategory(String name) {
      super(name);
    }
  
    /**
       Overrides the standard debug method by appending " world" at the
       end of each message.  */
    public 
    void debug(Object message) {
      super.log(FQCN, Level.DEBUG, message + " world.", null);    
    }
    
    /**
       This method overrides {@link Category#getInstance} by supplying
       its own factory type as a parameter.
    */
    public 
    static
    Logger getInstance(String name) {
      return Category.getInstance(name, myFactory); 
    }
  
    public
    void trace(Object message) {
      super.log(FQCN, XLevel.TRACE, message, null); 
    }
  }
  
  
  
  
  
  1.1                  jakarta-log4j/examples/subclass/MyCategoryFactory.java
  
  Index: MyCategoryFactory.java
  ===================================================================
  /*
   * 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.APL file.  */
  
  package org.apache.log4j.examples;
  
  import org.apache.log4j.Logger;
  import org.apache.log4j.spi.LoggerFactory;
  
  /**
     A factory that makes new {@link MyCategory} objects.
  
     See <b><a href="doc-files/MyCategoryFactory.java">source
     code</a></b> for more details.
  
     @author Ceki G&uuml;lc&uuml; */
  public class MyCategoryFactory implements LoggerFactory {
  
    /**
       The constructor should be public as it will be called by
       configurators in different packages.  */
    public
    MyCategoryFactory() {
    }
  
    public
    Logger makeNewLoggerInstance(String name) {
      return new MyCategory(name);
    }
  }
  
  
  
  1.1                  jakarta-log4j/examples/subclass/MyCategoryTest.java
  
  Index: MyCategoryTest.java
  ===================================================================
  /*
   * 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.APL file.  */
  
  package org.apache.log4j.examples;
  
  import org.apache.log4j.*;
  import org.apache.log4j.spi.LoggerFactory;
  import org.apache.log4j.xml.DOMConfigurator;
  import org.apache.log4j.xml.examples.XLevel;
  import org.apache.log4j.PropertyConfigurator;
  import org.apache.log4j.helpers.LogLog;
  
  /**
     A simple example showing category 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/MyCategory.java">source code</a></b> for more details.
  */
  public class MyCategoryTest {
  
    /**
       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 MyCategory. The output still
        // goes to System.out.
        Category root = Category.getRoot();
        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 {
        MyCategory c = (MyCategory) MyCategory.getInstance("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
    void usage(String errMsg) {
      System.err.println(errMsg);
      System.err.println("\nUsage: "+MyCategory.class.getName() + "[configFile]\n"
                  + " where *configFile* is an optional configuration file, "+
  		       "either in properties or XML format.");
      System.exit(1);
    }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>