MBean na zmiany w poziomie na gorąco dzienniki Log4j
Reklama
Zmiana na gorąco (tzn. bez ponownego uruchamiania 'Application Server) jest jednym z wymogów zazwyczaj częstsze w rozwoju aplikacji w języku Java.
Z biegiem czasu będę mógł zobaczyć i spróbować różnych rozwiązań (mniej lub bardziej eleganckie), ale większość czasu były raczej pracochłonnym i artykułuje, a fakt, że jest zawsze obwodów na pliki konfiguracyjne serwletu lub przez powoływanie się ręcznie za przeładować plik log4j.properties lub log4j.xml zmianie.
Dziś z pomocą Spring, aw szczególności klasy org.springframework.jmx.export.MBeanExporter ", staje się bardzo łatwe do stworzenia MBean mogą wystawiać wszystkie metody potrzebne do manipulowania gorący" logger ".
Teraz detalicznych wykonania, co musimy zacząć to prosta klasa, która może narażać metod manipulowania logowania:
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(); } } Następnym krokiem w "eksportowej klasy" Log4jConfigurator "jako MBean Wiosny poprzez:
<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" /> W tym momencie, kiedy wdrożyć nasz wniosek, mamy MBean "n2h: log4jConfigurator", które oznacza każdą JMX konsoli będą udostępniać wszystkie metody klasy "Log4jConfigurator".
Nadal bez komentarza "
Komentarze RSS dla tego posta. Trackback URI
Zostaw komentarz
Recent visitors
- Sieci społeczne
- Wyślij e-mail







































