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 <rgo...@apache.org> To: Log4J Users List <log4j-user@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 <phil.w...@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" <log4j-user@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" <log4j-user@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 <ralph.go...@dslextreme.com> > To: > Log4J Users List <log4j-user@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 <phil.w...@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-unsubscr...@logging.apache.org For additional commands, e-mail: log4j-user-h...@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.