You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Arwen Pond <ap...@book.com> on 2014/08/06 19:42:17 UTC

Set the log level based on command line args

I am investigating migrating from log4j 1 to log4j 2.  A pattern that I have in many of my applications is setting the log level based on command line arguments.  So if the -v (verbose) option is passed I set the log level to Info.  The default is set to warn.
    if (line.hasOption("v"))
    {
      logger.setLevel(Level.INFO);
    }

    if (line.hasOption("d"))
    {
      logger.setLevel(Level.DEBUG);
    }

In the world of log4j 2, what is the recommended way to accomplish this?

Thanks,
Arwen



This electronic mail message contains information that (a) is or 
may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE 
PROTECTED 
BY LAW FROM DISCLOSURE, and (b) is intended only for the use of 
the addressee(s) named herein.  If you are not an intended 
recipient, please contact the sender immediately and take the 
steps necessary to delete the message completely from your 
computer system.

Not Intended as a Substitute for a Writing: Notwithstanding the 
Uniform Electronic Transaction Act or any other law of similar 
effect, absent an express statement to the contrary, this e-mail 
message, its contents, and any attachments hereto are not 
intended 
to represent an offer or acceptance to enter into a contract and 
are not otherwise intended to bind this sender, 
barnesandnoble.com 
llc, barnesandnoble.com inc. or any other person or entity.

RE: Set the log level based on command line args

Posted by Arwen Pond <ap...@book.com>.
This is what I have tried:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5p [%t]: %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="debug" level="debug">
            <AppenderRef ref="Console"/>
        </Logger>

        <Logger name="verbose" level="info">
            <AppenderRef ref="Console"/>
        </Logger>

        <Logger name="normal" level="warn">
            <AppenderRef ref="Console"/>
        </Logger>
    </Loggers>
</Configuration>

Then I call this:
Logger logger = LogManager.getLogger("verbose");
logger.info("Hello World");

But I see duplicated lines in the output.
INFO  [main]: Hello World
11:36:18.191 [main] INFO  verbose - Hello World

What am I doing wrong?

Thanks,
Arwen

-----Original Message-----
From: Arwen Pond 
Sent: Wednesday, August 06, 2014 10:42 AM
To: log4j-user@logging.apache.org
Subject: Set the log level based on command line args

I am investigating migrating from log4j 1 to log4j 2.  A pattern that I have in many of my applications is setting the log level based on command line arguments.  So if the -v (verbose) option is passed I set the log level to Info.  The default is set to warn.
    if (line.hasOption("v"))
    {
      logger.setLevel(Level.INFO);
    }

    if (line.hasOption("d"))
    {
      logger.setLevel(Level.DEBUG);
    }

In the world of log4j 2, what is the recommended way to accomplish this?

Thanks,
Arwen



This electronic mail message contains information that (a) is or may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE PROTECTED BY LAW FROM DISCLOSURE, and (b) is intended only for the use of the addressee(s) named herein.  If you are not an intended recipient, please contact the sender immediately and take the steps necessary to delete the message completely from your computer system.

Not Intended as a Substitute for a Writing: Notwithstanding the Uniform Electronic Transaction Act or any other law of similar effect, absent an express statement to the contrary, this e-mail message, its contents, and any attachments hereto are not intended to represent an offer or acceptance to enter into a contract and are not otherwise intended to bind this sender, barnesandnoble.com llc, barnesandnoble.com inc. or any other person or entity.

This electronic mail message contains information that (a) is or 
may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE 
PROTECTED 
BY LAW FROM DISCLOSURE, and (b) is intended only for the use of 
the addressee(s) named herein.  If you are not an intended 
recipient, please contact the sender immediately and take the 
steps necessary to delete the message completely from your 
computer system.

Not Intended as a Substitute for a Writing: Notwithstanding the 
Uniform Electronic Transaction Act or any other law of similar 
effect, absent an express statement to the contrary, this e-mail 
message, its contents, and any attachments hereto are not 
intended 
to represent an offer or acceptance to enter into a contract and 
are not otherwise intended to bind this sender, 
barnesandnoble.com 
llc, barnesandnoble.com inc. or any other person or entity.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Set the log level based on command line args

Posted by Matt Sicker <bo...@gmail.com>.
I'm pretty sure the root logger has an implicit default configuration if
it's missing from your config file.


On 6 August 2014 15:57, Arwen Pond <ap...@book.com> wrote:

