The [EMAIL PROTECTED] and [EMAIL PROTECTED] lists were not created so this one bounced.


Date: 22 Dec 2003 17:47:57 -0000
Message-ID: <[EMAIL PROTECTED]>
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Subject: cvs commit: logging-log4j/examples/src/sort sort4.properties Sort.java sort1.properties sort3.properties sort2.properties SortAlgo.java


ceki 2003/12/22 09:47:57

  Added:       examples/src/sort sort4.properties Sort.java
                        sort1.properties sort3.properties sort2.properties
                        SortAlgo.java
  Log:

files in examples/sort moved to examples/src/sort

  Revision  Changes    Path
  1.1                  logging-log4j/examples/src/sort/sort4.properties

  Index: sort4.properties
  ===================================================================
  # Attach appender A1 to root. Set root level to Level.DEBUG.
  log4j.rootLogger=DEBUG, A1

  # A1 is set to be a FileAppender sending its output to
  # System.out. However, only error messages and above will be printed
  # in A1 because A1's threshold is set to Level.ERROR.

  # The fact that the root level is set to Prority.DEBUG only influences
  # log requests made to the root logger. It has no influence on the
  # *appenders* attached to root.

  log4j.appender.A1=org.apache.log4j.ConsoleAppender
  log4j.appender.A1.Threshold=ERROR

  log4j.appender.A1.layout=org.apache.log4j.PatternLayout
  log4j.appender.A1.layout.ConversionPattern=%p [%t] %c{2} (%M:%L) - %m%n

  # Set the level of the logger named "org.apache.log4j.examples" to
  # Level.INFO, attach appender A2.
  log4j.logger.org.apache.log4j.examples=INFO, A2

  # Appender A2 writes to the file "test" in user's home.
  log4j.appender.A2=org.apache.log4j.FileAppender
  log4j.appender.A2.File=${user.home}/test

  # Truncate 'test' if it aleady exists.
  log4j.appender.A2.Append=false

  # Appender A2 uses the PatternLayout.
  log4j.appender.A2.layout=org.apache.log4j.PatternLayout
  log4j.appender.A2.layout.ConversionPattern=%5r %-5p [%t] %c{2} - %m%n




1.1 logging-log4j/examples/src/sort/Sort.java


  Index: Sort.java
  ===================================================================
  package sort;

  import org.apache.log4j.PropertyConfigurator;
  import org.apache.log4j.Logger;

  /**
     Example code for log4j to viewed in conjunction with the [EMAIL PROTECTED]
     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
     argument. See its <b><a href="doc-files/Sort.java">source
     code</a></b> for more details.

     <p>Play around with different values in the configuration file and
     watch the changing behavior.

<p>Example configuration files can be found in <a
href="doc-files/sort1.properties">sort1.properties</a>, <a
href="doc-files/sort2.properties">sort2.properties</a>, <a
href="doc-files/sort3.properties">sort3.properties</a> and <a
href="doc-files/sort4.properties">sort4.properties</a> are supplied with the
package.


     <p>If you are also interested in logging performance, then have
     look at the [EMAIL PROTECTED] org.apache.log4j.performance.Logging} class.

@author Ceki G&uuml;lc&uuml; */

public class Sort {

static Logger logger = Logger.getLogger(Sort.class.getName());

    public static void main(String[] args) {
      if(args.length != 2) {
        usage("Incorrect number of parameters.");
      }
      int arraySize = -1;
      try {
        arraySize = Integer.valueOf(args[1]).intValue();
        if(arraySize <= 0)
        usage("Negative array size.");
      }
      catch(java.lang.NumberFormatException e) {
        usage("Could not number format ["+args[1]+"].");
      }

PropertyConfigurator.configure(args[0]);

int[] intArray = new int[arraySize];

      logger.info("Populating an array of " + arraySize + " elements in" +
             " reverse order.");
      for(int i = arraySize -1 ; i >= 0; i--) {
        intArray[i] = arraySize - i - 1;
      }

      SortAlgo sa1 = new SortAlgo(intArray);
      sa1.bubbleSort();
      sa1.dump();

      // We intentionally initilize sa2 with null.
      SortAlgo sa2 = new SortAlgo(null);
      logger.info("The next log statement should be an error message.");
      sa2.dump();
      logger.info("Exiting main method.");
    }

    static
    void usage(String errMsg) {
      System.err.println(errMsg);
      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");
      System.exit(1);
    }
  }



1.1 logging-log4j/examples/src/sort/sort1.properties

  Index: sort1.properties
  ===================================================================
  # An example log4j configuration file that outputs to System.out.  The
  # output information consists of relative time, log level, thread
  # name, logger name, nested diagnostic context and the message in that
  # order.

  # For the general syntax of property based configuration files see the
  # documenation of org.apache.log4j.PropertyConfigurator.

log4j.rootLogger=DEBUG, A1

  # A1 is set to be a ConsoleAppender which outputs to System.out.
  log4j.appender.A1=org.apache.log4j.ConsoleAppender

  # A1 uses PatternLayout.
  log4j.appender.A1.layout=org.apache.log4j.PatternLayout

  # The conversion pattern uses format specifiers. You might want to
  # change the pattern an watch the output format change.
  log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n

  # In this example, we are not really interested in INNER loop or SWAP
  # messages. See the effects of uncommenting and changing the levels of
  # the following loggers.
  # log4j.logger.org.apache.log4j.examples.SortAlgo.INNER=WARN
  # log4j.logger.org.apache.log4j.examples.SortAlgo.SWAP=WARN




