You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "simon@klaiber.com" <si...@klaiber.com> on 2002/01/28 14:10:14 UTC

Proposal: Setting Level with BasicConfigurator

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" <Lu...@quiq.com>
//              "Mark DONSZELMANN" <Ma...@cern.ch>
//              "Muly Oved" <mu...@hotmail.com>
//              "Simon Klaiber" <si...@klaiber.com>

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" <Lu...@quiq.com>
//              "Mark DONSZELMANN" <Ma...@cern.ch>
//              "Muly Oved" <mu...@hotmail.com>
//              "Simon Klaiber" <si...@klaiber.com>

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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


AW: Proposal: Setting Level with BasicConfigurator

Posted by Simon Klaiber <si...@klaiber.com>.
Hi Ceki,

> Simon,
>
> The BasicConfigurator is just a way to get log4j to print something
> very quickly. For more flexibility you can configure log4j
> programmatically or use configuration files.
>
> To set the level of the root logger to INFO level just do:
>
>    Logger.getRootLogger().setLevel(Level.INFO);
>
> I feel very bad about rejecting your contribution but I am afraid that
> is the right thing to do.

on one side you are right, on the other it is most convinient for the
log4j user (think especially of a new user)when he don't have to do the
stuff programmatical and therefore has not to learn a lot about the Api
For simple settings BasicConfigurator for more complicated one of the
file-based configurator. So Everything a starter needs to now, is how to
use a Configurator, How to get a Instance of a Category (Logger), how to
log something. As more convinient the "jump-in" for a new user is, as higher
is the acceptance of the packet.
But i think to the basic setting belongs teh Log Level also. There is
just a "convinience-gap" between the Basic- and File-Based Configurators.

When follow your argumentation consequently - just to get some printout -
you should deprecate the configure(Appender appender) method also, because
the
configure() method is more than enough for this on any machine.

Just think it over again. If yopu are still against it, I wont be mad at
you.

[...]
>
> As a general rule, helper classes, libraries, non-top level components
> should not attempt to configure log4j. Configuring log4j is the
> responsibility of the end-user or generally the application
> deployer. Whenever possible, a library should not try to configure
> logging but leave it to the deployer. After all, logging output is
> useful only if someone will take the time to look at them. If the
> end-user wishes to log then she should control the logging
> configuration. Nevertheless, it is helpful if the library developper
> provides documentation on logging, preferably with working examples.
>

Because of the responibilkity of the enduser log configuration shoud be
freely configurable, also in "out of the box" software. for this you need
sometimes a more complicated config loading procedure. This is where this
problem arise.


Byer Simon


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


Re: Proposal: Setting Level with BasicConfigurator

Posted by Ceki Gülcü <ce...@qos.ch>.
Simon,

The BasicConfigurator is just a way to get log4j to print something
very quickly. For more flexibility you can configure log4j
programmatically or use configuration files. 

To set the level of the root logger to INFO level just do:

   Logger.getRootLogger().setLevel(Level.INFO);

I feel very bad about rejecting your contribution but I am afraid that
is the right thing to do. 

At 08:10 28.01.2002 -0500, you wrote:
>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.

As a general rule, helper classes, libraries, non-top level components
should not attempt to configure log4j. Configuring log4j is the
responsibility of the end-user or generally the application
deployer. Whenever possible, a library should not try to configure
logging but leave it to the deployer. After all, logging output is
useful only if someone will take the time to look at them. If the
end-user wishes to log then she should control the logging
configuration. Nevertheless, it is helpful if the library developper
provides documentation on logging, preferably with working examples.



--
Ceki Gülcü



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