> Do you have to specify a root logger in the configuration?
>
> additivity="false" solved my issue.  Thanks.
>
> -----Original Message-----
> From: Ralph Goers [mailto:ralph.goers@dslextreme.com]
> Sent: Wednesday, August 06, 2014 1:17 PM
> To: Log4J Users List
> Subject: Re: Set the log level based on command line args
>
> I should also add that your configuration didn't show a root logger.
>  There is always a root logger so I suspect you are also logging to it.  If
> you make all your loggers be configured with additivity="false" that should
> stop happening.
>
> Ralph
>
> On Aug 6, 2014, at 1:13 PM, Ralph Goers <ra...@dslextreme.com>
> wrote:
>
> > Please see
> http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity
> >
> > Ralph
> >
>
>
> This electronic mail message contains information that (a) is or
> may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE
> PROTECTED
> BY LAW FROM DISCLOSURE, and (b) is intended only for the use of
> the addressee(s) named herein.  If you are not an intended
> recipient, please contact the sender immediately and take the
> steps necessary to delete the message completely from your
> computer system.
>
> Not Intended as a Substitute for a Writing: Notwithstanding the
> Uniform Electronic Transaction Act or any other law of similar
> effect, absent an express statement to the contrary, this e-mail
> message, its contents, and any attachments hereto are not
> intended
> to represent an offer or acceptance to enter into a contract and
> are not otherwise intended to bind this sender,
> barnesandnoble.com
> llc, barnesandnoble.com inc. or any other person or entity.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>


-- 
Matt Sicker <bo...@gmail.com>

RE: Set the log level based on command line args

Posted by Arwen Pond <ap...@book.com>.
Do you have to specify a root logger in the configuration?

additivity="false" solved my issue.  Thanks.

-----Original Message-----
From: Ralph Goers [mailto:ralph.goers@dslextreme.com] 
Sent: Wednesday, August 06, 2014 1:17 PM
To: Log4J Users List
Subject: Re: Set the log level based on command line args

I should also add that your configuration didn't show a root logger.  There is always a root logger so I suspect you are also logging to it.  If you make all your loggers be configured with additivity="false" that should stop happening.

Ralph

On Aug 6, 2014, at 1:13 PM, Ralph Goers <ra...@dslextreme.com> wrote:

> Please see http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity
> 
> Ralph
> 


This electronic mail message contains information that (a) is or 
may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE 
PROTECTED 
BY LAW FROM DISCLOSURE, and (b) is intended only for the use of 
the addressee(s) named herein.  If you are not an intended 
recipient, please contact the sender immediately and take the 
steps necessary to delete the message completely from your 
computer system.

Not Intended as a Substitute for a Writing: Notwithstanding the 
Uniform Electronic Transaction Act or any other law of similar 
effect, absent an express statement to the contrary, this e-mail 
message, its contents, and any attachments hereto are not 
intended 
to represent an offer or acceptance to enter into a contract and 
are not otherwise intended to bind this sender, 
barnesandnoble.com 
llc, barnesandnoble.com inc. or any other person or entity.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Set the log level based on command line args

Posted by Ralph Goers <ra...@dslextreme.com>.
I should also add that your configuration didn’t show a root logger.  There is always a root logger so I suspect you are also logging to it.  If you make all your loggers be configured with additivity=“false” that should stop happening.

Ralph

On Aug 6, 2014, at 1:13 PM, Ralph Goers <ra...@dslextreme.com> wrote:

> Please see http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity
> 
> Ralph
> 


Re: Set the log level based on command line args

Posted by Ralph Goers <ra...@dslextreme.com>.
Please see http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity

Ralph

On Aug 6, 2014, at 10:42 AM, Arwen Pond <ap...@book.com> wrote:

> I am investigating migrating from log4j 1 to log4j 2.  A pattern that I have in many of my applications is setting the log level based on command line arguments.  So if the -v (verbose) option is passed I set the log level to Info.  The default is set to warn.
>    if (line.hasOption("v"))
>    {
>      logger.setLevel(Level.INFO);
>    }
> 
>    if (line.hasOption("d"))
>    {
>      logger.setLevel(Level.DEBUG);
>    }
> 
> In the world of log4j 2, what is the recommended way to accomplish this?
> 
> Thanks,
> Arwen
> 
> 
> 
> This electronic mail message contains information that (a) is or
> may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE
> PROTECTED
> BY LAW FROM DISCLOSURE, and (b) is intended only for the use of
> the addressee(s) named herein.  If you are not an intended
> recipient, please contact the sender immediately and take the
> steps necessary to delete the message completely from your
> computer system.
> 
> Not Intended as a Substitute for a Writing: Notwithstanding the
> Uniform Electronic Transaction Act or any other law of similar
> effect, absent an express statement to the contrary, this e-mail
> message, its contents, and any attachments hereto are not
> intended
> to represent an offer or acceptance to enter into a contract and
> are not otherwise intended to bind this sender,
> barnesandnoble.com
> llc, barnesandnoble.com inc. or any other person or entity.