1 22 23 24 package com.mchange.v2.log.log4j; 25 26 import java.text.*; 27 import java.util.*; 28 29 import com.mchange.v2.log.*; 30 import com.mchange.v2.util.DoubleWeakHashMap; 31 32 import org.apache.log4j.*; 33 34 public final class Log4jMLog extends MLog 35 { 36 final static String CHECK_CLASS = "org.apache.log4j.Logger"; 37 38 MLogger global = null; 39 40 public Log4jMLog() throws ClassNotFoundException 41 { Class.forName( CHECK_CLASS ); } 42 43 public MLogger getMLogger(String name) 44 { 45 Logger lg = Logger.getLogger(name); 46 return new Log4jMLogger( lg ); 47 } 48 49 public MLogger getMLogger(Class cl) 50 { 51 Logger lg = Logger.getLogger(cl); 52 return new Log4jMLogger( lg ); 53 } 54 55 56 public MLogger getMLogger() 57 { 58 Logger lg = Logger.getRootLogger(); 59 return new Log4jMLogger( lg ); 60 } 61 62 private final static class Log4jMLogger implements MLogger 63 { 64 final static String FQCN = Log4jMLogger.class.getName(); 65 66 MLevel myLevel = null; 67 Logger logger; 68 69 Log4jMLogger( Logger logger ) 70 { this.logger = logger; } 71 72 private static MLevel guessMLevel(Level lvl) 73 { 74 if (lvl == null) 75 return null; 76 else if (lvl == Level.ALL) 77 return MLevel.ALL; 78 else if (lvl == Level.DEBUG) 79 return MLevel.FINEST; 80 else if (lvl == Level.ERROR) 81 return MLevel.SEVERE; 82 else if (lvl == Level.FATAL) 83 return MLevel.SEVERE; 84 else if (lvl == Level.INFO) 85 return MLevel.INFO; 86 else if (lvl == Level.OFF) 87 return MLevel.OFF; 88 else if (lvl == Level.WARN) 89 return MLevel.WARNING; 90 else 91 throw new IllegalArgumentException ("Unknown level: " + lvl); 92 } 93 94 private static Level level(MLevel lvl) 95 { 96 if (lvl == null) 97 return null; 98 else if (lvl == MLevel.ALL) 99 return Level.ALL; 100 else if (lvl == MLevel.CONFIG) 101 return Level.DEBUG; 102 else if (lvl == MLevel.FINE) 103 return Level.DEBUG; 104 else if (lvl == MLevel.FINER) 105 return Level.DEBUG; 106 else if (lvl == MLevel.FINEST) 107 return Level.DEBUG; 108 else if (lvl == MLevel.INFO) 109 return Level.INFO; 110 else if (lvl == MLevel.INFO) 111 return Level.OFF; 112 else if (lvl == MLevel.SEVERE) 113 return Level.ERROR; 114 else if (lvl == MLevel.WARNING) 115 return Level.WARN; 116 else 117 throw new IllegalArgumentException ("Unknown MLevel: " + lvl); 118 } 119 120 private static String createMessage(String srcClass, String srcMeth, String msg) 121 { 122 StringBuffer sb = new StringBuffer (511); 123 sb.append("[class: "); 124 sb.append( srcClass ); 125 sb.append("; method: "); 126 sb.append( srcMeth ); 127 if (! srcMeth.endsWith(")")) 128 sb.append("()"); 129 sb.append("] "); 130 sb.append( msg ); 131 return sb.toString(); 132 } 133 134 private static String createMessage(String srcMeth, String msg) 135 { 136 StringBuffer sb = new StringBuffer (511); 137 sb.append("[method: "); 138 sb.append( srcMeth ); 139 if (! srcMeth.endsWith(")")) 140 sb.append("()"); 141 sb.append("] "); 142 sb.append( msg ); 143 return sb.toString(); 144 } 145 146 public ResourceBundle getResourceBundle() 147 { return null; } 148 149 public String getResourceBundleName() 150 { return null; } 151 152 public void setFilter(Object java14Filter) throws SecurityException 153 { warning("setFilter() not supported by MLogger " + this.getClass().getName()); } 154 155 public Object getFilter() 156 { return null; } 157 158 private void log(Level lvl, Object msg, Throwable t) 159 { logger.log( FQCN, lvl, msg, t ); } 160 161 public void log(MLevel l, String msg) 162 { log( level(l), msg, null); } 163 164 public void log(MLevel l, String msg, Object param) 165 { log( level(l), (msg!=null ? MessageFormat.format(msg, new Object [] { param }) : null), null); } 166 167 public void log(MLevel l,String msg, Object [] params) 168 { log( level(l), (msg!=null ? MessageFormat.format(msg, params) : null), null); } 169 170 public void log(MLevel l, String msg, Throwable t) 171 { log( level(l), msg, t); } 172 173 public void logp(MLevel l, String srcClass, String srcMeth, String msg) 174 { log( level(l), createMessage( srcClass, srcMeth, msg), null); } 175 176 public void logp(MLevel l, String srcClass, String srcMeth, String msg, Object param) 177 { log( level(l), createMessage( srcClass, srcMeth, (msg!=null ? MessageFormat.format(msg, new Object [] {param}) : null) ), null); } 178 179 public void logp(MLevel l, String srcClass, String srcMeth, String msg, Object [] params) 180 { log( level(l), createMessage( srcClass, srcMeth, (msg!=null ? MessageFormat.format(msg, params) : null) ), null); } 181 182 public void logp(MLevel l, String srcClass, String srcMeth, String msg, Throwable t) 183 { log( level(l), createMessage( srcClass, srcMeth, msg ), t); } 184 185 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg) 186 { log( level(l), createMessage( srcClass, srcMeth, formatMessage(rb, msg, null) ), null); } 187 188 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Object param) 189 { log( level(l), createMessage( srcClass, srcMeth, formatMessage(rb, msg, new Object [] { param } ) ), null); } 190 191 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Object [] params) 192 { log( level(l), createMessage( srcClass, srcMeth, formatMessage(rb, msg, params) ), null); } 193 194 public void logrb(MLevel l, String srcClass, String srcMeth, String rb, String msg, Throwable t) 195 { log( level(l), createMessage( srcClass, srcMeth, formatMessage(rb, msg, null) ), t); } 196 197 public void entering(String srcClass, String srcMeth) 198 { log( Level.DEBUG, createMessage( srcClass, srcMeth, "entering method." ), null); } 199 200 public void entering(String srcClass, String srcMeth, Object param) 201 { log( Level.DEBUG, createMessage( srcClass, srcMeth, "entering method... param: " + param.toString() ), null); } 202 203 public void entering(String srcClass, String srcMeth, Object params[]) 204 { log( Level.DEBUG, createMessage( srcClass, srcMeth, "entering method... " + LogUtils.createParamsList( params ) ), null); } 205 206 public void exiting(String srcClass, String srcMeth) 207 { log( Level.DEBUG, createMessage( srcClass, srcMeth, "exiting method." ), null); } 208 209 public void exiting(String srcClass, String srcMeth, Object result) 210 { log( Level.DEBUG, createMessage( srcClass, srcMeth, "exiting method... result: " + result.toString() ), null); } 211 212 public void throwing(String srcClass, String srcMeth, Throwable t) 213 { log( Level.DEBUG, createMessage( srcClass, srcMeth, "throwing exception... " ), t); } 214 215 public void severe(String msg) 216 { log( Level.ERROR, msg, null); } 217 218 public void warning(String msg) 219 { log( Level.WARN, msg, null); } 220 221 public void info(String msg) 222 { log( Level.INFO, msg, null); } 223 224 public void config(String msg) 225 { log( Level.DEBUG, msg, null); } 226 227 public void fine(String msg) 228 { log( Level.DEBUG, msg, null); } 229 230 public void finer(String msg) 231 { log( Level.DEBUG, msg, null); } 232 233 public void finest(String msg) 234 { log( Level.DEBUG, msg, null); } 235 236 public synchronized void setLevel(MLevel l) throws SecurityException 237 { 238 logger.setLevel( level( l ) ); 239 myLevel = l; 240 } 241 242 public synchronized MLevel getLevel() 243 { 244 if (myLevel == null) 246 myLevel = guessMLevel( logger.getLevel() ); 247 return myLevel; 248 } 249 250 public boolean isLoggable(MLevel l) 251 { 252 return logger.isEnabledFor( level(l) ); 256 } 257 258 public String getName() 259 { return logger.getName(); } 260 261 public void addHandler(Object h) throws SecurityException 262 { 263 if (! (h instanceof Appender)) 264 throw new IllegalArgumentException ("The 'handler' " + h + " is not compatible with MLogger " + this); 265 logger.addAppender( (Appender) h ); 266 } 267 268 public void removeHandler(Object h) throws SecurityException 269 { 270 if (! (h instanceof Appender)) 271 throw new IllegalArgumentException ("The 'handler' " + h + " is not compatible with MLogger " + this); 272 logger.removeAppender( (Appender) h ); 273 } 274 275 public Object [] getHandlers() 276 { 277 List tmp = new LinkedList(); 278 for (Enumeration e = logger.getAllAppenders(); e.hasMoreElements(); ) 279 tmp.add( e.nextElement() ); 280 return tmp.toArray(); 281 } 282 283 public void setUseParentHandlers(boolean uph) 284 { logger.setAdditivity( uph ); } 285 286 public boolean getUseParentHandlers() 287 { return logger.getAdditivity(); } 288 } 289 290 private static String formatMessage( String rbname, String msg, Object [] params ) 291 { 292 if ( msg == null ) 293 { 294 if (params == null) 295 return ""; 296 else 297 return LogUtils.createParamsList( params ); 298 } 299 else 300 { 301 ResourceBundle rb = ResourceBundle.getBundle( rbname ); 302 if (rb != null) 303 { 304 String check = rb.getString( msg ); 305 if (check != null) 306 msg = check; 307 } 308 return (params == null ? msg : MessageFormat.format( msg, params )); 309 } 310 } 311 } | Popular Tags |