1 50 51 package org.openlaszlo.iv.flash.util; 52 53 import org.openlaszlo.iv.flash.api.*; 54 import java.io.*; 55 import java.net.*; 56 import java.util.*; 57 58 import org.apache.log4j.*; 59 import org.apache.log4j.spi.*; 60 import org.apache.log4j.helpers.*; 61 62 90 public class Log { 91 92 private static final String FQCN = Log.class.getName(); 93 94 private static Logger logger; 95 private static final String filePattern = "%d [%p] - %m%n"; 96 private static final String consolePattern = "%r [%p] - %m%n"; 97 98 static { 99 100 String log4j_conf = PropertyManager.getProperty("org.openlaszlo.iv.flash.log4j.configuration"); 101 102 if( log4j_conf != null ) { 103 URL url = null; 104 try { 105 url = new URL(log4j_conf); 106 } catch( MalformedURLException e ) { 107 File file = Util.getSysFile(log4j_conf); 108 try { 109 url = file.toURL(); 110 } catch( MalformedURLException ee ) { 111 } 112 } 113 114 if( url != null ) { 116 OptionConverter.selectAndConfigure(url, null, LogManager.getLoggerRepository()); 117 } 118 } 119 120 logger = Logger.getLogger("org.openlaszlo.iv.flash.Generator"); 121 logger.setResourceBundle(Resource.getInstance()); 122 } 123 124 129 public static Logger getLogger() { 130 return logger; 131 } 132 133 140 public static void setLogger( Logger logger ) { 141 Log.logger = logger; 142 } 143 144 147 public static void setLogToConsole() { 148 logger.removeAllAppenders(); 149 logger.addAppender(new ConsoleAppender(new PatternLayout(consolePattern))); 150 } 151 152 159 166 171 181 189 public static void logRB( IVException e ) { 190 log(e); 191 } 192 193 public static void log( IVException e ) { 194 String key = e.getMessageKey(); 195 Level level = getMessageLevel(key); 196 if( !logger.isEnabledFor(level) ) return; 197 198 logger.log(FQCN, level, e.getLocalizedMessage(), e); 199 } 200 201 206 public static void logRB( Throwable t ) { 207 log(t); 208 } 209 210 public static void log( Throwable t ) { 211 if( t instanceof IVException ) { 212 log( (IVException) t ); 213 } else { 214 error(t.getLocalizedMessage(), t); 215 } 216 } 217 218 225 public static void logRB( String key, Object [] p, Throwable t ) { 226 _logif(key, p, t); 227 } 228 229 235 public static void logRB( String key, Object [] p ) { 236 _logif(key, p, null); 237 } 238 239 245 public static void logRB( String key ) { 246 _logif(key, null, null); 247 } 248 249 255 public static void logRB( String key, Throwable t ) { 256 _logif(key, null, t); 257 } 258 259 public static void infoRB( String key, Object [] parms ) { 260 _logif(Level.INFO, key, parms, null); 261 } 262 263 public static void infoRB( String key, Object [] parms, Throwable t ) { 264 _logif(Level.INFO, key, parms, t); 265 } 266 267 public static void debugRB( String key, Object [] parms ) { 268 _logif(Level.DEBUG, key, parms, null); 269 } 270 271 public static void debugRB( String key, Object [] parms, Throwable t ) { 272 _logif(Level.DEBUG, key, parms, t); 273 } 274 275 public static void errorRB( String key, Object [] parms ) { 276 _logif(Level.ERROR, key, parms, null); 277 } 278 279 public static void errorRB( String key, Object [] parms, Throwable t ) { 280 _logif(Level.ERROR, key, parms, t); 281 } 282 283 public static void fatalRB( String key, Object [] parms ) { 284 _logif(Level.FATAL, key, parms, null); 285 } 286 287 public static void fatalRB( String key, Object [] parms, Throwable t ) { 288 _logif(Level.FATAL, key, parms, t); 289 } 290 291 public static void warnRB( String key, Object [] parms ) { 292 _logif(Level.WARN, key, parms, null); 293 } 294 295 public static void warnRB( String key, Object [] parms, Throwable t ) { 296 _logif(Level.WARN, key, parms, t); 297 } 298 299 303 public static void info( Object msg ) { 304 logger.info(msg); 305 } 306 307 public static void info( Object msg, Throwable t ) { 308 logger.info(msg, t); 309 } 310 311 public static void debug( Object msg ) { 312 logger.debug(msg); 313 } 314 315 public static void debug( Object msg, Throwable t ) { 316 logger.debug(msg, t); 317 } 318 319 public static void error( Object msg ) { 320 logger.error(msg); 321 } 322 323 public static void error( Object msg, Throwable t ) { 324 logger.error(msg, t); 325 } 326 327 public static void fatal( Object msg ) { 328 logger.fatal(msg); 329 } 330 331 public static void fatal( Object msg, Throwable t ) { 332 logger.fatal(msg, t); 333 } 334 335 public static void warn( Object msg ) { 336 logger.warn(msg); 337 } 338 339 public static void warn( Object msg, Throwable t ) { 340 logger.warn(msg, t); 341 } 342 343 349 public static Level getMessageLevel( String key ) { 350 if( key == null || key.length() == 0 ) return Level.FATAL; 351 char ch = key.charAt(0); 352 switch(ch) { 353 case '0': return Level.FATAL; 354 case '1': return Level.ERROR; 355 case '2': return Level.WARN; 356 case '3': return Level.INFO; 357 case '4': return Level.DEBUG; 358 default: return Level.FATAL; 359 } 360 } 361 362 public static void setFatalLevel() { 363 logger.setLevel(Level.FATAL); 364 } 365 366 public static void setErrorLevel() { 367 logger.setLevel(Level.ERROR); 368 } 369 370 public static void setWarnLevel() { 371 logger.setLevel(Level.WARN); 372 } 373 374 public static void setInfoLevel() { 375 logger.setLevel(Level.INFO); 376 } 377 378 public static void setDebugLevel() { 379 logger.setLevel(Level.DEBUG); 380 } 381 382 389 protected static void _logif( String key, Object [] parms, Throwable t ) { 390 _logif(getMessageLevel(key), key, parms, t); 391 } 392 393 401 protected static void _logif( Level level, String key, Object [] parms, Throwable t ) { 402 if( !logger.isEnabledFor(level) ) return; 403 _log(level, key, parms, t); 404 } 405 406 419 protected static void _log( Level level, String key, Object [] parms, Throwable t ) { 420 String message = getMessage(key, parms); 421 logger.callAppenders(new LoggingEvent(FQCN, logger, level, message, t)); 423 } 424 425 435 public static String getMessage( String key, Object [] parms ) { 436 return getMessage(logger.getResourceBundle(), key, parms); 437 } 438 439 450 public static String getMessage( ResourceBundle rb, String key, Object [] parms ) { 451 if( rb == null ) { 452 rb = Resource.getInstance(); 453 } 454 455 String msg; 456 try { 457 msg = rb.getString(key); 458 } catch( MissingResourceException e ) { 459 msg = "No resource is associated with key \""+key+"\"."; 460 error(msg); 461 } catch( Throwable t ) { 462 msg = "Error retrieving resource by key \""+key+"\"."; 463 error(msg, t); 464 } 465 466 if( parms != null ) { 467 msg = java.text.MessageFormat.format(msg, parms); 468 } 469 470 return msg; 471 } 472 473 } 474 | Popular Tags |