1.1 logging-log4j/examples/src/sort/sort3.properties


  Index: sort3.properties
  ===================================================================
  # An example log4j configuration file that directs its logging output
  # to a SocketAppender. The SocketAppender is configuted to send its
  # output to a server running on the localhost port number 12345.

# To test this example, you must start a log4j server with the command
#
#
# java org.apache.log4j.net.SocketServer 12345 configurationFile directory/
#
#


  # For the general syntax of property based configuration files see
  # the documenation of org.apache.log4j.PropertyConfigurator.

# The root logger uses the appender called A1.

log4j.rootLogger=DEBUG, A1

  # A1 is set to be a SocketAppender sending its output to the server
  running on the local host, port 12345.

  log4j.appender.A1=org.apache.log4j.net.SocketAppender
  log4j.appender.A1.Port=12345
  log4j.appender.A1.RemoteHost=localhost

  # In this example, we are not interested in INNER loop or SWAP
  # messages.  You might try to set INNER and SWAP to DEBUG for more
  # verbose output.

  log4j.logger.org.apache.log4j.examples.SortAlgo.INNER=INFO
  log4j.logger.org.apache.log4j.examples.SortAlgo.SWAP=INFO




1.1 logging-log4j/examples/src/sort/sort2.properties


  Index: sort2.properties
  ===================================================================
  # An example log4j configuration file that outputs both to System.out
  # and a file named 'test'.

  # For the general syntax of property based configuration files see the
  # documenation of org.apache.log4j.PropertyConfigurator.

  # WARNING: Location information can be useful but is very costly in
  # terms of computation.

# The root logger uses the appender called A1.

  # The root logger uses the appenders called A1 and A2. Since no level
  # is specified, note the empty string between the comma (",") and the
  # equals sign ("="), the level of the root logger remains
  # untouched. Log4j always initializes the level for the root logger to
  # DEBUG. The root logger is the only logger that has a default
  # level. Bu default, all other loggers do not have an assigned level,
  # such that they inherit their level instead.

log4j.rootLogger=, A1, A2

  # A1 is set to be ConsoleAppender sending its output to System.out
  log4j.appender.A1=org.apache.log4j.ConsoleAppender


# A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout

  # The conversion pattern consists of date in ISO8601 format, level,
  # thread name, logger name truncated to its rightmost two components
  # and left justified to 17 characters, location information consisting
  # of file name (padded to 13 characters) and line number, nested
  # diagnostic context, the and the application supplied message

log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n

  # Appender A2 writes to the file "test".
  log4j.appender.A2=org.apache.log4j.FileAppender
  log4j.appender.A2.File=test

  # Truncate 'test' if it aleady exists.
  log4j.appender.A2.Append=false

  # Appender A2 uses the PatternLayout.
  log4j.appender.A2.layout=org.apache.log4j.PatternLayout
  log4j.appender.A2.layout.ConversionPattern=%-5r %-5p [%t] %c{2} - %m%n


# In this example, we are not interested in INNER loop or SWAP # messages. You might try to set INNER and SWAP to DEBUG for more # verbose output.

  log4j.logger.org.apache.log4j.examples.SortAlgo.INNER=INFO
  log4j.logger.org.apache.log4j.examples.SortAlgo.SWAP=INFO




1.1 logging-log4j/examples/src/sort/SortAlgo.java


  Index: SortAlgo.java
  ===================================================================
  package sort;

  import org.apache.log4j.Category;
  import org.apache.log4j.NDC;

  /**
     Example code for log4j to viewed in conjunction with the [EMAIL PROTECTED]
     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; */
  public class SortAlgo {

    final static String className = SortAlgo.class.getName();
    final static Category CAT = Category.getInstance(className);
    final static Category OUTER = Category.getInstance(className + ".OUTER");
    final static Category INNER = Category.getInstance(className + ".INNER");
    final static Category DUMP = Category.getInstance(className + ".DUMP");
    final static Category SWAP = Category.getInstance(className + ".SWAP");

int[] intArray;

    SortAlgo(int[] intArray) {
      this.intArray = intArray;
    }

    void bubbleSort() {
      CAT.info( "Entered the sort method.");

      for(int i = intArray.length -1; i >= 0  ; i--) {
        NDC.push("i=" + i);
        OUTER.debug("in outer loop.");
        for(int j = 0; j < i; j++) {
        NDC.push("j=" + j);
        // It is poor practice to ship code with log staments in tight loops.
        // We do it anyway in this example.
        INNER.debug( "in inner loop.");
           if(intArray[j] > intArray[j+1])
           swap(j, j+1);
        NDC.pop();
        }
        NDC.pop();
      }
    }

    void dump() {
      if(! (this.intArray instanceof int[])) {
        DUMP.error("Tried to dump an uninitialized array.");
        return;
      }
      DUMP.info("Dump of integer array:");
      for(int i = 0; i < this.intArray.length; i++) {
        DUMP.info("Element [" + i + "]=" + this.intArray[i]);
      }
    }

    void swap(int l, int r) {
      // It is poor practice to ship code with log staments in tight
      // loops or code called potentially millions of times.
      SWAP.debug( "Swapping intArray["+l+"]=" + intArray[l] +
                             " and intArray["+r+"]=" + intArray[r]);
      int temp = this.intArray[l];
      this.intArray[l] = this.intArray[r];
      this.intArray[r] = temp;
    }
  }





-- Ceki Gülcü

For log4j documentation consider "The complete log4j manual"
ISBN: 2970036908 http://www.qos.ch/shop/products/clm_t.jsp




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to