Proposal:

What: Making the Log-Level of the root category manageable by the BasicConfigurator

How: Adding the methods
     configure(Level level)
     and
     configure(Appender appender, Level level)

Why: In some cases it would be nice to turn of low level (for instance DEBUG) loggings
     without or before using a config file. Enableing this in the BasicConfigurator is
     more convenient for the Developer as going over the root Logger itself
     Example 1: A small (Test?) project, where using a conf file is to much overhead
     Example 2: (A real case that brought me to change the code)
        Imaging a project that uses a XML Config file to config the names and pathes of
        all the needed files (And of course some other stuff). One of these files is 
the
        Log4J conf file. Now this project uses some XML-Helper classes to read the
        programs config file which are also using Log4J logging.
        This leads to the case, that when we use only the BasicConfigurator at the
        beginning, we get a lot of "debug" log statments out of the
        "read-the-xml-configfile-helper-classes" until we have extracted the path
        of the Log4J conf file so we can use it. This is pretty annoying, because I
        allready know my helper-classes are working and don't want to see the debug
        and info loggings.

I added two Versions of BasicConfigurator with 4 configure methods.

The 1st version is similar to the existing version and every method do its own stuff.

The 2nd version makes the settings only in the configure(appender, level) method.
The other 3 methods only substitue the "missing" settings and call configure(appender, 
level) with these.

I like the 2nd Version more, but i think this is a question of personal Taste




Code Version 1
#######################################################################
/*
 * 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.  */

// Contibutors: "Luke Blanshard" <[EMAIL PROTECTED]>
//              "Mark DONSZELMANN" <[EMAIL PROTECTED]>
//              "Muly Oved" <[EMAIL PROTECTED]>
//              "Simon Klaiber" <[EMAIL PROTECTED]>

package org.apache.log4j;

import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.RendererSupport;
import org.apache.log4j.or.ObjectRenderer;
import org.apache.log4j.or.RendererMap;
import java.util.Enumeration;

/**
   Use this class to quickly configure the package.

   <p>For file based configuration see {@link
   PropertyConfigurator}. For XML based configuration see {@link
   org.apache.log4j.xml.DOMConfigurator DOMConfigurator}.

   @since 0.8.1
   @author Ceki G&uuml;lc&uuml; */
public class BasicConfigurator {

  protected BasicConfigurator() {
  }

  /**
     Add a {@link FileAppender} that uses {@link PatternLayout} using
     the {@link PatternLayout#TTCC_CONVERSION_PATTERN} and prints to
     <code>System.out</code> to the root category and sets the LogLevel of
     the root category to DEBUG.  */
  static
  public
  void configure() {
    Logger root = Logger.getRoot();
    root.addAppender(new ConsoleAppender(
           new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
  }

  /**
     Add <code>appender</code> to the root category.
     @param appender The appender to add to the root category.
  */
  static
  public
  void configure(Appender appender) {
    Logger root = Logger.getRoot();
    root.addAppender(appender);
  }

  /**
     Add a {@link FileAppender} that uses {@link PatternLayout} using
     the {@link PatternLayout#TTCC_CONVERSION_PATTERN} and prints to
     <code>System.out</code> to the root category and sets the LogLevel of
     the root category to <code>level</code>.
     @param level The Log Level that should be used
  */
  static
  public
  void configure(Level level) {
    Logger root = Logger.getRoot();
    root.addAppender(new ConsoleAppender(
           new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
    root.setLevel(level);
  }

   /**
     Add <code>appender</code> to the root category and sets the LogLevel of
     the root category to <code>level</code>.
     @param appender The appender to add to the root category.
     @param level The Log Level that should be used
  */
  static
  public
  void configure(Appender appender, Level level) {
    Logger root = Logger.getRoot();
    root.addAppender(appender);
    root.setLevel(level);
  }

  /**
     Reset the default hierarchy to its defaut. It is equivalent to
     calling
     <code>Category.getDefaultHierarchy().resetConfiguration()</code>.

     See {@link Hierarchy#resetConfiguration()} for more details.  */
  public
  static
  void resetConfiguration() {
    LogManager.resetConfiguration();
  }
}
########################################################################






Code Version 2
########################################################################
/*
 * 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.  */

// Contibutors: "Luke Blanshard" <[EMAIL PROTECTED]>
//              "Mark DONSZELMANN" <[EMAIL PROTECTED]>
//              "Muly Oved" <[EMAIL PROTECTED]>
//              "Simon Klaiber" <[EMAIL PROTECTED]>

package org.apache.log4j;

import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.RendererSupport;
import org.apache.log4j.or.ObjectRenderer;
import org.apache.log4j.or.RendererMap;
import java.util.Enumeration;

/**
   Use this class to quickly configure the package.

   <p>For file based configuration see {@link
   PropertyConfigurator}. For XML based configuration see {@link
   org.apache.log4j.xml.DOMConfigurator DOMConfigurator}.

   @since 0.8.1
   @author Ceki G&uuml;lc&uuml; */
public class BasicConfigurator {

  protected BasicConfigurator() {
  }

  /**
     Add a {@link FileAppender} that uses {@link PatternLayout} using
     the {@link PatternLayout#TTCC_CONVERSION_PATTERN} and prints to
     <code>System.out</code> to the root category and sets the LogLevel of
     the root category to DEBUG.  */
  static
  public
  void configure() {
    ConsoleAppender appender = new ConsoleAppender(
      new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
    Level level =  Level.DEBUG;
    configure(appender, level);
  }

  /**
     Add <code>appender</code> to the root category.
     @param appender The appender to add to the root category.
  */
  static
  public
  void configure(Appender appender) {
    Level level =  Level.DEBUG;
    configure(appender, level);
  }

  /**
     Add a {@link FileAppender} that uses {@link PatternLayout} using
     the {@link PatternLayout#TTCC_CONVERSION_PATTERN} and prints to
     <code>System.out</code> to the root category and sets the LogLevel of
     the root category to <code>level</code>.
     @param level The Log Level that should be used
  */
  static
  public
  void configure(Level level) {
    ConsoleAppender appender = new ConsoleAppender(
      new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
    configure(appender, level);
  }

   /**
     Add <code>appender</code> to the root category and sets the LogLevel of
     the root category to <code>level</code>.
     @param appender The appender to add to the root category.
     @param level The Log Level that should be used
  */
  static
  public
  void configure(Appender appender, Level level) {
    Logger root = Logger.getRoot();
    root.addAppender(appender);
    root.setLevel(level);
  }

  /**
     Reset the default hierarchy to its defaut. It is equivalent to
     calling
     <code>Category.getDefaultHierarchy().resetConfiguration()</code>.

     See {@link Hierarchy#resetConfiguration()} for more details.  */
  public
  static
  void resetConfiguration() {
    LogManager.resetConfiguration();
  }
}

########################################################################


Bye
Simon Klaiber
Frankfurt Germany

--------------------------------------------------------------------
mail2web - Check your email from the web at
http://mail2web.com/ .


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

Reply via email to