[Bug 35] New: provide JMX bean similar to java.util.logging.LoggingMXBean / java.util.logging.Logging

http://bugzilla.qos.ch/show_bug.cgi?id=35 Summary: provide JMX bean similar to java.util.logging.LoggingMXBean / java.util.logging.Logging Product: logback-classic Version: unspecified Platform: All OS/Version: All Status: NEW Severity: normal Priority: P3 Component: Other AssignedTo: logback-dev@qos.ch ReportedBy: sdavids@gmx.de -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=35 ------- Comment #1 from sdavids@gmx.de 2007-01-04 08:36 ------- /******************************************************************************* * Copyright (c) 2007 Sebastian Davids. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation, and is available at * http://www.gnu.org/licenses/lgpl.html * * Contributors: * Sebastian Davids - initial API and implementation *******************************************************************************/ package ch.qos.logback.classic; /** * The management interface for the logging facility. * <p> * The {@link javax.management.ObjectName ObjectName} for uniquely identifying the <code>LoggingMBean</code> within an * MBeanServer is: <code>ch.qos.logback.classic:type=Logging</code> * </p> * * @see java.lang.management.ManagementFactory * @author Sebastian Davids, <a href="mailto:sebastian@davids.name">sebastian@davids.name</a> */ public interface LoggingMBean { /** * Returns the list of currently registered loggers. * * @return a list of <code>String</code> each of which is a currently registered <code>Logger</code> name */ public String[] getLoggerNames(); /** * Gets the name of the log level associated with the specified logger. * <p> * If the specified logger does not exist, <code>null</code> is returned. * </p> * * @param loggerName * the name of the <code>Logger</code> to be retrieved * @return the name of the log level of the specified logger or <code>null</code> if the specified logger does not * exist * @see Logger#getLevel */ String getLoggerLevel(String loggerName); /** * Sets the specified logger to the specified new level. * <p> * The level of the specified logger is set to the parsed <code>Level</code> matching the <code>levelName</code>. * </p> * <p> * If <code>null</code> the logger's level is set to a default. * </p> * * @param loggerName * the name of the <code>Logger</code> to be set; may not be <code>null</code> * @param levelName * the name of the level to set the specified logger to or <code>null</code> if to set the level to a * default * @throws IllegalArgumentException * if the specified logger does not exist * @see Logger#setLevel * @see Level#toLevel(String) */ void setLoggerLevel(String loggerName, String levelName); } -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=35 ------- Comment #2 from sdavids@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("ch.qos.logback.classic:type=Logging"); * Logging mbean = new Logging(); * if (mbs.isRegistered(on)) { * mbs.unregisterMBean(on); * } * mbs.registerMBean(mbean, on);</pre> * * @author Sebastian Davids, <a href="mailto:sebastian@davids.name">sebastian@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.

http://bugzilla.qos.ch/show_bug.cgi?id=35 ------- Comment #3 from sdavids@gmx.de 2007-01-04 08:39 ------- Permission granted to Logback Committers to incorporate the classes above into Logback codebase. You may move them into a more appropriate package and rename them. Please retain an attribution notice, thanks. @@ You might consider replacing the reflection stuff in getLoggerNames if you add an appropriate way to retrieve the logger names from the LoggerContext. -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=35 noreply.sebastien@qos.ch changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |FIXED ------- Comment #4 from noreply.sebastien@qos.ch 2007-01-10 21:35 ------- Hello again Sebastian, When you submitted your code to bugzilla, we had already some parts of JMX components done on December 20th, as you can see from the svn repository[1]. However, we liked your idea to provide a list of all declared loggers and implemented it. We did some internal refactoring, so that the final implementation of the method could be easier, and not use Reflection. On this contribution, we did not use any parts of your code, but most certainly we used your idea to provide the logger list. We are going to add your name to the JMXConfigurator classes as a contributor, and link this bugzilla entry. Thank you very much for you contribution to logback, and do not hesitate to send your ideas if you feel that anything could be better! [1]http://tinyurl.com/y5z4ow -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=35 noreply.sebastien@qos.ch changed: What |Removed |Added ---------------------------------------------------------------------------- Status|RESOLVED |CLOSED -- 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.
participants (1)
-
bugzilla-daemon@pixie.qos.ch