/* * @(#)TraceManager.java 1.16 03/12/19 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.sun.jmx.trace; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; /** * Provides an implementation of the {@link com.sun.jmx.trace.TraceDestination} * interface which uses the J2SE logging API. *

* Note that this implementation can be used only with J2SE version 1.4 or * higher. *

* All {@link Logger}s are contained in the javax.management namespace, which corresponds * to the name of the root package hosting all public JMX interfaces. For each log type * defined in {@link TraceTags}, we use a dedicated {@link Logger} with the same name * under javax.management. *

* The table below shows the list of {@link Logger} objects used in this implementation and * their corresponding category of activity. *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Logger NameJMX log typeInformation logged
javax.management.mbeanserver{@link TraceTags#INFO_MBEANSERVER}Information about the MBean Server
com.sun.jmx.snmp.daemon{@link TraceTags#INFO_ADAPTOR_SNMP}Information about the SNMP Adaptor
com.sun.jmx.snmp{@link TraceTags#INFO_SNMP}Information about SNMP
javax.management.mlet{@link TraceTags#INFO_MLET}Information from an MLet service
javax.management.monitor{@link TraceTags#INFO_MONITOR}Information from a monitor
javax.management.timer{@link TraceTags#INFO_TIMER}Information from a timer
javax.management.notification{@link TraceTags#INFO_NOTIFICATION}Information from the notification mechanism
javax.management.relation{@link TraceTags#INFO_RELATION}Information from the Relation Service
javax.management.modelmbean{@link TraceTags#INFO_MODELMBEAN}Information from the Model MBean components
javax.management.misc{@link TraceTags#INFO_MISC}Information sent from any other class
*

* The mapping for the JMX log levels is the following: *

* * * * * * * * * * * * * * * * * *
JMX log levelJ2SE logging API log level
{@link TraceTags#LEVEL_DEBUG}
{@link Level#FINEST}
{@link TraceTags#LEVEL_TRACE}
{@link Level#FINER}
{@link TraceTags#LEVEL_ERROR}
{@link Level#SEVERE}
* * @since 1.5 * @since.unbundled JMX RI 1.2 */ public class TraceManager implements TraceDestination { /** * Returns the {@link Level} corresponding to the integer value passed as * argument. This value is assumed to be part of the log levels defined by * class {@link TraceTags}. * * @return * */ private static Level getLevel(int level) { switch(level) { case TraceTags.LEVEL_DEBUG: return Level.FINEST; case TraceTags.LEVEL_TRACE: return Level.FINER; case TraceTags.LEVEL_ERROR: return Level.SEVERE; default: return null; } } /** * Returns the {@link Logger} corresponding to the integer value passed as * argument. This value is assumed to be part of the log types defined by * class {@link TraceTags}. * * @return * */ private static Logger getLogger(int type) { switch(type) { case TraceTags.INFO_MBEANSERVER: return Logger.getLogger("javax.management.mbeanserver"); case TraceTags.INFO_ADAPTOR_SNMP: return Logger.getLogger("com.sun.jmx.snmp.daemon"); case TraceTags.INFO_SNMP: return Logger.getLogger("com.sun.jmx.snmp"); case TraceTags.INFO_MLET: return Logger.getLogger("javax.management.mlet"); case TraceTags.INFO_MONITOR: return Logger.getLogger("javax.management.monitor"); case TraceTags.INFO_TIMER: return Logger.getLogger("javax.management.timer"); case TraceTags.INFO_MISC: return Logger.getLogger("javax.management.misc"); case TraceTags.INFO_NOTIFICATION: return Logger.getLogger("javax.management.notification"); case TraceTags.INFO_RELATION: return Logger.getLogger("javax.management.relation"); case TraceTags.INFO_MODELMBEAN: return Logger.getLogger("javax.management.modelmbean"); default: return null; } } /** * @see TraceDestination#isSelected */ public boolean isSelected(int level, int type) { Logger logger; Level lvl; if (((logger = getLogger(type)) != null) && ((lvl = getLevel(level)) != null)) { return logger.isLoggable(lvl); } return false; } /** * Note that the provided log type and log level have to be part of the * enumerated values defined in class {@link TraceTags}. Otherwise, nothing is * logged. * @see TraceDestination#send(int, int, String, String, String) */ public boolean send(int level, int type, String className, String methodName, String info) { if (isSelected(level, type)) { getLogger(type).logp(getLevel(level), className, methodName, info); return true; } return false; } /** * Note that the provided log type and log level have to be part of the * enumerated values defined in class {@link TraceTags}. Otherwise, nothing is * logged. * @see TraceDestination#send(int, int, String, String, Throwable) */ public boolean send(int level, int type, String className, String methodName, Throwable exception) { if (isSelected(level, type)) { getLogger(type).log(getLevel(level), className + ": Exception occured in " + methodName , exception); return true; } return false; } /** * Not implemented, as the control over log levels and output handler is * deferred to the J2SE logging API. * @see TraceDestination#reset **/ public void reset() throws IOException { } /** * Logs a warning message. * This is equivalent to * Logger.getLogger(loggerName).warning(msg); * * @since.unbundled JMX RI 1.2.1 **/ void warning(String loggerName, String msg) { Logger.getLogger(loggerName).warning(msg); } /** * Logs a fine message. * This is equivalent to * Logger.getLogger(loggerName).fine(msg); * * @since.unbundled JMX RI 1.2.1 **/ void fine(String loggerName, String msg) { Logger.getLogger(loggerName).fine(msg); } }