Un MBean per modificare a caldo il livello di log di Log4j
La modifica a caldo (ovvero senza riavvio dell’ Application Server) rappresenta una delle esigenze solitamente più frequenti nell’ambito dello sviluppo di applicazioni enterprise in Java.
Nel tempo ho potuto vedere e provare differenti soluzioni (più o meno eleganti) che però il più delle volte erano piuttosto laboriose ed articolate, e che di fatto consistevamo sempre in dei sistemi di polling sui files di configurazione o delle servlet da invocare manualmente per il ricaricamento del file log4j.properties o log4j.xml modificati.
Oggi invece con l’ausilio di Spring ed in particolare della classe “org.springframework.jmx.export.MBeanExporter”, diventa veramente semplicissimo creare un MBean in grado di esporre tutti i metodi necessari a manipolare a caldo un “Logger”.
Passiamo subito al dettaglio dell’implementazione; quello che ci serve per partire è una semplice classe in grado di esporre i metodi per manipolare i 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();
}
}
Lo step successivo consiste nell’ esportazione della classe “Log4jConfigurator” come MBean attraverso 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" />
A questo punto, una volta deployata la nostra applicazione, avremo a disposizione l’MBean “n2h:log4jConfigurator” che attraverso una qualsiasi console JMX ci metterà a disposizione tutti i metodi della classe “Log4jConfigurator”.
|
|
Ti è piaciuto l'articolo? Sottoscrivi ora il feed RSS! |
Ancora nessun commento »
Feed RSS dei commenti di questo post. TrackBack URI


































