You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by di...@multitask.com.au on 2003/08/11 03:12:10 UTC

[logging] How do I change logging levels at run time?

I've got a application (maven) that uses commons logging.

We have a command line switch (-debug) that I'd like to set the logging 
level for everything to 'debug'.

Is this possible with commons-logging, or must we use log4j to do this?
--
dIon Gillard, Multitask Consulting
Blog:      http://blogs.codehaus.org/people/dion/

Re: [logging] How do I change logging levels at run time?

Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Mon, 11 Aug 2003, Henning P. Schmiedehausen wrote:

> Date: Mon, 11 Aug 2003 07:20:58 +0000 (UTC)
> From: Henning P. Schmiedehausen <hp...@intermeta.de>
> Reply-To: Jakarta Commons Users List <co...@jakarta.apache.org>,
>      hps@intermeta.de
> To: commons-user@jakarta.apache.org
> Newsgroups: hometree.jakarta.commons.user
> Subject: Re: [logging] How do I change logging levels at run time?
>
> "Craig R. McClanahan" <cr...@apache.org> writes:
>
> >On Mon, 11 Aug 2003 dion@multitask.com.au wrote:
>
> >> Is this possible with commons-logging, or must we use log4j to do this?
>
> >You'll need to use your underlying logging system's configuration
> >mechanism for stuff like this.
>
> Hi Dion, Craig,
>
> I was thinking about this when I read about it on the maven-dev list
> and this is a suggestion how to solve it (I don't know much about the
> innard of commons-logging, I'm just a happy user):
>
> The problem is, that the log level is hard coded into the method names.
> You have "log.error, log.debug, log.notice" on the Log interface.
>
> So let's extend it with a new level called "default" (if you know any
> better name, good. I wasn't able to invent a better one. ;-) :
>
>     /**
>      * <p> Is default logging currently enabled? </p>
>      *
>      * <p> Call this method to prevent having to perform expensive operations
>      * (for example, <code>String</code> concatination)
>      * when the log level is more than the default level. </p>
>      */
>     public boolean isDefaultEnabled();
>
>    /**
>     * <p> Log a message with default log level. </p>
>     *
>     * @param message log this message
>     */
>    public void default (Object message);
>
> Which would use a "default" log level set by a System.property or any
> other means. This would allow an application to log its stuff at debug
> level when developing and at application level when running.
>
> In the Log4JLogger this would be quite a simple implementation:
>
> public void default (Object message) {
>   logger.log(FQCN, currentPrio, message, null);
> }
>
> with isDefaultEnabled a little more heavy because one would have to
> look which is<xxx>Enabled method on the logger is to be used. This
> would need a little reflection or a simple switch(currentPrio)
> statement.
>
> Getting the currentPrio setting in the C'tor of Log4JLogger from a
> System.property shouldn't be a problem.
>
> I'd like to see this, it would add flexibility to the commons-logging
> system while keeping the implementation independence that I like from
> it.
>
> Comments?
>

One very large issue is that Log is an interface, so that adding a new
method like this would break every existing Log implementation in the
world.  That's not a good thing to do to users.

As an alternative, you can certainly create your own Log implementation
that simply delegates calls to the standard c-l version, but adds the
extra method signatures.

> 	Regards
> 		Henning
>
>
>
>

Craig


Re: [logging] How do I change logging levels at run time?

Posted by "Henning P. Schmiedehausen" <hp...@intermeta.de>.
"Craig R. McClanahan" <cr...@apache.org> writes:

>On Mon, 11 Aug 2003 dion@multitask.com.au wrote:

>> Is this possible with commons-logging, or must we use log4j to do this?

>You'll need to use your underlying logging system's configuration
>mechanism for stuff like this.

Hi Dion, Craig,

I was thinking about this when I read about it on the maven-dev list
and this is a suggestion how to solve it (I don't know much about the
innard of commons-logging, I'm just a happy user):

The problem is, that the log level is hard coded into the method names.
You have "log.error, log.debug, log.notice" on the Log interface. 

So let's extend it with a new level called "default" (if you know any
better name, good. I wasn't able to invent a better one. ;-) :

    /**
     * <p> Is default logging currently enabled? </p>
     *
     * <p> Call this method to prevent having to perform expensive operations
     * (for example, <code>String</code> concatination)
     * when the log level is more than the default level. </p>
     */
    public boolean isDefaultEnabled();

   /**
    * <p> Log a message with default log level. </p>
    *
    * @param message log this message
    */
   public void default (Object message);

Which would use a "default" log level set by a System.property or any
other means. This would allow an application to log its stuff at debug
level when developing and at application level when running.

In the Log4JLogger this would be quite a simple implementation:

public void default (Object message) {
  logger.log(FQCN, currentPrio, message, null);
}

with isDefaultEnabled a little more heavy because one would have to
look which is<xxx>Enabled method on the logger is to be used. This
would need a little reflection or a simple switch(currentPrio)
statement.

Getting the currentPrio setting in the C'tor of Log4JLogger from a
System.property shouldn't be a problem.

I'd like to see this, it would add flexibility to the commons-logging
system while keeping the implementation independence that I like from
it.

Comments?

	Regards
		Henning



-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

Java, perl, Solaris, Linux, xSP Consulting, Web Services 
freelance consultant -- Jakarta Turbine Development  -- hero for hire

"You are being far too rational for this discussion."  
       --- Scott Robert Ladd in <3F...@coyotegulch.com>

Re: [logging] How do I change logging levels at run time?

Posted by di...@multitask.com.au.
Thanks Craig,

since we include log4 in our dependencies, I think we'll go for the 
assumption that it's there and see if we can add other supported 
environments (jdk14 should just be properties too) as we go.
--
dIon Gillard, Multitask Consulting
Blog:      http://blogs.codehaus.org/people/dion/


"Craig R. McClanahan" <cr...@apache.org> wrote on 11/08/2003 11:27:51 
AM:

> On Mon, 11 Aug 2003 dion@multitask.com.au wrote:
> 
> > Date: Mon, 11 Aug 2003 11:12:10 +1000
> > From: dion@multitask.com.au
> > Reply-To: Jakarta Commons Users List <co...@jakarta.apache.org>
> > To: commons-user@jakarta.apache.org
> > Subject: [logging] How do I change logging levels at run time?
> >
> > I've got a application (maven) that uses commons logging.
> >
> > We have a command line switch (-debug) that I'd like to set the 
logging
> > level for everything to 'debug'.
> >
> > Is this possible with commons-logging, or must we use log4j to do 
this?
> 
> You'll need to use your underlying logging system's configuration
> mechanism for stuff like this.
> 
> If you're using the simple logger that is built in to commons-logging, 
you
> can probably accomplish this by calling System.setProperty() on the
> appropriate property names.
> 
> > --
> > dIon Gillard, Multitask Consulting
> > Blog:      http://blogs.codehaus.org/people/dion/
> >
> 
> Craig
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 

Re: [logging] How do I change logging levels at run time?

Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Mon, 11 Aug 2003 dion@multitask.com.au wrote:

> Date: Mon, 11 Aug 2003 11:12:10 +1000
> From: dion@multitask.com.au
> Reply-To: Jakarta Commons Users List <co...@jakarta.apache.org>
> To: commons-user@jakarta.apache.org
> Subject: [logging] How do I change logging levels at run time?
>
> I've got a application (maven) that uses commons logging.
>
> We have a command line switch (-debug) that I'd like to set the logging
> level for everything to 'debug'.
>
> Is this possible with commons-logging, or must we use log4j to do this?

You'll need to use your underlying logging system's configuration
mechanism for stuff like this.

If you're using the simple logger that is built in to commons-logging, you
can probably accomplish this by calling System.setProperty() on the
appropriate property names.

> --
> dIon Gillard, Multitask Consulting
> Blog:      http://blogs.codehaus.org/people/dion/
>

Craig