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

2014-08-11 Thread Phil Wray
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.w...@db.com




From:
Matt Sicker 
To:
Log4J Users List , 
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  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  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  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"  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 
>> >> To:
>> >> Log4J Users List ,
>> >> 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  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 t

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

2014-08-08 Thread Matt Sicker
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  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  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  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"  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 
>> >> To:
>> >> Log4J Users List ,
>> >> 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  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" ,
>> >>> 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
>

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

2014-08-06 Thread Matt Sicker
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  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  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"  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 
> >> To:
> >> Log4J Users List ,
> >> 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  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" ,
> >>> 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" ,
> >>> 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
> >> leve

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

2014-08-06 Thread Ralph Goers
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  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"  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 
>> To:
>> Log4J Users List ,
>> 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  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" ,
>>> 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" ,
>>> 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 critica

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

2014-08-06 Thread Ang Xu
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"  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 
>To:
>Log4J Users List ,
>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  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" ,
>> 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" ,
>> 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 
>> To:
>> Log4J Users List ,
>> 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  wrote:
>>> 
>>> Classification: Pub

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

2014-06-25 Thread Phil Wray
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 
To:
Log4J Users List , 
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  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" , 
> 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" , 
> 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 
> To:
> Log4J Users List , 
> 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  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