1 24 25 package org.slf4j; 26 27 import org.slf4j.helpers.Util; 28 import org.slf4j.impl.StaticMDCBinder; 29 import org.slf4j.spi.MDCAdapter; 30 31 59 public class MDC { 60 61 static final String NULL_MDCA_URL = "http://www.slf4j.org/codes.html#null_MDCA"; 62 static final String NO_STATIC_MDC_BINDER_URL = "http://www.slf4j.org/codes.html#no_static_mdc_binder"; 63 static MDCAdapter mdcAdapter; 64 65 private MDC() { 66 } 67 68 static { 69 try { 70 mdcAdapter = StaticMDCBinder.SINGLETON.getMDCA(); 71 } catch(NoClassDefFoundError ncde) { 72 String msg = ncde.getMessage(); 73 if(msg != null && msg.indexOf("org/slf4j/impl/StaticMDCBinder") != -1) { 74 Util.reportFailure("Failed to load class \"org.slf4j.impl.StaticMDCBinder\"."); 75 Util.reportFailure("See "+NO_STATIC_MDC_BINDER_URL+" for further details."); 76 77 } 78 throw ncde; 79 } catch (Exception e) { 80 Util.reportFailure("Could not bind with an instance of class [" 82 + StaticMDCBinder.SINGLETON.getMDCAdapterClassStr() + "]", e); 83 } 84 } 85 86 87 92 public static void put(String key, String val) { 93 if(mdcAdapter == null) { 94 throw new IllegalStateException ("MDCAdapter cannot be null. See also "+NULL_MDCA_URL); 95 } 96 mdcAdapter.put(key, val); 97 } 98 99 105 public static String get(String key) { 106 if(mdcAdapter == null) { 107 throw new IllegalStateException ("MDCAdapter cannot be null. See also "+NULL_MDCA_URL); 108 } 109 return mdcAdapter.get(key); 110 } 111 112 116 public static void remove(String key) { 117 if(mdcAdapter == null) { 118 throw new IllegalStateException ("MDCAdapter cannot be null. See also "+NULL_MDCA_URL); 119 } 120 mdcAdapter.remove(key); 121 } 122 123 126 public static void clear() { 127 if(mdcAdapter == null) { 128 throw new IllegalStateException ("MDCAdapter cannot be null. See also "+NULL_MDCA_URL); 129 } 130 mdcAdapter.clear(); 131 } 132 133 139 public static MDCAdapter getMDCAdapter() { 140 return mdcAdapter; 141 } 142 } | Popular Tags |