Author: ceki Date: Mon Jul 2 22:14:31 2007 New Revision: 829 Added: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPMakerAdapter.java slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticMDCBinder.java slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/StaticMDCBinder.java slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jMDCAdapter.java Removed: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/MappedDiagnosticContext.java Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/MDC.java slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticLoggerBinder.java slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticMarkerBinder.java
Log: started work on MDC abstraction Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/MDC.java ============================================================================== --- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/MDC.java (original) +++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/MDC.java Mon Jul 2 22:14:31 2007 @@ -25,56 +25,69 @@ package org.slf4j; import org.slf4j.helpers.Util; -import org.slf4j.impl.StaticMarkerBinder; +import org.slf4j.impl.StaticMDCBinder; +import org.slf4j.spi.MDCAdapter; /** - * MDC class serves as an abstraction for the underlying logging system's + * MDC class serves as an abstraction of the underlying logging system's * MDC implementation. At this time, only log4j and logback offer MDC - * functionality. For other systems, this class defaults to nop (empty) - * implemnetation. + * functionality. For other systems, this SLF4J defaults to nop (empty) + * implementation. * - * <p> - * Please note that all methods in this class are static. + * <p>Please note that all methods in this class are static. * * @author Ceki Gülcü */ public class MDC { - static IMarkerFactory markerFactory; + static MDCAdapter mdcAdapter; private MDC() { } static { try { - markerFactory = StaticMarkerBinder.SINGLETON.getMarkerFactory(); + mdcAdapter = StaticMDCBinder.SINGLETON.getMDCA(); } catch (Exception e) { // we should never get here Util.reportFailure("Could not instantiate instance of class [" - + StaticMarkerBinder.SINGLETON.getMarkerFactoryClassStr() + "]", e); + + StaticMDCBinder.SINGLETON.getMDCAdapterClassStr() + "]", e); } } + /** - * Return a Marker instance as specified by the name parameter using the - * previously bound [EMAIL PROTECTED] IMarkerFactory}instance. - * - * @param name - * The name of the [EMAIL PROTECTED] Marker} object to return. - * @return marker + * Put a context value (the <code>val</code> parameter) as identified with + * the <code>key</code> parameter into the current thread's context map. This + * method delegates all work to the MDC of the underlying logging system. */ - public static Marker getMarker(String name) { - return markerFactory.getMarker(name); + public static void put(String key, String val) { + mdcAdapter.put(key, val); } /** - * Return the [EMAIL PROTECTED] IMarkerFactory}instance in use. - * - * <p>The IMarkerFactory instance is usually bound with this class at - * compile time. + * Get the context identified by the <code>key</code> parameter. + * This method delegates all work to the MDC of the underlying logging system. * - * @return the IMarkerFactory instance in use + * @return the string value identified by the <code>key</code> parameter. + */ + public static String get(String key) { + return mdcAdapter.get(key); + } + + /** + * Remove the the context identified by the <code>key</code> parameter using + * the underlying system's MDC implementation. + */ + public static void remove(String key) { + mdcAdapter.remove(key); + } + + /** + * Clear all entries in the MDC of the underlying implementation. */ - public static IMarkerFactory getIMarkerFactory() { - return markerFactory; + public void clear() { + mdcAdapter.clear(); } + + } \ No newline at end of file Added: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPMakerAdapter.java ============================================================================== --- (empty file) +++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPMakerAdapter.java Mon Jul 2 22:14:31 2007 @@ -0,0 +1,29 @@ +package org.slf4j.helpers; + +import org.slf4j.spi.MDCAdapter; + +/** + * This adapter is an empty implementation of the [EMAIL PROTECTED] MDCAdapter} interface. + * It is used for all logging systems which do not support mapped + * diagnostic contexts such as JDK14, simple and NOP. + * + * @author Ceki Gülcü + * + * @since 1.4.1 + */ +public class NOPMakerAdapter implements MDCAdapter { + + public void clear() { + } + + public String get(String key) { + return null; + } + + public void put(String key, String val) { + } + + public void remove(String key) { + } + +} Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticLoggerBinder.java ============================================================================== --- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticLoggerBinder.java (original) +++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticLoggerBinder.java Mon Jul 2 22:14:31 2007 @@ -30,7 +30,9 @@ * The binding of [EMAIL PROTECTED] LoggerFactory} class with an actual instance of * [EMAIL PROTECTED] ILoggerFactory} is performed using information returned by this class. * - * This class is meant to provide a dummy StaticLoggerBinder to the slf4j-api module. + * This class is meant to provide a dummy StaticLoggerBinder to the slf4j-api module. + * Real implementations are found in each SLF4J binding project, e.g. slf4j-nop, + * slf4j-log4j12 etc. * * @author Ceki Gülcü */ Added: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticMDCBinder.java ============================================================================== --- (empty file) +++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticMDCBinder.java Mon Jul 2 22:14:31 2007 @@ -0,0 +1,35 @@ +package org.slf4j.impl; + +import org.slf4j.spi.MDCAdapter; + + +/** + * This class is only a stub. Real implementations are found in + * each SLF4J binding project, e.g. slf4j-nop, slf4j-log4j12 etc. + * + * @author Ceki Gülcü + */ +public class StaticMDCBinder { + + + /** + * The unique instance of this class. + */ + public static final StaticMDCBinder SINGLETON = new StaticMDCBinder(); + + private StaticMDCBinder() { + throw new UnsupportedOperationException("This code should never make it into the jar"); + } + + /** + * Currently this method always returns an instance of + * [EMAIL PROTECTED] StaticMDCBinder}. + */ + public MDCAdapter getMDCA() { + throw new UnsupportedOperationException("This code should never make it into the jar"); + } + + public String getMDCAdapterClassStr() { + throw new UnsupportedOperationException("This code should never make it into the jar"); + } +} Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticMarkerBinder.java ============================================================================== --- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticMarkerBinder.java (original) +++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/impl/StaticMarkerBinder.java Mon Jul 2 22:14:31 2007 @@ -34,7 +34,9 @@ * The binding of [EMAIL PROTECTED] MarkerFactory} class with an actual instance of * [EMAIL PROTECTED] IMarkerFactory} is performed using information returned by this class. * - * This class is meant to provide a dummy StaticMarkerBinder to the slf4j-api module. + * This class is meant to provide a *dummy* StaticMarkerBinder to the slf4j-api module. + * Real implementations are found in each SLF4J binding project, e.g. slf4j-nop, + * slf4j-log4j12 etc. * * @author Ceki Gülcü */ Added: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java ============================================================================== --- (empty file) +++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java Mon Jul 2 22:14:31 2007 @@ -0,0 +1,38 @@ +package org.slf4j.spi; + +/** + * This interface abstracts the service offered by various MDC + * implementations. + * + * @author Ceki Gülcü + * @since 1.4.1 + */ +public interface MDCAdapter { + + /** + * Put a context value (the <code>val</code> parameter) as identified with + * the <code>key</code> parameter into the current thread's context map. + * + * <p>If the current thread does not have a context map it is created as a side + * effect of this call. + */ + public void put(String key, String val); + + /** + * Get the context identified by the <code>key</code> parameter. + * + * @return the string value identified by the <code>key</code> parameter. + */ + public String get(String key); + + /** + * Remove the the context identified by the <code>key</code> parameter. + */ + public void remove(String key); + + /** + * Clear all entries in the MDC. + */ + public void clear(); + +} Added: slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/StaticMDCBinder.java ============================================================================== --- (empty file) +++ slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/StaticMDCBinder.java Mon Jul 2 22:14:31 2007 @@ -0,0 +1,35 @@ +package org.slf4j.impl; + +import org.slf4j.helpers.NOPMakerAdapter; +import org.slf4j.spi.MDCAdapter; + + +/** + * This implementation is bound to [EMAIL PROTECTED] NOPMakerAdapter}. + * + * @author Ceki Gülcü + */ +public class StaticMDCBinder { + + + /** + * The unique instance of this class. + */ + public static final StaticMDCBinder SINGLETON = new StaticMDCBinder(); + + private StaticMDCBinder() { + throw new UnsupportedOperationException("This code should never make it into the jar"); + } + + /** + * Currently this method always returns an instance of + * [EMAIL PROTECTED] StaticMDCBinder}. + */ + public MDCAdapter getMDCA() { + return new NOPMakerAdapter(); + } + + public String getMDCAdapterClassStr() { + return NOPMakerAdapter.class.getName(); + } +} Added: slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jMDCAdapter.java ============================================================================== --- (empty file) +++ slf4j/trunk/slf4j-log4j12/src/main/java/org/slf4j/impl/Log4jMDCAdapter.java Mon Jul 2 22:14:31 2007 @@ -0,0 +1,28 @@ +package org.slf4j.impl; + +import java.util.Map; + +import org.slf4j.spi.MDCAdapter; + +public class Log4jMDCAdapter implements MDCAdapter { + + public void clear() { + Map map = org.apache.log4j.MDC.getContext(); + if(map != null) { + map.clear(); + } + } + + public String get(String key) { + return (String) org.apache.log4j.MDC.get(key); + } + + public void put(String key, String val) { + org.apache.log4j.MDC.put(key, val); + } + + public void remove(String key) { + org.apache.log4j.MDC.remove(key); + } + +} _______________________________________________ dev mailing list dev@slf4j.org http://www.slf4j.org/mailman/listinfo/dev