Subscribe to the newsletter or RSS feed to receive updates automatically blog!

MBean for a change in the level of hot logs Log4j



The change hot (ie without restarting the 'Application Server) is one of the requirements usually more frequent in the development of enterprise applications in Java.
Over time I could see and try different solutions (more or less elegant) but most times were rather laborious and articulated, and that fact is always of polling on configuration files or by invoking servlet manually for reload file log4j.properties or log4j.xml changed.
Today with the help of Spring and in particular the class org.springframework.jmx.export.MBeanExporter ", it becomes really easy to create an MBean can expose all the methods needed to manipulate a hot" Logger ".

We now retail implementation; what we need to get started is a simple class that can expose the methods to manipulate the loggers:

 package net.nothing2hide.jmx; import java.util.Enumeration; import java.util.List; import java.util.Vector; import org.apache.log4j.Level; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; public class Log4jConfigurator { /** * Set the log level to INFO for the target logger * @param target The name of the logger to change */ public void enableInfo(String target) { LogManager.getLogger(target).setLevel(Level.INFO); } /** * Set the log level to WARN for the target logger * @param target The name of the logger to change */ public void enableWarn(String target) { LogManager.getLogger(target).setLevel(Level.WARN); } /** * Set the log level to ERROR for the target logger * @param target The name of the logger to change */ public void enableError(String target) { LogManager.getLogger(target).setLevel(Level.ERROR); } /** * Set the log level to DEBUG for the target logger * @param target The name of the logger to change */ public void enableDebug(String target) { LogManager.getLogger(target).setLevel(Level.DEBUG); } /** * Set the log level to INFO for the ROOT logger */ public void enableInfoGlobally() { LogManager.getRootLogger().setLevel(Level.INFO); } /** * Set the log level to WARN for the ROOT logger */ public void enableWarnGlobally() { LogManager.getRootLogger().setLevel(Level.WARN); } /** * Set the log level to ERROR for the ROOT logger */ public void enableErrorGlobally() { LogManager.getRootLogger().setLevel(Level.ERROR); } /** * Set the log level to DEBUG for the ROOT logger */ public void enableDebugGlobally() { LogManager.getRootLogger().setLevel(Level.DEBUG); } /** * @return The list of all the available loggers */ public String listAvailableLoggers(){ Enumeration en = LogManager.getLoggerRepository() .getCurrentLoggers(); StringBuffer res = new StringBuffer(); while(en.hasMoreElements()){ Logger logger = (Logger)en.nextElement(); res.append(logger.getName()).append("n"); } return res.toString(); } } 

The next step in 'export of Class "Log4jConfigurator" as MBean through Spring:

 <bean id="myMBeansExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false"> <property name="beans"> <map> <entry key="n2h:name=log4jConfigurator" value-ref="log4jConfigurator"/> </map> </property> <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING" /> </bean> <bean id="log4jConfigurator" class="net.nothing2hide.jmx.Log4jConfigurator" /> 

At this point, once deploy our application, we have the MBean "n2h: log4jConfigurator" which means any JMX console will make available all methods of class "Log4jConfigurator."



Did you like this article? Sign up now to receive updates or news articles:
Subscribe to RSS feeds write to the RSS feed


Still no comment '

Comments RSS feed for this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Nothing2Hide © 2006 All rights reserved.

License | Disclaimer

Close
Send e-mail