1 22 23 24 package com.mchange.v2.log; 25 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import com.mchange.v1.util.StringTokenizerUtils; 29 import com.mchange.v2.cfg.MultiPropertiesConfig; 30 31 public abstract class MLog 32 { 33 final static NameTransformer transformer; 34 final static MLog mlog; 35 36 final static MultiPropertiesConfig CONFIG; 37 38 final static MLogger logger; 39 40 static 41 { 42 String [] defaults = new String [] 43 { 44 "/com/mchange/v2/log/default-mchange-log.properties", 45 "/mchange-log.properties", 46 "/" 47 }; 48 CONFIG = MultiPropertiesConfig.readVmConfig( defaults, null ); 49 50 String classnamesStr = CONFIG.getProperty("com.mchange.v2.log.MLog"); 51 String [] classnames = null; 52 if (classnamesStr == null) 53 classnamesStr = CONFIG.getProperty("com.mchange.v2.log.mlog"); 54 if (classnamesStr != null) 55 classnames = StringTokenizerUtils.tokenizeToArray( classnamesStr, ", \t\r\n" ); 56 57 boolean warn = false; 58 MLog tmpml = null; 59 if (classnames != null) 60 tmpml = findByClassnames( classnames ); 61 if (tmpml == null) 62 tmpml = findByClassnames( MLogClasses.CLASSNAMES ); 63 if (tmpml == null) 64 { 65 warn = true; 66 tmpml = new FallbackMLog(); 67 } 68 mlog = tmpml; 69 if (warn) 70 info("Using " + mlog.getClass().getName() + " -- Named logger's not supported, everything goes to System.err."); 71 72 logger = mlog.getLogger( MLog.class ); 73 String loggerDesc = mlog.getClass().getName(); 74 if ("com.mchange.v2.log.jdk14logging.Jdk14MLog".equals( loggerDesc )) 75 loggerDesc = "java 1.4+ standard"; 76 else if ("com.mchange.v2.log.log4j.Log4jMLog".equals( loggerDesc )) 77 loggerDesc = "log4j"; 78 79 if (logger.isLoggable( MLevel.INFO )) 80 logger.log( MLevel.INFO, "MLog clients using " + loggerDesc + " logging."); 81 82 NameTransformer tmpt = null; 83 String tClassName = CONFIG.getProperty("com.mchange.v2.log.NameTransformer"); 84 if (tClassName == null) 85 tClassName = CONFIG.getProperty("com.mchange.v2.log.nametransformer"); 86 try 87 { 88 if (tClassName != null) 89 tmpt = (NameTransformer) Class.forName( tClassName ).newInstance(); 90 } 91 catch ( Exception e ) 92 { 93 System.err.println("Failed to instantiate com.mchange.v2.log.NameTransformer '" + tClassName + "'!"); 94 e.printStackTrace(); 95 } 96 transformer = tmpt; 97 98 } 100 101 public static MLog findByClassnames( String [] classnames ) 102 { 103 List attempts = null; 104 for (int i = 0, len = classnames.length; i < len; ++i) 105 { 106 try { return (MLog) Class.forName( classnames[i] ).newInstance(); } 107 catch (Exception e) 108 { 109 if (attempts == null) 110 attempts = new ArrayList (); 111 attempts.add( classnames[i] ); 112 } 115 } 116 System.err.println("Tried without success to load the following MLog classes:"); 117 for (int i = 0, len = attempts.size(); i < len; ++i) 118 System.err.println("\t" + attempts.get(i)); 119 return null; 120 } 121 122 public static MLog instance() 123 { return mlog; } 124 125 public static MLogger getLogger(String name) 126 { 127 MLogger out; 128 if ( transformer == null ) 129 out = instance().getMLogger( name ); 130 else 131 { 132 String xname = transformer.transformName( name ); 133 if (xname != null) 134 out = instance().getMLogger( xname ); 135 else 136 out = instance().getMLogger( name ); 137 } 138 return out; 139 } 140 141 public static MLogger getLogger(Class cl) 142 { 143 MLogger out; 144 if ( transformer == null ) 145 out = instance().getMLogger( cl ); 146 else 147 { 148 String xname = transformer.transformName( cl ); 149 if (xname != null) 150 out = instance().getMLogger( xname ); 151 else 152 out = instance().getMLogger( cl ); 153 } 154 return out; 155 } 156 157 public static MLogger getLogger() 158 { 159 MLogger out; 160 if ( transformer == null ) 161 out = instance().getMLogger(); 162 else 163 { 164 String xname = transformer.transformName(); 165 if (xname != null) 166 out = instance().getMLogger( xname ); 167 else 168 out = instance().getMLogger(); 169 } 170 return out; 171 } 172 173 public static void log(MLevel l, String msg) 174 { instance().getLogger().log( l, msg ); } 175 176 public static void log(MLevel l, String msg, Object param) 177 { instance().getLogger().log( l, msg, param ); } 178 179 public static void log(MLevel l,String msg, Object [] params) 180 { instance().getLogger().log( l, msg, params ); } 181 182 public static void log(MLevel l, String msg,Throwable t) 183 { instance().getLogger().log( l, msg, t ); } 184 185 public static void logp(MLevel l, String srcClass, String srcMeth, String msg) 186 { instance().getLogger().logp( l, srcClass, srcMeth, msg ); } 187 188 public static void logp(MLevel l, String srcClass, String srcMeth, String msg, Object param) 189 { instance().getLogger().logp( l, srcClass, srcMeth, msg, param ); } 190 191 public static void logp(MLevel l, String srcClass, String srcMeth, String msg, Object [] params) 192 { instance().getLogger().logp( l, srcClass, srcMeth, msg, params ); } 193 194 public static void logp(MLevel l, String srcClass, String srcMeth, String msg, Throwable t) 195 { instance().getLogger().logp( l, srcClass, srcMeth, msg, t ); } 196 197 public static void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg) 198 { instance().getLogger().logp( l, srcClass, srcMeth, rb, msg ); } 199 200 public static void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Object param) 201 { instance().getLogger().logrb( l, srcClass, srcMeth, rb, msg, param ); } 202 203 public static void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Object [] params) 204 { instance().getLogger().logrb( l, srcClass, srcMeth, rb, msg, params ); } 205 206 public static void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Throwable t) 207 { instance().getLogger().logrb( l, srcClass, srcMeth, rb, msg, t ); } 208 209 public static void entering(String srcClass, String srcMeth) 210 { instance().getLogger().entering( srcClass, srcMeth ); } 211 212 public static void entering(String srcClass, String srcMeth, Object param) 213 { instance().getLogger().entering( srcClass, srcMeth, param ); } 214 215 public static void entering(String srcClass, String srcMeth, Object params[]) 216 { instance().getLogger().entering( srcClass, srcMeth, params ); } 217 218 public static void exiting(String srcClass, String srcMeth) 219 { instance().getLogger().exiting( srcClass, srcMeth ); } 220 221 public static void exiting(String srcClass, String srcMeth, Object result) 222 { instance().getLogger().exiting( srcClass, srcMeth, result ); } 223 224 public static void throwing(String srcClass, String srcMeth, Throwable t) 225 { instance().getLogger().throwing( srcClass, srcMeth, t); } 226 227 public static void severe(String msg) 228 { instance().getLogger().severe( msg ); } 229 230 public static void warning(String msg) 231 { instance().getLogger().warning( msg ); } 232 233 public static void info(String msg) 234 { instance().getLogger().info( msg ); } 235 236 public static void config(String msg) 237 { instance().getLogger().config( msg ); } 238 239 public static void fine(String msg) 240 { instance().getLogger().fine( msg ); } 241 242 public static void finer(String msg) 243 { instance().getLogger().finer( msg ); } 244 245 public static void finest(String msg) 246 { instance().getLogger().finest( msg ); } 247 248 public abstract MLogger getMLogger(String name); 249 public abstract MLogger getMLogger(Class cl); 250 public abstract MLogger getMLogger(); 251 } | Popular Tags |