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 Phil Wray <ph...@db.com> on 2014/06/23 12:44:57 UTC

how to change logging level for a class at runtime

Classification: Public

Hi,
Would like to turn on debug for a particular class or package at runtime, 
either via jmx or programmatically, where this class or package is 
arbitrary and not known at start up time please.
I can't see a way to do this?
Previous posts titled "Programmatic configuration of loggers" show how to 
do this for an existing named logger but not for an arbitrary class or 
package which is a typical use case.
LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);

But this returns the root logger if no existing logger for that name 
exists - and we can't just enable debug on everything.
Is there a way of adding a new LoggerConfig into the configuration?

thanks,
Phil Wray




---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.

Re: how to change logging level for a class at runtime

Posted by Ralph Goers <ra...@dslextreme.com>.
In looking at the JMX support it doesn’t look like the ability to do that was implemented. Perhaps you could create a Jira request for that enhancement?

Ralph

On Jun 23, 2014, at 7:55 AM, Ralph Goers <ra...@dslextreme.com> wrote:

> Please see http://people.apache.org/~rgoers/log4j2/manual/customconfig.html
> 
> Ralph
> 
> On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:
> 
>> Classification: Public
>> 
>> Hi,
>> Would like to turn on debug for a particular class or package at runtime, 
>> either via jmx or programmatically, where this class or package is 
>> arbitrary and not known at start up time please.
>> I can't see a way to do this?
>> Previous posts titled "Programmatic configuration of loggers" show how to 
>> do this for an existing named logger but not for an arbitrary class or 
>> package which is a typical use case.
>> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
>> 
>> But this returns the root logger if no existing logger for that name 
>> exists - and we can't just enable debug on everything.
>> Is there a way of adding a new LoggerConfig into the configuration?
>> 
>> thanks,
>> Phil Wray
>> 
>> 
>> 
>> 
>> ---
>> 
>> This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
>> 
>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.
> 


Re: how to change logging level for a class at runtime

Posted by Phil Wray <ph...@db.com>.
Classification: Public

Hi Ralph,
Thanks for getting back, but the examples do not show how to change levels 
at runtime,

There is no method addLogger(String, LoggerConfig) method on the 
Configuration interface.
The addLogger method exists on BaseConfiguration, however it throws an 
IllegalStateException as its already started, and says you have to create 
a new configuration.

Is it actually possible to change the logging level for a class/package at 
runtime after startup?
I think this is a common use case for troubleshooting critical long 
running processes.
 

thanks,
Phil






From:
Ralph Goers <ra...@dslextreme.com>
To:
Log4J Users List <lo...@logging.apache.org>, 
Date:
23/06/2014 15:57
Subject:
Re: how to change logging level for a class at runtime



Please see 
http://people.apache.org/~rgoers/log4j2/manual/customconfig.html

Ralph

On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:

> Classification: Public
> 
> Hi,
> Would like to turn on debug for a particular class or package at 
runtime, 
> either via jmx or programmatically, where this class or package is 
> arbitrary and not known at start up time please.
> I can't see a way to do this?
> Previous posts titled "Programmatic configuration of loggers" show how 
to 
> do this for an existing named logger but not for an arbitrary class or 
> package which is a typical use case.
> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
> 
> But this returns the root logger if no existing logger for that name 
> exists - and we can't just enable debug on everything.
> Is there a way of adding a new LoggerConfig into the configuration?
> 
> thanks,
> Phil Wray
> 
> 
> 
> 
> ---
> 
> This e-mail may contain confidential and/or privileged information. If 
you are not the intended recipient (or have received this e-mail in error) 
please notify the sender immediately and delete this e-mail. Any 
unauthorized copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
> 
> Please refer to http://www.db.com/en/content/eu_disclosures.htm for 
additional EU corporate and regulatory disclosures and to 
http://www.db.com/unitedkingdom/content/privacy.htm for information about 
privacy.







---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.

Re: how to change logging level for a class at runtime

Posted by Phil Wray <ph...@db.com>.
Classification: Public

Hi Ralph,
Sorry hadn't looked at the latest source  - see you've made changes to 
Configuration to allow adding of LoggerConfig via the interface that are 
not yet in the release candidate jar.
I'll take the latest source and build from that.


thanks,
Phil





From:
Phil Wray/ext/dbcom
To:
"Log4J Users List" <lo...@logging.apache.org>, 
Date:
24/06/2014 09:07
Subject:
Re: how to change logging level for a class at runtime


Classification: Public

Hi Ralph,
Thanks for getting back, but the examples do not show how to change levels 
at runtime,

There is no method addLogger(String, LoggerConfig) method on the 
Configuration interface.
The addLogger method exists on BaseConfiguration, however it throws an 
IllegalStateException as its already started, and says you have to create 
a new configuration.

Is it actually possible to change the logging level for a class/package at 
runtime after startup?
I think this is a common use case for troubleshooting critical long 
running processes.
 

thanks,
Phil







From:
Ralph Goers <ra...@dslextreme.com>
To:
Log4J Users List <lo...@logging.apache.org>, 
Date:
23/06/2014 15:57
Subject:
Re: how to change logging level for a class at runtime



Please see 
http://people.apache.org/~rgoers/log4j2/manual/customconfig.html

Ralph

On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:

> Classification: Public
> 
> Hi,
> Would like to turn on debug for a particular class or package at 
runtime, 
> either via jmx or programmatically, where this class or package is 
> arbitrary and not known at start up time please.
> I can't see a way to do this?
> Previous posts titled "Programmatic configuration of loggers" show how 
to 
> do this for an existing named logger but not for an arbitrary class or 
> package which is a typical use case.
> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
> 
> But this returns the root logger if no existing logger for that name 
> exists - and we can't just enable debug on everything.
> Is there a way of adding a new LoggerConfig into the configuration?
> 
> thanks,
> Phil Wray
> 
> 
> 
> 
> ---
> 
> This e-mail may contain confidential and/or privileged information. If 
you are not the intended recipient (or have received this e-mail in error) 
please notify the sender immediately and delete this e-mail. Any 
unauthorized copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
> 
> Please refer to http://www.db.com/en/content/eu_disclosures.htm for 
additional EU corporate and regulatory disclosures and to 
http://www.db.com/unitedkingdom/content/privacy.htm for information about 
privacy.








---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.

Re: how to change logging level for a class at runtime [I]

Posted by Phil Wray <ph...@db.com>.
Classification: For internal use only

LoggerConfigAdmin mbean instances only exist for explicitly configured 
loggers - so you can only change the level for loggers originally defined 
in your config file - not for an arbitrary class or package - which I've 
found a useful feature.


I've updated the jira with a patch file and 2 new classes - 
LoggerLevelAdmin and LoggerLevelAdminMBean.
Server changed to register a single LoggerLevelAdmin mbean that allows you 
to dynamically change level for any logger - creating a new loggerConfig 
item if necessary.
If someone could take a look at them would be great.


thanks,
Phil

Phil Wray
Developer

Deutsche Bank AG, Filiale London
Global Technology
20 Finsbury Circus, EC2M 1NB London, United Kingdom
Tel. +44(20)7547 4918
Mobile +44 (0)7815 780246
Email phil.wray@db.com




From:
Matt Sicker <bo...@gmail.com>
To:
Log4J Users List <lo...@logging.apache.org>, 
Date:
09/08/2014 02:35
Subject:
Re: how to change logging level for a class at runtime [I]



Is this not what you're looking for?

http://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/jmx/LoggerConfigAdminMBean.html



On 6 August 2014 22:44, Matt Sicker <bo...@gmail.com> wrote:

