Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Pyx4j framework Copyright (C) 2006-2007 pyx4.com. Created on 18-Jan-07

Author(s):
vlads
Version:
$Id: Log4jJMX.java 100 2007-04-21 01:14:17Z vlads $
  
 package com.pyx4j.log;
 
 import java.util.Set;
 
 
Register log4j Loggers and Appenders in JMX
 
 public class Log4jJMX {
 
     private static final Logger log = Logger.getLogger();
 
     private MBeanServer mbServer;
 
     private Set<Appenderappenders;
 
     private Log4jJMX(String subDomain) {
         this. = new HashSet<Appender>();
         this. = ManagementFactory.getPlatformMBeanServer();
     }
 
     public static void register(String subDomain) {
 
         Log4jJMX reg = new Log4jJMX(subDomain);
 
         reg.registerLoggerMBean(subDomain, LogManager.getRootLogger());
 
         Enumeration loggers = LogManager.getCurrentLoggers();
         while (loggers.hasMoreElements()) {
             reg.registerLoggerMBean(subDomain, (org.apache.log4j.Loggerloggers.nextElement());
         }
 
         for (Appender appender : reg.appenders) {
             reg.registerAppenderMBean(subDomainappender);
         }
     }
     
     public static void unregister(String subDomain) {
         Set<Appenderappenders = new HashSet<Appender>();
         MBeanServer mbServer = ManagementFactory.getPlatformMBeanServer();
         try {
             org.apache.log4j.Logger logger = LogManager.getRootLogger();
             Enumeration enumeration = logger.getAllAppenders();
             while (enumeration.hasMoreElements()) {
                 Appender appender = (Appenderenumeration.nextElement();
                 if (!appenders.contains(appender)) {
                     appenders.add(appender);
                 }
             }
             
             mbServer.unregisterMBean(getObjectName(subDomainlogger.getClass(), logger.getName()));
         } catch (Throwable e) {
             .error("unregister"e);
         }
         
         Enumeration loggers = LogManager.getCurrentLoggers();
         while (loggers.hasMoreElements()) {
             org.apache.log4j.Logger logger = (org.apache.log4j.Loggerloggers.nextElement();
             if (logger.getLevel() == null) {
                 continue;
             }
             Enumeration enumeration = logger.getAllAppenders();
             while (enumeration.hasMoreElements()) {
                 Appender appender = (Appenderenumeration.nextElement();
                 if (!appenders.contains(appender)) {
                     appenders.add(appender);
                 }
             }
             try {
                 mbServer.unregisterMBean(getObjectName(subDomainlogger.getClass(), logger.getName()));
             } catch (Throwable e) {
                 .error("unregister"e);
             }
         }
         for (Appender appender : appenders) {
             try {
                 mbServer.unregisterMBean(getObjectName(subDomainappender.getClass(), appender.getName()));
             } catch (Throwable e) {
                .error("unregister"e);
            }
        }
    }
    private static ObjectName getObjectName(String subDomainClass<?> kclassString namethrows MalformedObjectNameException {
        
        Hashtable<StringStringkeys = new Hashtable<StringString>();
        String domain = Logger.class.getPackage().getName();
        String type = "logger";
        if (Appender.class.isAssignableFrom(kclass)) {
            type = "appender";
        }
        
        keys.put("context"subDomain);
        keys.put("type"type);
        keys.put("id"name);
        
        return new ObjectName(domainkeys);
    }
    
    private void registerLoggerMBean(String subDomainorg.apache.log4j.Logger logger) {
        // Only declared in log4j.xml loggers
        if (logger.getLevel() == null) {
            return;
        }
        try {
            LoggerDynamicMBean loggerMBean = new LoggerDynamicMBean(logger);
            .registerMBean(loggerMBeangetObjectName(subDomainLogger.classlogger.getName()));
            Enumeration enumeration = logger.getAllAppenders();
            while (enumeration.hasMoreElements()) {
                Appender appender = (Appenderenumeration.nextElement();
                if (!.contains(appender)) {
                    .add(appender);
                }
            }
        } catch (Throwable e) {
            .error("Log4j JMX error"e);
        }
    }
    private void registerAppenderMBean(String subDomainAppender appender) {
        try {
            .registerMBean(new AppenderDynamicMBean(appender), getObjectName(subDomainappender.getClass(), appender.getName()));
        } catch (Exception e) {
            .error("Log4j JMX error"e);
        }
    }
New to GrepCode? Check out our FAQ X