1 23 24 29 30 package com.sun.jdo.spi.persistence.utility.logging; 31 32 import java.io.*; 33 import java.util.*; 34 import java.text.MessageFormat ; 35 36 import com.sun.jdo.spi.persistence.utility.I18NHelper; 37 import com.sun.jdo.spi.persistence.utility.StringHelper; 38 39 48 abstract public class AbstractLogger implements Logger 49 { 50 51 private static final ResourceBundle _messages = 52 I18NHelper.loadBundle(AbstractLogger.class); 53 54 55 private static final int _defaultLevel = INFO; 56 57 58 private static final char _levelSeparator='.'; 59 60 private static Map _levelNamesToValues; 61 62 private static boolean _hasLoggingProperties = true; 63 private static Properties _loggingProperties; 64 65 66 private final ResourceBundle _bundle; 67 68 69 private final String _loggerName; 70 71 72 private final int _level; 73 74 static 75 { 76 _levelNamesToValues = new HashMap(); 77 _levelNamesToValues.put("ALL", new Integer (ALL)); _levelNamesToValues.put("FINEST", new Integer (FINEST)); _levelNamesToValues.put("FINER", new Integer (FINER)); _levelNamesToValues.put("FINE", new Integer (FINE)); _levelNamesToValues.put("CONFIG", new Integer (CONFIG)); _levelNamesToValues.put("INFO", new Integer (INFO)); _levelNamesToValues.put("WARNING", new Integer (WARNING)); _levelNamesToValues.put("SEVERE", new Integer (SEVERE)); _levelNamesToValues.put("OFF", new Integer (OFF)); } 87 88 95 public AbstractLogger (String loggerName, String bundleName, 96 ClassLoader loader) 97 { 98 _loggerName = loggerName; 99 _level = readLoggingLevel(loggerName); 100 _bundle = I18NHelper.loadBundle(bundleName, loader); 101 } 102 103 105 protected static ResourceBundle getMessages () { return _messages; } 106 107 private static Map getLevelNameMap () { return _levelNamesToValues; } 108 109 private static synchronized Properties getLoggingProperties () 111 { 112 if (_hasLoggingProperties && (_loggingProperties == null)) 113 { 114 String fileName = 115 System.getProperty("java.util.logging.config.file"); 117 if (fileName == null) 118 { 119 fileName = System.getProperty("java.home"); 121 if (fileName != null) 122 { 123 File file = new File(fileName, "lib"); 125 file = new File(file, "logging.properties"); 127 try 128 { 129 fileName = file.getCanonicalPath(); 130 } 131 catch (IOException ioe) 132 { 133 fileName = null; 134 } 135 } 136 } 137 138 if (fileName != null) 139 { 140 InputStream inputStream = null; 141 File file = null; 142 143 try 144 { 145 Properties properties = new Properties(); 146 BufferedInputStream bufferedInputStream = null; 147 148 inputStream = new FileInputStream(fileName); 149 bufferedInputStream = new BufferedInputStream(inputStream); 150 properties.load(bufferedInputStream); 151 _loggingProperties = properties; 152 } 153 catch (Exception e) 154 { 155 _hasLoggingProperties = false; 156 } 157 finally 158 { 159 if (inputStream != null) 160 { 161 try 162 { 163 inputStream.close(); 164 } 165 catch (IOException ioe) 166 { 167 } 169 } 170 } 171 } 172 else 173 _hasLoggingProperties = false; 174 } 175 176 return _loggingProperties; 177 } 178 179 182 public static String toString (int level) 183 { 184 String bundleKey = null; 185 186 switch (level) 187 { 188 case Logger.OFF: 189 bundleKey = "utility.level_off"; break; 191 case Logger.SEVERE: 192 bundleKey = "utility.level_severe"; break; 194 case Logger.WARNING: 195 bundleKey = "utility.level_warning"; break; 197 case Logger.INFO: 198 bundleKey = "utility.level_info"; break; 200 case Logger.CONFIG: 201 bundleKey = "utility.level_config"; break; 203 case Logger.FINE: 204 bundleKey = "utility.level_fine"; break; 206 case Logger.FINER: 207 bundleKey = "utility.level_finer"; break; 209 case Logger.FINEST: 210 bundleKey = "utility.level_finest"; break; 212 case Logger.ALL: 213 bundleKey = "utility.level_all"; break; 215 } 216 217 return ((bundleKey != null) ? 218 I18NHelper.getMessage(getMessages(), bundleKey) : null); 219 } 220 221 223 protected ResourceBundle getBundle () { return _bundle; } 224 225 public int getLevel () { return _level; } 226 227 private int readLoggingLevel (String loggerName) 228 { 229 String value = findLevelMatch(loggerName); 230 int level = _defaultLevel; 231 232 if (value != null) 233 { 234 Object lookupValue = null; 235 236 value = value.trim(); 237 lookupValue = getLevelNameMap().get(value); 238 239 if (lookupValue != null) 240 level = ((Integer )lookupValue).intValue(); 241 else { 243 try 244 { 245 level = Integer.parseInt(value); 246 } 247 catch (NumberFormatException nfe) 248 { 249 } 251 } 252 } 253 254 return level; 255 } 256 257 261 private String findLevelMatch (String loggerName) 262 { 263 Properties properties = getLoggingProperties(); 264 String value = null; 265 266 if (properties != null) 267 { 268 while (value == null) 269 { 270 int lastIndex = loggerName.lastIndexOf(_levelSeparator); 271 272 value = properties.getProperty(loggerName + ".level"); 274 if (loggerName.trim().length() > 0) 275 { 276 loggerName = ((lastIndex == -1) ? "" : loggerName.substring(0, lastIndex)); 278 } 279 else 280 return value; 281 } 282 } 283 284 return value; 285 } 286 287 294 public boolean isLoggable () { return (FINE >= getLevel()); } 295 296 304 public boolean isLoggable (int levelValue) 305 { 306 return (levelValue >= getLevel()); 307 } 308 309 319 public void entering (String sourceClass, String sourceMethod) 320 { 321 entering(sourceClass, sourceMethod, (Object [])null); 322 } 323 324 336 public void entering (String sourceClass, String sourceMethod, 337 Object param1) 338 { 339 if (isLoggable(FINER)) 340 entering(sourceClass, sourceMethod, new Object []{param1}); 341 } 342 343 356 public void entering (String sourceClass, String sourceMethod, 357 Object params[]) 358 { 359 if (isLoggable(FINER)) 360 { 361 MessageFormat messageFormat = null; 362 String messageKey = null; 363 String [] args = null; 364 365 if ((params != null) && (params.length > 0)) 366 { 367 messageKey = "entering_method_params"; args = new String []{sourceClass, sourceMethod, 369 StringHelper.arrayToSeparatedList(Arrays.asList(params))}; 370 } 371 else 372 { 373 messageKey = "entering_method"; args = new String []{sourceClass, sourceMethod}; 375 } 376 377 messageFormat = new MessageFormat ( 378 getMessages().getString(messageKey)); 379 finer(messageFormat.format(args)); 380 } 381 } 382 383 393 public void exiting (String sourceClass, String sourceMethod) 394 { 395 exiting(sourceClass, sourceMethod, null); 396 } 397 398 410 public void exiting (String sourceClass, String sourceMethod, Object result) 411 { 412 if (isLoggable(FINER)) 413 { 414 MessageFormat messageFormat = null; 415 String messageKey = null; 416 Object [] args = null; 417 418 if (result != null) 419 { 420 messageKey = "exiting_method_return"; args = new Object []{sourceClass, sourceMethod, result}; 422 } 423 else 424 { 425 messageKey = "exiting_method"; args = new Object []{sourceClass, sourceMethod}; 427 } 428 429 messageFormat = new MessageFormat ( 430 getMessages().getString(messageKey)); 431 finer(messageFormat.format(args)); 432 } 433 } 434 435 456 public void throwing (String sourceClass, String sourceMethod, 457 Throwable thrown) 458 { 459 if (isLoggable(FINER)) 460 { 461 MessageFormat messageFormat = new MessageFormat ( 462 getMessages().getString("throwing_method")); 463 464 log(FINER, messageFormat.format( 465 new String []{sourceClass, sourceMethod}), thrown); 466 } 467 } 468 469 478 public void severe (String msg) { log(SEVERE, msg); } 479 480 489 public void warning (String msg) { log(WARNING, msg); } 490 491 500 public void info (String msg) { log(INFO, msg); } 501 502 511 public void config (String msg) { log(CONFIG, msg); } 512 513 523 public void log (int level, String msg) 524 { 525 if (isLoggable(level)) 526 logInternal(level, getMessage(msg)); 527 } 528 529 540 public void log (int level, String msg, Object o1) 541 { 542 if (isLoggable(level)) 543 log(level, msg, new Object []{o1}); 544 } 545 546 557 public void log (int level, String msg, Object [] o) 558 { 559 if (isLoggable(level)) 560 { 561 int count = ((o == null) ? 0 : o.length); 562 String formattedMessage = msg; 563 564 if (count > 0) 565 { 566 MessageFormat messageFormat = 567 new MessageFormat (getBundle().getString(msg)); 568 569 if (messageFormat != null) 570 formattedMessage = messageFormat.format(o); 571 } 572 else 573 formattedMessage = getMessage(msg); 574 575 logInternal(level, formattedMessage); 576 } 577 } 578 579 591 public void log (int level, String msg, Object o1, Object o2) 592 { 593 if (isLoggable(level)) 594 log(level, msg, new Object []{o1, o2}); 595 } 596 597 610 public void log (int level, String msg, Object o1, Object o2, Object o3) 611 { 612 if (isLoggable(level)) 613 log(level, msg, new Object []{o1, o2, o3}); 614 } 615 616 628 abstract public void log (int level, String msg, Throwable thrown); 629 630 639 public void fine (String msg) { log(FINE, msg); } 640 641 651 public void fine (String msg, Object o1) { log(FINE, msg, o1); } 652 653 663 public void fine (String msg, Object [] o) { log(FINE, msg, o); } 664 665 676 public void fine (String msg, Object o1, Object o2) 677 { 678 log(FINE, msg, o1, o2); 679 } 680 681 693 public void fine (String msg, Object o1, Object o2, Object o3) 694 { 695 log(FINE, msg, o1, o2, o3); 696 } 697 698 707 public void finer (String msg) { log(FINER, msg); } 708 709 719 public void finer (String msg, Object [] o) { log(FINER, msg, o); } 720 721 731 public void finer (String msg, Object o1) { log(FINER, msg, o1); } 732 733 744 public void finer (String msg, Object o1, Object o2) 745 { 746 log(FINER, msg, o1, o2); 747 } 748 749 761 public void finer (String msg, Object o1, Object o2, Object o3) 762 { 763 log(FINER, msg, o1, o2, o3); 764 } 765 766 775 public void finest (String msg) { log(FINEST, msg); } 776 777 787 public void finest (String msg, Object [] o) { log(FINEST, msg, o); } 788 789 799 public void finest (String msg, Object o1) { log(FINEST, msg, o1); } 800 801 812 public void finest (String msg, Object o1, Object o2) 813 { 814 log(FINEST, msg, o1, o2); 815 } 816 817 829 public void finest (String msg, Object o1, Object o2, Object o3) 830 { 831 log(FINEST, msg, o1, o2, o3); 832 } 833 834 838 public String getName () { return _loggerName; } 839 840 843 public String toString () 844 { 845 StringBuffer buffer = new StringBuffer (getClass().getName()); 846 847 buffer.append(": "); buffer.append(" name: "); buffer.append(getName()); 850 buffer.append(", logging level: "); buffer.append(toString(getLevel())); 852 853 return buffer.toString(); 854 } 855 856 864 protected String getMessage (String message) 865 { 866 try 867 { 868 return getBundle().getString(message); 869 } 870 catch (MissingResourceException e) 871 { 872 return message; 873 } 874 875 } 876 877 886 protected String getMessageWithPrefix (int level, String message) 887 { 888 MessageFormat messageFormat = new MessageFormat ( 889 getMessages().getString("logging_prefix")); 891 return messageFormat.format( 892 new String []{toString(level), message}); 893 } 894 895 902 abstract protected void logInternal (int level, String message); 903 } 904 | Popular Tags |