> I may take a look at this soon as I've been getting into JMX lately at
> work in order to automate some build/deployment infrastructure. It's 
still
> fresh ;)
>
>
> On 6 August 2014 17:42, Ralph Goers <ra...@dslextreme.com> wrote:
>
>> I wouldn?t have asked for a patch submission if I didn?t plan to 
consider
>> it. That said, we are all volunteers here and sometimes don?t get to 
things
>> as quickly as we would like.
>>
>> Ralph
>>
>> On Aug 6, 2014, at 1:24 PM, Ang Xu <ax...@linkedin.com.INVALID> wrote:
>>
>> > Hi,
>> >
>> > Is there any plan to merge this patch to the main branch? This
>> > functionality seems extremely useful when modifying config file is 
not
>> an
>> > option.
>> >
>> > Thanks,
>> > Ang
>> >
>> > On 6/25/14, 12:19 AM, "Phil Wray" <ph...@db.com> wrote:
>> >
>> >> Classification: For internal use only
>> >>
>> >> Hey Ralph.
>> >> Sure - have created:
>> >> LOG4J2-681 Add ability to change log level for any class/package
>> >> dynamically via jmx
>> >>
>> >> cheers,
>> >> Phil
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> From:
>> >> Ralph Goers <rg...@apache.org>
>> >> To:
>> >> Log4J Users List <lo...@logging.apache.org>,
>> >> Date:
>> >> 24/06/2014 15:34
>> >> Subject:
>> >> Re: how to change logging level for a class at runtime
>> >>
>> >>
>> >>
>> >> Phil,
>> >>
>> >> That looks pretty good.  It would be great if you could create a 
Jira
>> >> issue and add that as a patch
>> >>
>> >> Sent from my iPad
>> >>
>> >>> On Jun 24, 2014, at 7:01 AM, Phil Wray <ph...@db.com> wrote:
>> >>>
>> >>> Classification: Public
>> >>>
>> >>> Hi Ralph,
>> >>> Just to confirm running latest trunk version its possible to change
>> >> levels
>> >>> at runtime using code as per your docs.
>> >>> Thanks for the help.
>> >>> I see a tag for 2.0-rc2 was taken a couple of days ago - I'm 
guessing
>> >> this
>> >>> is being released imminently?
>> >>>
>> >>> In case it helps others I ended up with the following util to let 
us
>> >>> change levels via jmx, just using whatever existing appender the
>> logger
>> >>> would have had.
>> >>>
>> >>>       @ManagedOperation
>> >>>       public String changeLogLevel(String loggerName, String 
level){
>> >>>               Level newLevel = Level.toLevel(level);
>> >>>               LoggerContext ctx = (LoggerContext)
>> >> LogManager.getContext(
>> >>> false);
>> >>>               Configuration config = ctx.getConfiguration();
>> >>>               LoggerConfig loggerConfig =
>> >>> config.getLoggerConfig(loggerName);
>> >>>               String msg;
>> >>>
>> >>>               if (loggerName.equals(loggerConfig.getName())){
>> >>>                       Level oldLevel = loggerConfig.getLevel();
>> >>>                       loggerConfig.setLevel(newLevel);
>> >>>                       msg = String.format("Modified existing logger 
%s
>> >>> level from %s to %s", loggerName, oldLevel, newLevel);
>> >>>               } else {
>> >>>                       createCopyFrom(loggerConfig, loggerName,
>> >> newLevel,
>> >>> config);
>> >>>                       msg = String.format("Added new logger %s with
>> >>> level %s previously used logger %s with level %s", loggerName,
>> >>> newLevel,
>> >>
>> >>> loggerConfig.getName(), loggerConfig.getLevel());
>> >>>               }
>> >>>               ctx.updateLoggers();
>> >>>               LOG.info(msg);
>> >>>               return msg;
>> >>>       }
>> >>>
>> >>>       private void createCopyFrom(LoggerConfig original, String 
name,
>> >>> Level level, Configuration config){
>> >>>               AppenderRef[] refs = 
original.getAppenderRefs().toArray(
>> >>> new AppenderRef[]{});
>> >>>               Filter filter = original.getFilter();
>> >>>               LoggerConfig copy = LoggerConfig.createLogger(
>> >>>                               "false",
>> >>>                               level,
>> >>>                               name,
>> >>>                               Boolean.toString
>> >>> (original.isIncludeLocation()),
>> >>>                               refs, null, config, filter);
>> >>>
>> >>>               for (Appender appender :
>> >>> original.getAppenders().values()){
>> >>>                       copy.addAppender(appender, null, null);
>> >>>               }
>> >>>               config.addLogger(name, copy);
>> >>>       }
>> >>>
>> >>> cheers,
>> >>> Phil
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> From:
>> >>> Phil Wray/ext/dbcom
>> >>> To:
>> >>> "Log4J Users List" <lo...@logging.apache.org>,
>> >>> Date:
>> >>> 24/06/2014 11:16
>> >>> Subject:
>> >>> Re: how to change logging level for a class at runtime
>> >>>
>> >>>
>> >>> Classification: Public
>> >>>
>> >>> Hi Ralph,
>> >>> Sorry hadn't looked at the latest source  - see you've made changes 
to
>> >>> Configuration to allow adding of LoggerConfig via the interface 
that
>> >>> are
>> >>
>> >>> not yet in the release candidate jar.
>> >>> I'll take the latest source and build from that.
>> >>>
>> >>>
>> >>> thanks,
>> >>> Phil
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> From:
>> >>> Phil Wray/ext/dbcom
>> >>> To:
>> >>> "Log4J Users List" <lo...@logging.apache.org>,
>> >>> Date:
>> >>> 24/06/2014 09:07
>> >>> Subject:
>> >>> Re: how to change logging level for a class at runtime
>> >>>
>> >>>
>> >>> Classification: Public
>> >>>
>> >>> Hi Ralph,
>> >>> Thanks for getting back, but the examples do not show how to change
>> >> levels
>> >>> at runtime,
>> >>>
>> >>> There is no method addLogger(String, LoggerConfig) method on the
>> >>> Configuration interface.
>> >>> The addLogger method exists on BaseConfiguration, however it throws 
an
>> >>> IllegalStateException as its already started, and says you have to
>> >> create
>> >>> a new configuration.
>> >>>
>> >>> Is it actually possible to change the logging level for a
>> class/package
>> >> at
>> >>> runtime after startup?
>> >>> I think this is a common use case for troubleshooting critical long
>> >>> running processes.
>> >>>
>> >>>
>> >>> thanks,
>> >>> Phil
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> From:
>> >>> Ralph Goers <ra...@dslextreme.com>
>> >>> To:
>> >>> Log4J Users List <lo...@logging.apache.org>,
>> >>> Date:
>> >>> 23/06/2014 15:57
>> >>> Subject:
>> >>> Re: how to change logging level for a class at runtime
>> >>>
>> >>>
>> >>>
>> >>> Please see
>> >>> http://people.apache.org/~rgoers/log4j2/manual/customconfig.html
>> >>>
>> >>> Ralph
>> >>>
>> >>>> On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:
>> >>>>
>> >>>> Classification: Public
>> >>>>
>> >>>> Hi,
>> >>>> Would like to turn on debug for a particular class or package at
>> >>> runtime,
>> >>>> either via jmx or programmatically, where this class or package is
>> >>>> arbitrary and not known at start up time please.
>> >>>> I can't see a way to do this?
>> >>>> Previous posts titled "Programmatic configuration of loggers" show
>> how
>> >>> to
>> >>>> do this for an existing named logger but not for an arbitrary 
class
>> or
>> >>>> package which is a typical use case.
>> >>>> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
>> >>>>
>> >>>> But this returns the root logger if no existing logger for that 
name
>> >>>> exists - and we can't just enable debug on everything.
>> >>>> Is there a way of adding a new LoggerConfig into the 
configuration?
>> >>>>
>> >>>> thanks,
>> >>>> Phil Wray
>> >>>>
>> >>>>
>> >>>>
>> >>>>
>> >>>> ---
>> >>>>
>> >>>> This e-mail may contain confidential and/or privileged 
information.
>> If
>> >>> you are not the intended recipient (or have received this e-mail in
>> >> error)
>> >>> please notify the sender immediately and delete this e-mail. Any
>> >>> unauthorized copying, disclosure or distribution of the material in
>> >>> this
>> >>
>> >>> e-mail is strictly forbidden.
>> >>>>
>> >>>> Please refer to http://www.db.com/en/content/eu_disclosures.htm 
for
>> >>> additional EU corporate and regulatory disclosures and to
>> >>> http://www.db.com/unitedkingdom/content/privacy.htm for information
>> >> about
>> >>> privacy.
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> ---
>> >>>
>> >>> This e-mail may contain confidential and/or privileged information. 
If
>> >> you are not the intended recipient (or have received this e-mail in
>> >> error)
>> >> please notify the sender immediately and delete this e-mail. Any
>> >> unauthorized copying, disclosure or distribution of the material in
>> this
>> >> e-mail is strictly forbidden.
>> >>>
>> >>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>> >> additional EU corporate and regulatory disclosures and to
>> >> http://www.db.com/unitedkingdom/content/privacy.htm for information
>> about
>> >> privacy.
>> >>
>> >> 
---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> >> For additional commands, e-mail: log4j-user-help@logging.apache.org
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> ---
>> >>
>> >> This e-mail may contain confidential and/or privileged information. 
If
>> >> you are not the intended recipient (or have received this e-mail in
>> >> error) please notify the sender immediately and delete this e-mail. 
Any
>> >> unauthorized copying, disclosure or distribution of the material in
>> this
>> >> e-mail is strictly forbidden.
>> >>
>> >> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>> >> additional EU corporate and regulatory disclosures and to
>> >> http://www.db.com/unitedkingdom/content/privacy.htm for information
>> about
>> >> privacy.
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> > For additional commands, e-mail: log4j-user-help@logging.apache.org
>> >
>>
>>
>> ---------------------------------------------------------------------
>> 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>
>



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






---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.

Re: how to change logging level for a class at runtime [I]

Posted by Matt Sicker <bo...@gmail.com>.
Is this not what you're looking for?

http://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/jmx/LoggerConfigAdminMBean.html


On 6 August 2014 22:44, Matt Sicker <bo...@gmail.com> wrote:

> I may take a look at this soon as I've been getting into JMX lately at
> work in order to automate some build/deployment infrastructure. It's still
> fresh ;)
>
>
> On 6 August 2014 17:42, Ralph Goers <ra...@dslextreme.com> wrote:
>
>> I wouldn’t have asked for a patch submission if I didn’t plan to consider
>> it. That said, we are all volunteers here and sometimes don’t get to things
>> as quickly as we would like.
>>
>> Ralph
>>
>> On Aug 6, 2014, at 1:24 PM, Ang Xu <ax...@linkedin.com.INVALID> wrote:
>>
>> > Hi,
>> >
>> > Is there any plan to merge this patch to the main branch? This
>> > functionality seems extremely useful when modifying config file is not
>> an
>> > option.
>> >
>> > Thanks,
>> > Ang
>> >
>> > On 6/25/14, 12:19 AM, "Phil Wray" <ph...@db.com> wrote:
>> >
>> >> Classification: For internal use only
>> >>
>> >> Hey Ralph.
>> >> Sure - have created:
>> >> LOG4J2-681 Add ability to change log level for any class/package
>> >> dynamically via jmx
>> >>
>> >> cheers,
>> >> Phil
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> From:
>> >> Ralph Goers <rg...@apache.org>
>> >> To:
>> >> Log4J Users List <lo...@logging.apache.org>,
>> >> Date:
>> >> 24/06/2014 15:34
>> >> Subject:
>> >> Re: how to change logging level for a class at runtime
>> >>
>> >>
>> >>
>> >> Phil,
>> >>
>> >> That looks pretty good.  It would be great if you could create a Jira
>> >> issue and add that as a patch
>> >>
>> >> Sent from my iPad
>> >>
>> >>> On Jun 24, 2014, at 7:01 AM, Phil Wray <ph...@db.com> wrote:
>> >>>
>> >>> Classification: Public
>> >>>
>> >>> Hi Ralph,
>> >>> Just to confirm running latest trunk version its possible to change
>> >> levels
>> >>> at runtime using code as per your docs.
>> >>> Thanks for the help.
>> >>> I see a tag for 2.0-rc2 was taken a couple of days ago - I'm guessing
>> >> this
>> >>> is being released imminently?
>> >>>
>> >>> In case it helps others I ended up with the following util to let us
>> >>> change levels via jmx, just using whatever existing appender the
>> logger
>> >>> would have had.
>> >>>
>> >>>       @ManagedOperation
>> >>>       public String changeLogLevel(String loggerName, String level){
>> >>>               Level newLevel = Level.toLevel(level);
>> >>>               LoggerContext ctx = (LoggerContext)
>> >> LogManager.getContext(
>> >>> false);
>> >>>               Configuration config = ctx.getConfiguration();
>> >>>               LoggerConfig loggerConfig =
>> >>> config.getLoggerConfig(loggerName);
>> >>>               String msg;
>> >>>
>> >>>               if (loggerName.equals(loggerConfig.getName())){
>> >>>                       Level oldLevel = loggerConfig.getLevel();
>> >>>                       loggerConfig.setLevel(newLevel);
>> >>>                       msg = String.format("Modified existing logger %s
>> >>> level from %s to %s", loggerName, oldLevel, newLevel);
>> >>>               } else {
>> >>>                       createCopyFrom(loggerConfig, loggerName,
>> >> newLevel,
>> >>> config);
>> >>>                       msg = String.format("Added new logger %s with
>> >>> level %s previously used logger %s with level %s", loggerName,
>> >>> newLevel,
>> >>
>> >>> loggerConfig.getName(), loggerConfig.getLevel());
>> >>>               }
>> >>>               ctx.updateLoggers();
>> >>>               LOG.info(msg);
>> >>>               return msg;
>> >>>       }
>> >>>
>> >>>       private void createCopyFrom(LoggerConfig original, String name,
>> >>> Level level, Configuration config){
>> >>>               AppenderRef[] refs = original.getAppenderRefs().toArray(
>> >>> new AppenderRef[]{});
>> >>>               Filter filter = original.getFilter();
>> >>>               LoggerConfig copy = LoggerConfig.createLogger(
>> >>>                               "false",
>> >>>                               level,
>> >>>                               name,
>> >>>                               Boolean.toString
>> >>> (original.isIncludeLocation()),
>> >>>                               refs, null, config, filter);
>> >>>
>> >>>               for (Appender appender :
>> >>> original.getAppenders().values()){
>> >>>                       copy.addAppender(appender, null, null);
>> >>>               }
>> >>>               config.addLogger(name, copy);
>> >>>       }
>> >>>
>> >>> cheers,
>> >>> Phil
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> From:
>> >>> Phil Wray/ext/dbcom
>> >>> To:
>> >>> "Log4J Users List" <lo...@logging.apache.org>,
>> >>> Date:
>> >>> 24/06/2014 11:16
>> >>> Subject:
>> >>> Re: how to change logging level for a class at runtime
>> >>>
>> >>>
>> >>> Classification: Public
>> >>>
>> >>> Hi Ralph,
>> >>> Sorry hadn't looked at the latest source  - see you've made changes to
>> >>> Configuration to allow adding of LoggerConfig via the interface that
>> >>> are
>> >>
>> >>> not yet in the release candidate jar.
>> >>> I'll take the latest source and build from that.
>> >>>
>> >>>
>> >>> thanks,
>> >>> Phil
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> From:
>> >>> Phil Wray/ext/dbcom
>> >>> To:
>> >>> "Log4J Users List" <lo...@logging.apache.org>,
>> >>> Date:
>> >>> 24/06/2014 09:07
>> >>> Subject:
>> >>> Re: how to change logging level for a class at runtime
>> >>>
>> >>>
>> >>> Classification: Public
>> >>>
>> >>> Hi Ralph,
>> >>> Thanks for getting back, but the examples do not show how to change
>> >> levels
>> >>> at runtime,
>> >>>
>> >>> There is no method addLogger(String, LoggerConfig) method on the
>> >>> Configuration interface.
>> >>> The addLogger method exists on BaseConfiguration, however it throws an
>> >>> IllegalStateException as its already started, and says you have to
>> >> create
>> >>> a new configuration.
>> >>>
>> >>> Is it actually possible to change the logging level for a
>> class/package
>> >> at
>> >>> runtime after startup?
>> >>> I think this is a common use case for troubleshooting critical long
>> >>> running processes.
>> >>>
>> >>>
>> >>> thanks,
>> >>> Phil
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> From:
>> >>> Ralph Goers <ra...@dslextreme.com>
>> >>> To:
>> >>> Log4J Users List <lo...@logging.apache.org>,
>> >>> Date:
>> >>> 23/06/2014 15:57
>> >>> Subject:
>> >>> Re: how to change logging level for a class at runtime
>> >>>
>> >>>
>> >>>
>> >>> Please see
>> >>> http://people.apache.org/~rgoers/log4j2/manual/customconfig.html
>> >>>
>> >>> Ralph
>> >>>
>> >>>> On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:
>> >>>>
>> >>>> Classification: Public
>> >>>>
>> >>>> Hi,
>> >>>> Would like to turn on debug for a particular class or package at
>> >>> runtime,
>> >>>> either via jmx or programmatically, where this class or package is
>> >>>> arbitrary and not known at start up time please.
>> >>>> I can't see a way to do this?
>> >>>> Previous posts titled "Programmatic configuration of loggers" show
>> how
>> >>> to
>> >>>> do this for an existing named logger but not for an arbitrary class
>> or
>> >>>> package which is a typical use case.
>> >>>> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
>> >>>>
>> >>>> But this returns the root logger if no existing logger for that name
>> >>>> exists - and we can't just enable debug on everything.
>> >>>> Is there a way of adding a new LoggerConfig into the configuration?
>> >>>>
>> >>>> thanks,
>> >>>> Phil Wray
>> >>>>
>> >>>>
>> >>>>
>> >>>>
>> >>>> ---
>> >>>>
>> >>>> This e-mail may contain confidential and/or privileged information.
>> If
>> >>> you are not the intended recipient (or have received this e-mail in
>> >> error)
>> >>> please notify the sender immediately and delete this e-mail. Any
>> >>> unauthorized copying, disclosure or distribution of the material in
>> >>> this
>> >>
>> >>> e-mail is strictly forbidden.
>> >>>>
>> >>>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>> >>> additional EU corporate and regulatory disclosures and to
>> >>> http://www.db.com/unitedkingdom/content/privacy.htm for information
>> >> about
>> >>> privacy.
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> ---
>> >>>
>> >>> This e-mail may contain confidential and/or privileged information. If
>> >> you are not the intended recipient (or have received this e-mail in
>> >> error)
>> >> please notify the sender immediately and delete this e-mail. Any
>> >> unauthorized copying, disclosure or distribution of the material in
>> this
>> >> e-mail is strictly forbidden.
>> >>>
>> >>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>> >> additional EU corporate and regulatory disclosures and to
>> >> http://www.db.com/unitedkingdom/content/privacy.htm for information
>> about
>> >> privacy.
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> >> For additional commands, e-mail: log4j-user-help@logging.apache.org
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> ---
>> >>
>> >> This e-mail may contain confidential and/or privileged information. If
>> >> you are not the intended recipient (or have received this e-mail in
>> >> error) please notify the sender immediately and delete this e-mail. Any
>> >> unauthorized copying, disclosure or distribution of the material in
>> this
>> >> e-mail is strictly forbidden.
>> >>
>> >> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>> >> additional EU corporate and regulatory disclosures and to
>> >> http://www.db.com/unitedkingdom/content/privacy.htm for information
>> about
>> >> privacy.
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> > For additional commands, e-mail: log4j-user-help@logging.apache.org
>> >
>>
>>
>> ---------------------------------------------------------------------
>> 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>
>



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

Re: how to change logging level for a class at runtime [I]

Posted by Matt Sicker <bo...@gmail.com>.
I may take a look at this soon as I've been getting into JMX lately at work
in order to automate some build/deployment infrastructure. It's still fresh
;)


