[logback-dev] [Bug 35] provide JMX bean similar to java.util.logging.LoggingMXBean / java.util.logging.Logging

bugzilla-daemon at pixie.qos.ch bugzilla-daemon at pixie.qos.ch
Thu Jan 4 08:36:43 CET 2007


http://bugzilla.qos.ch/show_bug.cgi?id=35





------- Comment #2 from sdavids at gmx.de  2007-01-04 08:36 -------
package ch.qos.logback.classic;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;

/**
 * Logging is the implementation class of LoggingMBean.
 * <p>
 * The <code>LoggingMBean</code> interface provides a standard method for
management access to the individual
 * <code>ch.qos.logback.classic.Logger</code> objects available at runtime.
 * </p>
 * <h3>Example Usage</h3>
 * <pre> ObjectName on = new
ObjectName(&quot;ch.qos.logback.classic:type=Logging&quot;);
 * Logging mbean = new Logging();
 * if (mbs.isRegistered(on)) {
 *     mbs.unregisterMBean(on);
 * }
 * mbs.registerMBean(mbean, on);</pre>
 * 
 * @author Sebastian Davids, <a
href="mailto:sebastian at davids.name">sebastian at davids.name</a>
 * @see javax.management
 * @see Logger
 */
public class Logging implements LoggingMBean {

    private static final String EMPTY_STRING = ""; //$NON-NLS-1$

    private static final String[] EMPTY_STRING_ARRAY = new String[0];

    /** {@inheritDoc} */
    public String getLoggerLevel(String loggerName) {
        ILoggerFactory factory = LoggerFactory.getILoggerFactory();
        if (factory instanceof LoggerContext) {
            LoggerContext context = (LoggerContext) factory;
            Logger logger = context.exists(loggerName);
            if (logger == null) {
                return null;
            }
            Level level = logger.getLevel();
            return level == null ? EMPTY_STRING : level.toString();
        }
        // ILoggerFactory#getLogger(loggerName) would create a new logger
        // so always return null (logger does not exist) even if it actually
does
        return null;
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    public String[] getLoggerNames() {
        ILoggerFactory factory = LoggerFactory.getILoggerFactory();
        if (factory instanceof LoggerContext) {
            LoggerContext context = (LoggerContext) factory;
            // loggerCache is not accessible so use reflection
            try {
                Field loggerCacheField =
LoggerContext.class.getDeclaredField("loggerCache"); //$NON-NLS-1$
                loggerCacheField.setAccessible(true);
                Map<String, Logger> loggerCache = (Map<String, Logger>)
loggerCacheField.get(context);
                if (loggerCache == null) {
                    return EMPTY_STRING_ARRAY;
                }
                Set<String> loggerNames = loggerCache.keySet();
                return loggerNames.toArray(new String[loggerNames.size()]);
            } catch (ClassCastException e) {
                return EMPTY_STRING_ARRAY;
            } catch (SecurityException e) {
                return EMPTY_STRING_ARRAY;
            } catch (NoSuchFieldException e) {
                return EMPTY_STRING_ARRAY;
            } catch (IllegalArgumentException e) {
                return EMPTY_STRING_ARRAY;
            } catch (IllegalAccessException e) {
                return EMPTY_STRING_ARRAY;
            }
        }
        return EMPTY_STRING_ARRAY;
    }

    /** {@inheritDoc} */
    public void setLoggerLevel(String loggerName, String levelName) {
        if (loggerName == null) {
            throw new NullPointerException("loggerName is null"); //$NON-NLS-1$
        }
        ILoggerFactory factory = LoggerFactory.getILoggerFactory();
        if (factory instanceof LoggerContext) {
            LoggerContext context = (LoggerContext) factory;
            Logger logger = context.exists(loggerName);
            if (logger == null) {
                throw new IllegalArgumentException("Logger " + loggerName +
"does not exist"); //$NON-NLS-1$//$NON-NLS-2$
            }
            Level level = Level.toLevel(levelName);
            logger.setLevel(level);
        }
        // ILoggerFactory#getLogger(loggerName) would create a new logger
        // so fail silently
    }
}


-- 
Configure bugmail: http://bugzilla.qos.ch/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.



More information about the logback-dev mailing list