On 6 August 2014 17:42, Ralph Goers <ra...@dslextreme.com> wrote:

> I wouldn’t have asked for a patch submission if I didn’t plan to consider
> it. That said, we are all volunteers here and sometimes don’t get to things
> as quickly as we would like.
>
> Ralph
>
> On Aug 6, 2014, at 1:24 PM, Ang Xu <ax...@linkedin.com.INVALID> wrote:
>
> > Hi,
> >
> > Is there any plan to merge this patch to the main branch? This
> > functionality seems extremely useful when modifying config file is not an
> > option.
> >
> > Thanks,
> > Ang
> >
> > On 6/25/14, 12:19 AM, "Phil Wray" <ph...@db.com> wrote:
> >
> >> Classification: For internal use only
> >>
> >> Hey Ralph.
> >> Sure - have created:
> >> LOG4J2-681 Add ability to change log level for any class/package
> >> dynamically via jmx
> >>
> >> cheers,
> >> Phil
> >>
> >>
> >>
> >>
> >>
> >> From:
> >> Ralph Goers <rg...@apache.org>
> >> To:
> >> Log4J Users List <lo...@logging.apache.org>,
> >> Date:
> >> 24/06/2014 15:34
> >> Subject:
> >> Re: how to change logging level for a class at runtime
> >>
> >>
> >>
> >> Phil,
> >>
> >> That looks pretty good.  It would be great if you could create a Jira
> >> issue and add that as a patch
> >>
> >> Sent from my iPad
> >>
> >>> On Jun 24, 2014, at 7:01 AM, Phil Wray <ph...@db.com> wrote:
> >>>
> >>> Classification: Public
> >>>
> >>> Hi Ralph,
> >>> Just to confirm running latest trunk version its possible to change
> >> levels
> >>> at runtime using code as per your docs.
> >>> Thanks for the help.
> >>> I see a tag for 2.0-rc2 was taken a couple of days ago - I'm guessing
> >> this
> >>> is being released imminently?
> >>>
> >>> In case it helps others I ended up with the following util to let us
> >>> change levels via jmx, just using whatever existing appender the logger
> >>> would have had.
> >>>
> >>>       @ManagedOperation
> >>>       public String changeLogLevel(String loggerName, String level){
> >>>               Level newLevel = Level.toLevel(level);
> >>>               LoggerContext ctx = (LoggerContext)
> >> LogManager.getContext(
> >>> false);
> >>>               Configuration config = ctx.getConfiguration();
> >>>               LoggerConfig loggerConfig =
> >>> config.getLoggerConfig(loggerName);
> >>>               String msg;
> >>>
> >>>               if (loggerName.equals(loggerConfig.getName())){
> >>>                       Level oldLevel = loggerConfig.getLevel();
> >>>                       loggerConfig.setLevel(newLevel);
> >>>                       msg = String.format("Modified existing logger %s
> >>> level from %s to %s", loggerName, oldLevel, newLevel);
> >>>               } else {
> >>>                       createCopyFrom(loggerConfig, loggerName,
> >> newLevel,
> >>> config);
> >>>                       msg = String.format("Added new logger %s with
> >>> level %s previously used logger %s with level %s", loggerName,
> >>> newLevel,
> >>
> >>> loggerConfig.getName(), loggerConfig.getLevel());
> >>>               }
> >>>               ctx.updateLoggers();
> >>>               LOG.info(msg);
> >>>               return msg;
> >>>       }
> >>>
> >>>       private void createCopyFrom(LoggerConfig original, String name,
> >>> Level level, Configuration config){
> >>>               AppenderRef[] refs = original.getAppenderRefs().toArray(
> >>> new AppenderRef[]{});
> >>>               Filter filter = original.getFilter();
> >>>               LoggerConfig copy = LoggerConfig.createLogger(
> >>>                               "false",
> >>>                               level,
> >>>                               name,
> >>>                               Boolean.toString
> >>> (original.isIncludeLocation()),
> >>>                               refs, null, config, filter);
> >>>
> >>>               for (Appender appender :
> >>> original.getAppenders().values()){
> >>>                       copy.addAppender(appender, null, null);
> >>>               }
> >>>               config.addLogger(name, copy);
> >>>       }
> >>>
> >>> cheers,
> >>> Phil
> >>>
> >>>
> >>>
> >>>
> >>> From:
> >>> Phil Wray/ext/dbcom
> >>> To:
> >>> "Log4J Users List" <lo...@logging.apache.org>,
> >>> Date:
> >>> 24/06/2014 11:16
> >>> Subject:
> >>> Re: how to change logging level for a class at runtime
> >>>
> >>>
> >>> Classification: Public
> >>>
> >>> Hi Ralph,
> >>> Sorry hadn't looked at the latest source  - see you've made changes to
> >>> Configuration to allow adding of LoggerConfig via the interface that
> >>> are
> >>
> >>> not yet in the release candidate jar.
> >>> I'll take the latest source and build from that.
> >>>
> >>>
> >>> thanks,
> >>> Phil
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>> From:
> >>> Phil Wray/ext/dbcom
> >>> To:
> >>> "Log4J Users List" <lo...@logging.apache.org>,
> >>> Date:
> >>> 24/06/2014 09:07
> >>> Subject:
> >>> Re: how to change logging level for a class at runtime
> >>>
> >>>
> >>> Classification: Public
> >>>
> >>> Hi Ralph,
> >>> Thanks for getting back, but the examples do not show how to change
> >> levels
> >>> at runtime,
> >>>
> >>> There is no method addLogger(String, LoggerConfig) method on the
> >>> Configuration interface.
> >>> The addLogger method exists on BaseConfiguration, however it throws an
> >>> IllegalStateException as its already started, and says you have to
> >> create
> >>> a new configuration.
> >>>
> >>> Is it actually possible to change the logging level for a class/package
> >> at
> >>> runtime after startup?
> >>> I think this is a common use case for troubleshooting critical long
> >>> running processes.
> >>>
> >>>
> >>> thanks,
> >>> Phil
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>> From:
> >>> Ralph Goers <ra...@dslextreme.com>
> >>> To:
> >>> Log4J Users List <lo...@logging.apache.org>,
> >>> Date:
> >>> 23/06/2014 15:57
> >>> Subject:
> >>> Re: how to change logging level for a class at runtime
> >>>
> >>>
> >>>
> >>> Please see
> >>> http://people.apache.org/~rgoers/log4j2/manual/customconfig.html
> >>>
> >>> Ralph
> >>>
> >>>> On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:
> >>>>
> >>>> Classification: Public
> >>>>
> >>>> Hi,
> >>>> Would like to turn on debug for a particular class or package at
> >>> runtime,
> >>>> either via jmx or programmatically, where this class or package is
> >>>> arbitrary and not known at start up time please.
> >>>> I can't see a way to do this?
> >>>> Previous posts titled "Programmatic configuration of loggers" show how
> >>> to
> >>>> do this for an existing named logger but not for an arbitrary class or
> >>>> package which is a typical use case.
> >>>> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
> >>>>
> >>>> But this returns the root logger if no existing logger for that name
> >>>> exists - and we can't just enable debug on everything.
> >>>> Is there a way of adding a new LoggerConfig into the configuration?
> >>>>
> >>>> thanks,
> >>>> Phil Wray
> >>>>
> >>>>
> >>>>
> >>>>
> >>>> ---
> >>>>
> >>>> This e-mail may contain confidential and/or privileged information. If
> >>> you are not the intended recipient (or have received this e-mail in
> >> error)
> >>> please notify the sender immediately and delete this e-mail. Any
> >>> unauthorized copying, disclosure or distribution of the material in
> >>> this
> >>
> >>> e-mail is strictly forbidden.
> >>>>
> >>>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
> >>> additional EU corporate and regulatory disclosures and to
> >>> http://www.db.com/unitedkingdom/content/privacy.htm for information
> >> about
> >>> privacy.
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>> ---
> >>>
> >>> This e-mail may contain confidential and/or privileged information. If
> >> you are not the intended recipient (or have received this e-mail in
> >> error)
> >> please notify the sender immediately and delete this e-mail. Any
> >> unauthorized copying, disclosure or distribution of the material in this
> >> e-mail is strictly forbidden.
> >>>
> >>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
> >> additional EU corporate and regulatory disclosures and to
> >> http://www.db.com/unitedkingdom/content/privacy.htm for information
> about
> >> privacy.
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> >> For additional commands, e-mail: log4j-user-help@logging.apache.org
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >> ---
> >>
> >> This e-mail may contain confidential and/or privileged information. If
> >> you are not the intended recipient (or have received this e-mail in
> >> error) please notify the sender immediately and delete this e-mail. Any
> >> unauthorized copying, disclosure or distribution of the material in this
> >> e-mail is strictly forbidden.
> >>
> >> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
> >> additional EU corporate and regulatory disclosures and to
> >> http://www.db.com/unitedkingdom/content/privacy.htm for information
> about
> >> privacy.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > For additional commands, e-mail: log4j-user-help@logging.apache.org
> >
>
>
> ---------------------------------------------------------------------
> 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: how to change logging level for a class at runtime [I]

Posted by Ralph Goers <ra...@dslextreme.com>.
I wouldn’t have asked for a patch submission if I didn’t plan to consider it. That said, we are all volunteers here and sometimes don’t get to things as quickly as we would like.

Ralph

On Aug 6, 2014, at 1:24 PM, Ang Xu <ax...@linkedin.com.INVALID> wrote:

> Hi,
> 
> Is there any plan to merge this patch to the main branch? This
> functionality seems extremely useful when modifying config file is not an
> option.
> 
> Thanks,
> Ang
> 
> On 6/25/14, 12:19 AM, "Phil Wray" <ph...@db.com> wrote:
> 
>> Classification: For internal use only
>> 
>> Hey Ralph.
>> Sure - have created:
>> LOG4J2-681 Add ability to change log level for any class/package
>> dynamically via jmx
>> 
>> cheers,
>> Phil
>> 
>> 
>> 
>> 
>> 
>> From:
>> Ralph Goers <rg...@apache.org>
>> To:
>> Log4J Users List <lo...@logging.apache.org>,
>> Date:
>> 24/06/2014 15:34
>> Subject:
>> Re: how to change logging level for a class at runtime
>> 
>> 
>> 
>> Phil,
>> 
>> That looks pretty good.  It would be great if you could create a Jira
>> issue and add that as a patch
>> 
>> Sent from my iPad
>> 
>>> On Jun 24, 2014, at 7:01 AM, Phil Wray <ph...@db.com> wrote:
>>> 
>>> Classification: Public
>>> 
>>> Hi Ralph,
>>> Just to confirm running latest trunk version its possible to change
>> levels 
>>> at runtime using code as per your docs.
>>> Thanks for the help.
>>> I see a tag for 2.0-rc2 was taken a couple of days ago - I'm guessing
>> this 
>>> is being released imminently?
>>> 
>>> In case it helps others I ended up with the following util to let us
>>> change levels via jmx, just using whatever existing appender the logger
>>> would have had.
>>> 
>>>       @ManagedOperation
>>>       public String changeLogLevel(String loggerName, String level){
>>>               Level newLevel = Level.toLevel(level);
>>>               LoggerContext ctx = (LoggerContext)
>> LogManager.getContext(
>>> false);
>>>               Configuration config = ctx.getConfiguration();
>>>               LoggerConfig loggerConfig =
>>> config.getLoggerConfig(loggerName);
>>>               String msg;
>>> 
>>>               if (loggerName.equals(loggerConfig.getName())){
>>>                       Level oldLevel = loggerConfig.getLevel();
>>>                       loggerConfig.setLevel(newLevel);
>>>                       msg = String.format("Modified existing logger %s
>>> level from %s to %s", loggerName, oldLevel, newLevel);
>>>               } else {
>>>                       createCopyFrom(loggerConfig, loggerName,
>> newLevel, 
>>> config);
>>>                       msg = String.format("Added new logger %s with
>>> level %s previously used logger %s with level %s", loggerName,
>>> newLevel, 
>> 
>>> loggerConfig.getName(), loggerConfig.getLevel());
>>>               }
>>>               ctx.updateLoggers();
>>>               LOG.info(msg);
>>>               return msg;
>>>       }
>>> 
>>>       private void createCopyFrom(LoggerConfig original, String name,
>>> Level level, Configuration config){
>>>               AppenderRef[] refs = original.getAppenderRefs().toArray(
>>> new AppenderRef[]{});
>>>               Filter filter = original.getFilter();
>>>               LoggerConfig copy = LoggerConfig.createLogger(
>>>                               "false",
>>>                               level,
>>>                               name,
>>>                               Boolean.toString
>>> (original.isIncludeLocation()),
>>>                               refs, null, config, filter);
>>> 
>>>               for (Appender appender :
>>> original.getAppenders().values()){
>>>                       copy.addAppender(appender, null, null);
>>>               }
>>>               config.addLogger(name, copy);
>>>       }
>>> 
>>> cheers,
>>> Phil
>>> 
>>> 
>>> 
>>> 
>>> From:
>>> Phil Wray/ext/dbcom
>>> To:
>>> "Log4J Users List" <lo...@logging.apache.org>,
>>> Date:
>>> 24/06/2014 11:16
>>> Subject:
>>> Re: how to change logging level for a class at runtime
>>> 
>>> 
>>> Classification: Public
>>> 
>>> Hi Ralph,
>>> Sorry hadn't looked at the latest source  - see you've made changes to
>>> Configuration to allow adding of LoggerConfig via the interface that
>>> are 
>> 
>>> not yet in the release candidate jar.
>>> I'll take the latest source and build from that.
>>> 
>>> 
>>> thanks,
>>> Phil
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> From:
>>> Phil Wray/ext/dbcom
>>> To:
>>> "Log4J Users List" <lo...@logging.apache.org>,
>>> Date:
>>> 24/06/2014 09:07
>>> Subject:
>>> Re: how to change logging level for a class at runtime
>>> 
>>> 
>>> Classification: Public
>>> 
>>> Hi Ralph,
>>> Thanks for getting back, but the examples do not show how to change
>> levels 
>>> at runtime,
>>> 
>>> There is no method addLogger(String, LoggerConfig) method on the
>>> Configuration interface.
>>> The addLogger method exists on BaseConfiguration, however it throws an
>>> IllegalStateException as its already started, and says you have to
>> create 
>>> a new configuration.
>>> 
>>> Is it actually possible to change the logging level for a class/package
>> at 
>>> runtime after startup?
>>> I think this is a common use case for troubleshooting critical long
>>> running processes.
>>> 
>>> 
>>> thanks,
>>> Phil
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> From:
>>> Ralph Goers <ra...@dslextreme.com>
>>> To:
>>> Log4J Users List <lo...@logging.apache.org>,
>>> Date:
>>> 23/06/2014 15:57
>>> Subject:
>>> Re: how to change logging level for a class at runtime
>>> 
>>> 
>>> 
>>> Please see 
>>> http://people.apache.org/~rgoers/log4j2/manual/customconfig.html
>>> 
>>> Ralph
>>> 
>>>> On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:
>>>> 
>>>> Classification: Public
>>>> 
>>>> Hi,
>>>> Would like to turn on debug for a particular class or package at
>>> runtime, 
>>>> either via jmx or programmatically, where this class or package is
>>>> arbitrary and not known at start up time please.
>>>> I can't see a way to do this?
>>>> Previous posts titled "Programmatic configuration of loggers" show how
>>> to 
>>>> do this for an existing named logger but not for an arbitrary class or
>>>> package which is a typical use case.
>>>> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
>>>> 
>>>> But this returns the root logger if no existing logger for that name
>>>> exists - and we can't just enable debug on everything.
>>>> Is there a way of adding a new LoggerConfig into the configuration?
>>>> 
>>>> thanks,
>>>> Phil Wray
>>>> 
>>>> 
>>>> 
>>>> 
>>>> ---
>>>> 
>>>> This e-mail may contain confidential and/or privileged information. If
>>> you are not the intended recipient (or have received this e-mail in
>> error) 
>>> please notify the sender immediately and delete this e-mail. Any
>>> unauthorized copying, disclosure or distribution of the material in
>>> this 
>> 
>>> e-mail is strictly forbidden.
>>>> 
>>>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>>> additional EU corporate and regulatory disclosures and to
>>> http://www.db.com/unitedkingdom/content/privacy.htm for information
>> about 
>>> privacy.
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> ---
>>> 
>>> This e-mail may contain confidential and/or privileged information. If
>> you are not the intended recipient (or have received this e-mail in
>> error) 
>> please notify the sender immediately and delete this e-mail. Any
>> unauthorized copying, disclosure or distribution of the material in this
>> e-mail is strictly forbidden.
>>> 
>>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>> additional EU corporate and regulatory disclosures and to
>> http://www.db.com/unitedkingdom/content/privacy.htm for information about
>> privacy.
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> For additional commands, e-mail: log4j-user-help@logging.apache.org
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> ---
>> 
>> This e-mail may contain confidential and/or privileged information. If
>> you are not the intended recipient (or have received this e-mail in
>> error) please notify the sender immediately and delete this e-mail. Any
>> unauthorized copying, disclosure or distribution of the material in this
>> e-mail is strictly forbidden.
>> 
>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>> additional EU corporate and regulatory disclosures and to
>> http://www.db.com/unitedkingdom/content/privacy.htm for information about
>> privacy.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 


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


Re: how to change logging level for a class at runtime [I]

Posted by Ang Xu <ax...@linkedin.com.INVALID>.
Hi,

Is there any plan to merge this patch to the main branch? This
functionality seems extremely useful when modifying config file is not an
option.

Thanks,
Ang

On 6/25/14, 12:19 AM, "Phil Wray" <ph...@db.com> wrote:

>Classification: For internal use only
>
>Hey Ralph.
>Sure - have created:
>LOG4J2-681 Add ability to change log level for any class/package
>dynamically via jmx
>
>cheers,
>Phil
>
>
>
>
>
>From:
>Ralph Goers <rg...@apache.org>
>To:
>Log4J Users List <lo...@logging.apache.org>,
>Date:
>24/06/2014 15:34
>Subject:
>Re: how to change logging level for a class at runtime
>
>
>
>Phil,
>
>That looks pretty good.  It would be great if you could create a Jira
>issue and add that as a patch
>
>Sent from my iPad
>
>> On Jun 24, 2014, at 7:01 AM, Phil Wray <ph...@db.com> wrote:
>> 
>> Classification: Public
>> 
>> Hi Ralph,
>> Just to confirm running latest trunk version its possible to change
>levels 
>> at runtime using code as per your docs.
>> Thanks for the help.
>> I see a tag for 2.0-rc2 was taken a couple of days ago - I'm guessing
>this 
>> is being released imminently?
>> 
>> In case it helps others I ended up with the following util to let us
>> change levels via jmx, just using whatever existing appender the logger
>> would have had.
>> 
>>        @ManagedOperation
>>        public String changeLogLevel(String loggerName, String level){
>>                Level newLevel = Level.toLevel(level);
>>                LoggerContext ctx = (LoggerContext)
>LogManager.getContext(
>> false);
>>                Configuration config = ctx.getConfiguration();
>>                LoggerConfig loggerConfig =
>> config.getLoggerConfig(loggerName);
>>                String msg;
>> 
>>                if (loggerName.equals(loggerConfig.getName())){
>>                        Level oldLevel = loggerConfig.getLevel();
>>                        loggerConfig.setLevel(newLevel);
>>                        msg = String.format("Modified existing logger %s
>> level from %s to %s", loggerName, oldLevel, newLevel);
>>                } else {
>>                        createCopyFrom(loggerConfig, loggerName,
>newLevel, 
>> config);
>>                        msg = String.format("Added new logger %s with
>> level %s previously used logger %s with level %s", loggerName,
>>newLevel, 
>
>> loggerConfig.getName(), loggerConfig.getLevel());
>>                }
>>                ctx.updateLoggers();
>>                LOG.info(msg);
>>                return msg;
>>        }
>> 
>>        private void createCopyFrom(LoggerConfig original, String name,
>> Level level, Configuration config){
>>                AppenderRef[] refs = original.getAppenderRefs().toArray(
>> new AppenderRef[]{});
>>                Filter filter = original.getFilter();
>>                LoggerConfig copy = LoggerConfig.createLogger(
>>                                "false",
>>                                level,
>>                                name,
>>                                Boolean.toString
>> (original.isIncludeLocation()),
>>                                refs, null, config, filter);
>> 
>>                for (Appender appender :
>> original.getAppenders().values()){
>>                        copy.addAppender(appender, null, null);
>>                }
>>                config.addLogger(name, copy);
>>        }
>> 
>> cheers,
>> Phil
>> 
>> 
>> 
>> 
>> From:
>> Phil Wray/ext/dbcom
>> To:
>> "Log4J Users List" <lo...@logging.apache.org>,
>> Date:
>> 24/06/2014 11:16
>> Subject:
>> Re: how to change logging level for a class at runtime
>> 
>> 
>> Classification: Public
>> 
>> Hi Ralph,
>> Sorry hadn't looked at the latest source  - see you've made changes to
>> Configuration to allow adding of LoggerConfig via the interface that
>>are 
>
>> not yet in the release candidate jar.
>> I'll take the latest source and build from that.
>> 
>> 
>> thanks,
>> Phil
>> 
>> 
>> 
>> 
>> 
>> 
>> From:
>> Phil Wray/ext/dbcom
>> To:
>> "Log4J Users List" <lo...@logging.apache.org>,
>> Date:
>> 24/06/2014 09:07
>> Subject:
>> Re: how to change logging level for a class at runtime
>> 
>> 
>> Classification: Public
>> 
>> Hi Ralph,
>> Thanks for getting back, but the examples do not show how to change
>levels 
>> at runtime,
>> 
>> There is no method addLogger(String, LoggerConfig) method on the
>> Configuration interface.
>> The addLogger method exists on BaseConfiguration, however it throws an
>> IllegalStateException as its already started, and says you have to
>create 
>> a new configuration.
>> 
>> Is it actually possible to change the logging level for a class/package
>at 
>> runtime after startup?
>> I think this is a common use case for troubleshooting critical long
>> running processes.
>> 
>> 
>> thanks,
>> Phil
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> From:
>> Ralph Goers <ra...@dslextreme.com>
>> To:
>> Log4J Users List <lo...@logging.apache.org>,
>> Date:
>> 23/06/2014 15:57
>> Subject:
>> Re: how to change logging level for a class at runtime
>> 
>> 
>> 
>> Please see 
>> http://people.apache.org/~rgoers/log4j2/manual/customconfig.html
>> 
>> Ralph
>> 
>>> On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:
>>> 
>>> Classification: Public
>>> 
>>> Hi,
>>> Would like to turn on debug for a particular class or package at
>> runtime, 
>>> either via jmx or programmatically, where this class or package is
>>> arbitrary and not known at start up time please.
>>> I can't see a way to do this?
>>> Previous posts titled "Programmatic configuration of loggers" show how
>> to 
>>> do this for an existing named logger but not for an arbitrary class or
>>> package which is a typical use case.
>>> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
>>> 
>>> But this returns the root logger if no existing logger for that name
>>> exists - and we can't just enable debug on everything.
>>> Is there a way of adding a new LoggerConfig into the configuration?
>>> 
>>> thanks,
>>> Phil Wray
>>> 
>>> 
>>> 
>>> 
>>> ---
>>> 
>>> This e-mail may contain confidential and/or privileged information. If
>> you are not the intended recipient (or have received this e-mail in
>error) 
>> please notify the sender immediately and delete this e-mail. Any
>> unauthorized copying, disclosure or distribution of the material in
>>this 
>
>> e-mail is strictly forbidden.
>>> 
>>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>> additional EU corporate and regulatory disclosures and to
>> http://www.db.com/unitedkingdom/content/privacy.htm for information
>about 
>> privacy.
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> ---
>> 
>> This e-mail may contain confidential and/or privileged information. If
>you are not the intended recipient (or have received this e-mail in
>error) 
>please notify the sender immediately and delete this e-mail. Any
>unauthorized copying, disclosure or distribution of the material in this
>e-mail is strictly forbidden.
>> 
>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>additional EU corporate and regulatory disclosures and to
>http://www.db.com/unitedkingdom/content/privacy.htm for information about
>privacy.
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>
>
>
>
>
>
>---
>
>This e-mail may contain confidential and/or privileged information. If
>you are not the intended recipient (or have received this e-mail in
>error) please notify the sender immediately and delete this e-mail. Any
>unauthorized copying, disclosure or distribution of the material in this
>e-mail is strictly forbidden.
>
>Please refer to http://www.db.com/en/content/eu_disclosures.htm for
>additional EU corporate and regulatory disclosures and to
>http://www.db.com/unitedkingdom/content/privacy.htm for information about
>privacy.


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


Re: how to change logging level for a class at runtime [I]

Posted by Phil Wray <ph...@db.com>.
Classification: For internal use only

Hey Ralph.
Sure - have created:
LOG4J2-681 Add ability to change log level for any class/package 
dynamically via jmx

cheers,
Phil





From:
Ralph Goers <rg...@apache.org>
To:
Log4J Users List <lo...@logging.apache.org>, 
Date:
24/06/2014 15:34
Subject:
Re: how to change logging level for a class at runtime



Phil,

That looks pretty good.  It would be great if you could create a Jira 
issue and add that as a patch

Sent from my iPad

> On Jun 24, 2014, at 7:01 AM, Phil Wray <ph...@db.com> wrote:
> 
> Classification: Public
> 
> Hi Ralph,
> Just to confirm running latest trunk version its possible to change 
levels 
> at runtime using code as per your docs.
> Thanks for the help.
> I see a tag for 2.0-rc2 was taken a couple of days ago - I'm guessing 
this 
> is being released imminently?
> 
> In case it helps others I ended up with the following util to let us 
> change levels via jmx, just using whatever existing appender the logger 
> would have had.
> 
>        @ManagedOperation
>        public String changeLogLevel(String loggerName, String level){
>                Level newLevel = Level.toLevel(level);
>                LoggerContext ctx = (LoggerContext) 
LogManager.getContext(
> false);
>                Configuration config = ctx.getConfiguration();
>                LoggerConfig loggerConfig = 
> config.getLoggerConfig(loggerName);
>                String msg;
> 
>                if (loggerName.equals(loggerConfig.getName())){
>                        Level oldLevel = loggerConfig.getLevel();
>                        loggerConfig.setLevel(newLevel);
>                        msg = String.format("Modified existing logger %s 
> level from %s to %s", loggerName, oldLevel, newLevel); 
>                } else {
>                        createCopyFrom(loggerConfig, loggerName, 
newLevel, 
> config);
>                        msg = String.format("Added new logger %s with 
> level %s previously used logger %s with level %s", loggerName, newLevel, 

> loggerConfig.getName(), loggerConfig.getLevel());
>                }
>                ctx.updateLoggers();
>                LOG.info(msg);
>                return msg;
>        }
> 
>        private void createCopyFrom(LoggerConfig original, String name, 
> Level level, Configuration config){
>                AppenderRef[] refs = original.getAppenderRefs().toArray(
> new AppenderRef[]{});
>                Filter filter = original.getFilter();
>                LoggerConfig copy = LoggerConfig.createLogger(
>                                "false", 
>                                level, 
>                                name, 
>                                Boolean.toString
> (original.isIncludeLocation()), 
>                                refs, null, config, filter);
> 
>                for (Appender appender : 
> original.getAppenders().values()){
>                        copy.addAppender(appender, null, null);
>                }
>                config.addLogger(name, copy);
>        }
> 
> cheers,
> Phil
> 
> 
> 
> 
> From:
> Phil Wray/ext/dbcom
> To:
> "Log4J Users List" <lo...@logging.apache.org>, 
> Date:
> 24/06/2014 11:16
> Subject:
> Re: how to change logging level for a class at runtime
> 
> 
> Classification: Public
> 
> Hi Ralph,
> Sorry hadn't looked at the latest source  - see you've made changes to 
> Configuration to allow adding of LoggerConfig via the interface that are 

> not yet in the release candidate jar.
> I'll take the latest source and build from that.
> 
> 
> thanks,
> Phil
> 
> 
> 
> 
> 
> 
> From:
> Phil Wray/ext/dbcom
> To:
> "Log4J Users List" <lo...@logging.apache.org>, 
> Date:
> 24/06/2014 09:07
> Subject:
> Re: how to change logging level for a class at runtime
> 
> 
> Classification: Public
> 
> Hi Ralph,
> Thanks for getting back, but the examples do not show how to change 
levels 
> at runtime,
> 
> There is no method addLogger(String, LoggerConfig) method on the 
> Configuration interface.
> The addLogger method exists on BaseConfiguration, however it throws an 
> IllegalStateException as its already started, and says you have to 
create 
> a new configuration.
> 
> Is it actually possible to change the logging level for a class/package 
at 
> runtime after startup?
> I think this is a common use case for troubleshooting critical long 
> running processes.
> 
> 
> thanks,
> Phil
> 
> 
> 
> 
> 
> 
> 
> From:
> Ralph Goers <ra...@dslextreme.com>
> To:
> Log4J Users List <lo...@logging.apache.org>, 
> Date:
> 23/06/2014 15:57
> Subject:
> Re: how to change logging level for a class at runtime
> 
> 
> 
> Please see 
> http://people.apache.org/~rgoers/log4j2/manual/customconfig.html
> 
> Ralph
> 
>> On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:
>> 
>> Classification: Public
>> 
>> Hi,
>> Would like to turn on debug for a particular class or package at
> runtime, 
>> either via jmx or programmatically, where this class or package is 
>> arbitrary and not known at start up time please.
>> I can't see a way to do this?
>> Previous posts titled "Programmatic configuration of loggers" show how
> to 
>> do this for an existing named logger but not for an arbitrary class or 
>> package which is a typical use case.
>> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
>> 
>> But this returns the root logger if no existing logger for that name 
>> exists - and we can't just enable debug on everything.
>> Is there a way of adding a new LoggerConfig into the configuration?
>> 
>> thanks,
>> Phil Wray
>> 
>> 
>> 
>> 
>> ---
>> 
>> This e-mail may contain confidential and/or privileged information. If
> you are not the intended recipient (or have received this e-mail in 
error) 
> please notify the sender immediately and delete this e-mail. Any 
> unauthorized copying, disclosure or distribution of the material in this 

> e-mail is strictly forbidden.
>> 
>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
> additional EU corporate and regulatory disclosures and to 
> http://www.db.com/unitedkingdom/content/privacy.htm for information 
about 
> privacy.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> ---
> 
> This e-mail may contain confidential and/or privileged information. If 
you are not the intended recipient (or have received this e-mail in error) 
please notify the sender immediately and delete this e-mail. Any 
unauthorized copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
> 
> Please refer to http://www.db.com/en/content/eu_disclosures.htm for 
additional EU corporate and regulatory disclosures and to 
http://www.db.com/unitedkingdom/content/privacy.htm for information about 
privacy.

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







---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.

Re: how to change logging level for a class at runtime

Posted by Ralph Goers <rg...@apache.org>.
Phil,

That looks pretty good.  It would be great if you could create a Jira issue and add that as a patch

Sent from my iPad

> On Jun 24, 2014, at 7:01 AM, Phil Wray <ph...@db.com> wrote:
> 
> Classification: Public
> 
> Hi Ralph,
> Just to confirm running latest trunk version its possible to change levels 
> at runtime using code as per your docs.
> Thanks for the help.
> I see a tag for 2.0-rc2 was taken a couple of days ago - I'm guessing this 
> is being released imminently?
> 
> In case it helps others I ended up with the following util to let us 
> change levels via jmx, just using whatever existing appender the logger 
> would have had.
> 
>        @ManagedOperation
>        public String changeLogLevel(String loggerName, String level){
>                Level newLevel = Level.toLevel(level);
>                LoggerContext ctx = (LoggerContext) LogManager.getContext(
> false);
>                Configuration config = ctx.getConfiguration();
>                LoggerConfig loggerConfig = 
> config.getLoggerConfig(loggerName);
>                String msg;
> 
>                if (loggerName.equals(loggerConfig.getName())){
>                        Level oldLevel = loggerConfig.getLevel();
>                        loggerConfig.setLevel(newLevel);
>                        msg = String.format("Modified existing logger %s 
> level from %s to %s", loggerName, oldLevel, newLevel); 
>                } else {
>                        createCopyFrom(loggerConfig, loggerName, newLevel, 
> config);
>                        msg = String.format("Added new logger %s with 
> level %s previously used logger %s with level %s", loggerName, newLevel, 
> loggerConfig.getName(), loggerConfig.getLevel());
>                }
>                ctx.updateLoggers();
>                LOG.info(msg);
>                return msg;
>        }
> 
>        private void createCopyFrom(LoggerConfig original, String name, 
> Level level, Configuration config){
>                AppenderRef[] refs = original.getAppenderRefs().toArray(
> new AppenderRef[]{});
>                Filter filter = original.getFilter();
>                LoggerConfig copy = LoggerConfig.createLogger(
>                                "false", 
>                                level, 
>                                name, 
>                                Boolean.toString
> (original.isIncludeLocation()), 
>                                refs, null, config, filter);
> 
>                for (Appender appender : 
> original.getAppenders().values()){
>                        copy.addAppender(appender, null, null);
>                }
>                config.addLogger(name, copy);
>        }
> 
> cheers,
> Phil
> 
> 
> 
> 
> From:
> Phil Wray/ext/dbcom
> To:
> "Log4J Users List" <lo...@logging.apache.org>, 
> Date:
> 24/06/2014 11:16
> Subject:
> Re: how to change logging level for a class at runtime
> 
> 
> Classification: Public
> 
> Hi Ralph,
> Sorry hadn't looked at the latest source  - see you've made changes to 
> Configuration to allow adding of LoggerConfig via the interface that are 
> not yet in the release candidate jar.
> I'll take the latest source and build from that.
> 
> 
> thanks,
> Phil
> 
> 
> 
> 
> 
> 
> From:
> Phil Wray/ext/dbcom
> To:
> "Log4J Users List" <lo...@logging.apache.org>, 
> Date:
> 24/06/2014 09:07
> Subject:
> Re: how to change logging level for a class at runtime
> 
> 
> Classification: Public
> 
> Hi Ralph,
> Thanks for getting back, but the examples do not show how to change levels 
> at runtime,
> 
> There is no method addLogger(String, LoggerConfig) method on the 
> Configuration interface.
> The addLogger method exists on BaseConfiguration, however it throws an 
> IllegalStateException as its already started, and says you have to create 
> a new configuration.
> 
> Is it actually possible to change the logging level for a class/package at 
> runtime after startup?
> I think this is a common use case for troubleshooting critical long 
> running processes.
> 
> 
> thanks,
> Phil
> 
> 
> 
> 
> 
> 
> 
> From:
> Ralph Goers <ra...@dslextreme.com>
> To:
> Log4J Users List <lo...@logging.apache.org>, 
> Date:
> 23/06/2014 15:57
> Subject:
> Re: how to change logging level for a class at runtime
> 
> 
> 
> Please see 
> http://people.apache.org/~rgoers/log4j2/manual/customconfig.html
> 
> Ralph
> 
>> On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:
>> 
>> Classification: Public
>> 
>> Hi,
>> Would like to turn on debug for a particular class or package at
> runtime, 
>> either via jmx or programmatically, where this class or package is 
>> arbitrary and not known at start up time please.
>> I can't see a way to do this?
>> Previous posts titled "Programmatic configuration of loggers" show how
> to 
>> do this for an existing named logger but not for an arbitrary class or 
>> package which is a typical use case.
>> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
>> 
>> But this returns the root logger if no existing logger for that name 
>> exists - and we can't just enable debug on everything.
>> Is there a way of adding a new LoggerConfig into the configuration?
>> 
>> thanks,
>> Phil Wray
>> 
>> 
>> 
>> 
>> ---
>> 
>> This e-mail may contain confidential and/or privileged information. If
> you are not the intended recipient (or have received this e-mail in error) 
> please notify the sender immediately and delete this e-mail. Any 
> unauthorized copying, disclosure or distribution of the material in this 
> e-mail is strictly forbidden.
>> 
>> Please refer to http://www.db.com/en/content/eu_disclosures.htm for
> additional EU corporate and regulatory disclosures and to 
> http://www.db.com/unitedkingdom/content/privacy.htm for information about 
> privacy.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> ---
> 
> This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
> 
> Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.

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


Re: how to change logging level for a class at runtime

Posted by Phil Wray <ph...@db.com>.
Classification: Public

Hi Ralph,
Just to confirm running latest trunk version its possible to change levels 
at runtime using code as per your docs.
Thanks for the help.
I see a tag for 2.0-rc2 was taken a couple of days ago - I'm guessing this 
is being released imminently?

In case it helps others I ended up with the following util to let us 
change levels via jmx, just using whatever existing appender the logger 
would have had.

        @ManagedOperation
        public String changeLogLevel(String loggerName, String level){
                Level newLevel = Level.toLevel(level);
                LoggerContext ctx = (LoggerContext) LogManager.getContext(
false);
                Configuration config = ctx.getConfiguration();
                LoggerConfig loggerConfig = 
config.getLoggerConfig(loggerName);
                String msg;
 
                if (loggerName.equals(loggerConfig.getName())){
                        Level oldLevel = loggerConfig.getLevel();
                        loggerConfig.setLevel(newLevel);
                        msg = String.format("Modified existing logger %s 
level from %s to %s", loggerName, oldLevel, newLevel); 
                } else {
                        createCopyFrom(loggerConfig, loggerName, newLevel, 
config);
                        msg = String.format("Added new logger %s with 
level %s previously used logger %s with level %s", loggerName, newLevel, 
loggerConfig.getName(), loggerConfig.getLevel());
                }
                ctx.updateLoggers();
                LOG.info(msg);
                return msg;
        }
 
        private void createCopyFrom(LoggerConfig original, String name, 
Level level, Configuration config){
                AppenderRef[] refs = original.getAppenderRefs().toArray(
new AppenderRef[]{});
                Filter filter = original.getFilter();
                LoggerConfig copy = LoggerConfig.createLogger(
                                "false", 
                                level, 
                                name, 
                                Boolean.toString
(original.isIncludeLocation()), 
                                refs, null, config, filter);
 
                for (Appender appender : 
original.getAppenders().values()){
                        copy.addAppender(appender, null, null);
                }
                config.addLogger(name, copy);
        }

cheers,
Phil




From:
Phil Wray/ext/dbcom
To:
"Log4J Users List" <lo...@logging.apache.org>, 
Date:
24/06/2014 11:16
Subject:
Re: how to change logging level for a class at runtime


Classification: Public

Hi Ralph,
Sorry hadn't looked at the latest source  - see you've made changes to 
Configuration to allow adding of LoggerConfig via the interface that are 
not yet in the release candidate jar.
I'll take the latest source and build from that.


thanks,
Phil






From:
Phil Wray/ext/dbcom
To:
"Log4J Users List" <lo...@logging.apache.org>, 
Date:
24/06/2014 09:07
Subject:
Re: how to change logging level for a class at runtime


Classification: Public

Hi Ralph,
Thanks for getting back, but the examples do not show how to change levels 
at runtime,

There is no method addLogger(String, LoggerConfig) method on the 
Configuration interface.
The addLogger method exists on BaseConfiguration, however it throws an 
IllegalStateException as its already started, and says you have to create 
a new configuration.

Is it actually possible to change the logging level for a class/package at 
runtime after startup?
I think this is a common use case for troubleshooting critical long 
running processes.
 

thanks,
Phil







From:
Ralph Goers <ra...@dslextreme.com>
To:
Log4J Users List <lo...@logging.apache.org>, 
Date:
23/06/2014 15:57
Subject:
Re: how to change logging level for a class at runtime



Please see 
http://people.apache.org/~rgoers/log4j2/manual/customconfig.html

Ralph

On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:

> Classification: Public
> 
> Hi,
> Would like to turn on debug for a particular class or package at 
runtime, 
> either via jmx or programmatically, where this class or package is 
> arbitrary and not known at start up time please.
> I can't see a way to do this?
> Previous posts titled "Programmatic configuration of loggers" show how 
to 
> do this for an existing named logger but not for an arbitrary class or 
> package which is a typical use case.
> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
> 
> But this returns the root logger if no existing logger for that name 
> exists - and we can't just enable debug on everything.
> Is there a way of adding a new LoggerConfig into the configuration?
> 
> thanks,
> Phil Wray
> 
> 
> 
> 
> ---
> 
> This e-mail may contain confidential and/or privileged information. If 
you are not the intended recipient (or have received this e-mail in error) 
please notify the sender immediately and delete this e-mail. Any 
unauthorized copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
> 
> Please refer to http://www.db.com/en/content/eu_disclosures.htm for 
additional EU corporate and regulatory disclosures and to 
http://www.db.com/unitedkingdom/content/privacy.htm for information about 
privacy.









---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.

Re: how to change logging level for a class at runtime

Posted by Ralph Goers <ra...@dslextreme.com>.
Please see http://people.apache.org/~rgoers/log4j2/manual/customconfig.html

Ralph

On Jun 23, 2014, at 3:44 AM, Phil Wray <ph...@db.com> wrote:

> Classification: Public
> 
> Hi,
> Would like to turn on debug for a particular class or package at runtime, 
> either via jmx or programmatically, where this class or package is 
> arbitrary and not known at start up time please.
> I can't see a way to do this?
> Previous posts titled "Programmatic configuration of loggers" show how to 
> do this for an existing named logger but not for an arbitrary class or 
> package which is a typical use case.
> LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
> 
> But this returns the root logger if no existing logger for that name 
> exists - and we can't just enable debug on everything.
> Is there a way of adding a new LoggerConfig into the configuration?
> 
> thanks,
> Phil Wray
> 
> 
> 
> 
> ---
> 
> This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
> 
> Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.