1 7 8 9 package java.util.logging; 10 11 import java.util.*; 12 import java.security.*; 13 import java.lang.ref.WeakReference ; 14 15 144 145 146 public class Logger { 147 private static final Handler emptyHandlers[] = new Handler [0]; 148 private static final int offValue = Level.OFF.intValue(); 149 private LogManager manager; 150 private String name; 151 private ArrayList handlers; 152 private String resourceBundleName; 153 private boolean useParentHandlers = true; 154 private Filter filter; 155 private boolean anonymous; 156 157 private ResourceBundle catalog; private String catalogName; private Locale catalogLocale; 161 private static Object treeLock = new Object (); 164 private Logger parent; private ArrayList kids; private Level levelObject; 169 private volatile int levelValue; 171 181 public static final Logger global = new Logger ("global"); 182 183 200 protected Logger(String name, String resourceBundleName) { 201 this.manager = LogManager.getLogManager(); 202 if (resourceBundleName != null) { 203 setupResourceInfo(resourceBundleName); 205 } 206 this.name = name; 207 levelValue = Level.INFO.intValue(); 208 } 209 210 private Logger(String name) { 214 this.name = name; 216 levelValue = Level.INFO.intValue(); 217 } 218 219 void setLogManager(LogManager manager) { 222 this.manager = manager; 223 } 224 225 private void checkAccess() throws SecurityException { 226 if (!anonymous) { 227 if (manager == null) { 228 manager = LogManager.getLogManager(); 230 } 231 manager.checkAccess(); 232 } 233 } 234 235 253 public static synchronized Logger getLogger(String name) { 254 LogManager manager = LogManager.getLogManager(); 255 Logger result = manager.getLogger(name); 256 if (result == null) { 257 result = new Logger (name, null); 258 manager.addLogger(result); 259 result = manager.getLogger(name); 260 } 261 return result; 262 } 263 264 294 public static synchronized Logger getLogger(String name, String resourceBundleName) { 295 LogManager manager = LogManager.getLogManager(); 296 Logger result = manager.getLogger(name); 297 if (result == null) { 298 result = new Logger (name, resourceBundleName); 301 manager.addLogger(result); 302 result = manager.getLogger(name); 303 } 304 if (result.resourceBundleName == null) { 305 result.setupResourceInfo(resourceBundleName); 307 } else if (!result.resourceBundleName.equals(resourceBundleName)) { 308 throw new IllegalArgumentException (result.resourceBundleName + 309 " != " + resourceBundleName); 310 } 311 return result; 312 } 313 314 315 335 public static synchronized Logger getAnonymousLogger() { 336 LogManager manager = LogManager.getLogManager(); 337 Logger result = new Logger (null, null); 338 result.anonymous = true; 339 Logger root = manager.getLogger(""); 340 result.doSetParent(root); 341 return result; 342 } 343 344 367 public static synchronized Logger getAnonymousLogger(String resourceBundleName) { 368 LogManager manager = LogManager.getLogManager(); 369 Logger result = new Logger (null, resourceBundleName); 370 result.anonymous = true; 371 Logger root = manager.getLogger(""); 372 result.doSetParent(root); 373 return result; 374 } 375 376 384 public ResourceBundle getResourceBundle() { 385 return findResourceBundle(getResourceBundleName()); 386 } 387 388 395 public String getResourceBundleName() { 396 return resourceBundleName; 397 } 398 399 410 public void setFilter(Filter newFilter) throws SecurityException { 411 checkAccess(); 412 filter = newFilter; 413 } 414 415 420 public Filter getFilter() { 421 return filter; 422 } 423 424 433 public void log(LogRecord record) { 434 if (record.getLevel().intValue() < levelValue || levelValue == offValue) { 435 return; 436 } 437 synchronized (this) { 438 if (filter != null && !filter.isLoggable(record)) { 439 return; 440 } 441 } 442 443 446 Logger logger = this; 447 while (logger != null) { 448 Handler targets[] = logger.getHandlers(); 449 450 if (targets != null) { 451 for (int i = 0; i < targets.length; i++) { 452 targets[i].publish(record); 453 } 454 } 455 456 if (!logger.getUseParentHandlers()) { 457 break; 458 } 459 460 logger = logger.getParent(); 461 } 462 } 463 464 private void doLog(LogRecord lr) { 468 lr.setLoggerName(name); 469 String ebname = getEffectiveResourceBundleName(); 470 if (ebname != null) { 471 lr.setResourceBundleName(ebname); 472 lr.setResourceBundle(findResourceBundle(ebname)); 473 } 474 log(lr); 475 } 476 477 478 482 492 public void log(Level level, String msg) { 493 if (level.intValue() < levelValue || levelValue == offValue) { 494 return; 495 } 496 LogRecord lr = new LogRecord (level, msg); 497 doLog(lr); 498 } 499 500 511 public void log(Level level, String msg, Object param1) { 512 if (level.intValue() < levelValue || levelValue == offValue) { 513 return; 514 } 515 LogRecord lr = new LogRecord (level, msg); 516 Object params[] = { param1 }; 517 lr.setParameters(params); 518 doLog(lr); 519 } 520 521 532 public void log(Level level, String msg, Object params[]) { 533 if (level.intValue() < levelValue || levelValue == offValue) { 534 return; 535 } 536 LogRecord lr = new LogRecord (level, msg); 537 lr.setParameters(params); 538 doLog(lr); 539 } 540 541 557 public void log(Level level, String msg, Throwable thrown) { 558 if (level.intValue() < levelValue || levelValue == offValue) { 559 return; 560 } 561 LogRecord lr = new LogRecord (level, msg); 562 lr.setThrown(thrown); 563 doLog(lr); 564 } 565 566 570 583 public void logp(Level level, String sourceClass, String sourceMethod, String msg) { 584 if (level.intValue() < levelValue || levelValue == offValue) { 585 return; 586 } 587 LogRecord lr = new LogRecord (level, msg); 588 lr.setSourceClassName(sourceClass); 589 lr.setSourceMethodName(sourceMethod); 590 doLog(lr); 591 } 592 593 607 public void logp(Level level, String sourceClass, String sourceMethod, 608 String msg, Object param1) { 609 if (level.intValue() < levelValue || levelValue == offValue) { 610 return; 611 } 612 LogRecord lr = new LogRecord (level, msg); 613 lr.setSourceClassName(sourceClass); 614 lr.setSourceMethodName(sourceMethod); 615 Object params[] = { param1 }; 616 lr.setParameters(params); 617 doLog(lr); 618 } 619 620 634 public void logp(Level level, String sourceClass, String sourceMethod, 635 String msg, Object params[]) { 636 if (level.intValue() < levelValue || levelValue == offValue) { 637 return; 638 } 639 LogRecord lr = new LogRecord (level, msg); 640 lr.setSourceClassName(sourceClass); 641 lr.setSourceMethodName(sourceMethod); 642 lr.setParameters(params); 643 doLog(lr); 644 } 645 646 665 public void logp(Level level, String sourceClass, String sourceMethod, 666 String msg, Throwable thrown) { 667 if (level.intValue() < levelValue || levelValue == offValue) { 668 return; 669 } 670 LogRecord lr = new LogRecord (level, msg); 671 lr.setSourceClassName(sourceClass); 672 lr.setSourceMethodName(sourceMethod); 673 lr.setThrown(thrown); 674 doLog(lr); 675 } 676 677 678 682 private void doLog(LogRecord lr, String rbname) { 686 lr.setLoggerName(name); 687 if (rbname != null) { 688 lr.setResourceBundleName(rbname); 689 lr.setResourceBundle(findResourceBundle(rbname)); 690 } 691 log(lr); 692 } 693 694 713 714 public void logrb(Level level, String sourceClass, String sourceMethod, 715 String bundleName, String msg) { 716 if (level.intValue() < levelValue || levelValue == offValue) { 717 return; 718 } 719 LogRecord lr = new LogRecord (level, msg); 720 lr.setSourceClassName(sourceClass); 721 lr.setSourceMethodName(sourceMethod); 722 doLog(lr, bundleName); 723 } 724 725 745 public void logrb(Level level, String sourceClass, String sourceMethod, 746 String bundleName, String msg, Object param1) { 747 if (level.intValue() < levelValue || levelValue == offValue) { 748 return; 749 } 750 LogRecord lr = new LogRecord (level, msg); 751 lr.setSourceClassName(sourceClass); 752 lr.setSourceMethodName(sourceMethod); 753 Object params[] = { param1 }; 754 lr.setParameters(params); 755 doLog(lr, bundleName); 756 } 757 758 778 public void logrb(Level level, String sourceClass, String sourceMethod, 779 String bundleName, String msg, Object params[]) { 780 if (level.intValue() < levelValue || levelValue == offValue) { 781 return; 782 } 783 LogRecord lr = new LogRecord (level, msg); 784 lr.setSourceClassName(sourceClass); 785 lr.setSourceMethodName(sourceMethod); 786 lr.setParameters(params); 787 doLog(lr, bundleName); 788 } 789 790 815 public void logrb(Level level, String sourceClass, String sourceMethod, 816 String bundleName, String msg, Throwable thrown) { 817 if (level.intValue() < levelValue || levelValue == offValue) { 818 return; 819 } 820 LogRecord lr = new LogRecord (level, msg); 821 lr.setSourceClassName(sourceClass); 822 lr.setSourceMethodName(sourceMethod); 823 lr.setThrown(thrown); 824 doLog(lr, bundleName); 825 } 826 827 828 832 842 public void entering(String sourceClass, String sourceMethod) { 843 if (Level.FINER.intValue() < levelValue) { 844 return; 845 } 846 logp(Level.FINER, sourceClass, sourceMethod, "ENTRY"); 847 } 848 849 861 public void entering(String sourceClass, String sourceMethod, Object param1) { 862 if (Level.FINER.intValue() < levelValue) { 863 return; 864 } 865 Object params[] = { param1 }; 866 logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", params); 867 } 868 869 882 public void entering(String sourceClass, String sourceMethod, Object params[]) { 883 if (Level.FINER.intValue() < levelValue) { 884 return; 885 } 886 String msg = "ENTRY"; 887 if (params == null ) { 888 logp(Level.FINER, sourceClass, sourceMethod, msg); 889 return; 890 } 891 for (int i = 0; i < params.length; i++) { 892 msg = msg + " {" + i + "}"; 893 } 894 logp(Level.FINER, sourceClass, sourceMethod, msg, params); 895 } 896 897 907 public void exiting(String sourceClass, String sourceMethod) { 908 if (Level.FINER.intValue() < levelValue) { 909 return; 910 } 911 logp(Level.FINER, sourceClass, sourceMethod, "RETURN"); 912 } 913 914 915 927 public void exiting(String sourceClass, String sourceMethod, Object result) { 928 if (Level.FINER.intValue() < levelValue) { 929 return; 930 } 931 Object params[] = { result }; 932 logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", result); 933 } 934 935 956 public void throwing(String sourceClass, String sourceMethod, Throwable thrown) { 957 if (Level.FINER.intValue() < levelValue || levelValue == offValue ) { 958 return; 959 } 960 LogRecord lr = new LogRecord (Level.FINER, "THROW"); 961 lr.setSourceClassName(sourceClass); 962 lr.setSourceMethodName(sourceMethod); 963 lr.setThrown(thrown); 964 doLog(lr); 965 } 966 967 971 980 public void severe(String msg) { 981 if (Level.SEVERE.intValue() < levelValue) { 982 return; 983 } 984 log(Level.SEVERE, msg); 985 } 986 987 996 public void warning(String msg) { 997 if (Level.WARNING.intValue() < levelValue) { 998 return; 999 } 1000 log(Level.WARNING, msg); 1001 } 1002 1003 1012 public void info(String msg) { 1013 if (Level.INFO.intValue() < levelValue) { 1014 return; 1015 } 1016 log(Level.INFO, msg); 1017 } 1018 1019 1028 public void config(String msg) { 1029 if (Level.CONFIG.intValue() < levelValue) { 1030 return; 1031 } 1032 log(Level.CONFIG, msg); 1033 } 1034 1035 1044 public void fine(String msg) { 1045 if (Level.FINE.intValue() < levelValue) { 1046 return; 1047 } 1048 log(Level.FINE, msg); 1049 } 1050 1051 1060 public void finer(String msg) { 1061 if (Level.FINER.intValue() < levelValue) { 1062 return; 1063 } 1064 log(Level.FINER, msg); 1065 } 1066 1067 1076 public void finest(String msg) { 1077 if (Level.FINEST.intValue() < levelValue) { 1078 return; 1079 } 1080 log(Level.FINEST, msg); 1081 } 1082 1083 1087 1101 public void setLevel(Level newLevel) throws SecurityException { 1102 checkAccess(); 1103 synchronized (treeLock) { 1104 levelObject = newLevel; 1105 updateEffectiveLevel(); 1106 } 1107 } 1108 1109 1116 public Level getLevel() { 1117 return levelObject; 1118 } 1119 1120 1128 public boolean isLoggable(Level level) { 1129 if (level.intValue() < levelValue || levelValue == offValue) { 1130 return false; 1131 } 1132 return true; 1133 } 1134 1135 1139 public String getName() { 1140 return name; 1141 } 1142 1143 1154 public synchronized void addHandler(Handler handler) throws SecurityException { 1155 handler.getClass(); 1157 checkAccess(); 1158 if (handlers == null) { 1159 handlers = new ArrayList(); 1160 } 1161 handlers.add(handler); 1162 } 1163 1164 1173 public synchronized void removeHandler(Handler handler) throws SecurityException { 1174 checkAccess(); 1175 if (handler == null) { 1176 return; 1177 } 1178 if (handlers == null) { 1179 return; 1180 } 1181 handlers.remove(handler); 1182 } 1183 1184 1189 public synchronized Handler [] getHandlers() { 1190 if (handlers == null) { 1191 return emptyHandlers; 1192 } 1193 Handler result[] = new Handler [handlers.size()]; 1194 result = (Handler [])handlers.toArray(result); 1195 return result; 1196 } 1197 1198 1209 public synchronized void setUseParentHandlers(boolean useParentHandlers) { 1210 checkAccess(); 1211 this.useParentHandlers = useParentHandlers; 1212 } 1213 1214 1220 public synchronized boolean getUseParentHandlers() { 1221 return useParentHandlers; 1222 } 1223 1224 1230 private synchronized ResourceBundle findResourceBundle(String name) { 1231 if (name == null) { 1233 return null; 1234 } 1235 1236 Locale currentLocale = Locale.getDefault(); 1237 1238 if (catalog != null && currentLocale == catalogLocale 1240 && name == catalogName) { 1241 return catalog; 1242 } 1243 1244 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 1247 if (cl == null) { 1248 cl = ClassLoader.getSystemClassLoader(); 1249 } 1250 try { 1251 catalog = ResourceBundle.getBundle(name, currentLocale, cl); 1252 catalogName = name; 1253 catalogLocale = currentLocale; 1254 return catalog; 1255 } catch (MissingResourceException ex) { 1256 } 1259 1260 1261 for (int ix = 0; ; ix++) { 1264 Class clz = sun.reflect.Reflection.getCallerClass(ix); 1265 if (clz == null) { 1266 break; 1267 } 1268 ClassLoader cl2 = clz.getClassLoader(); 1269 if (cl2 == null) { 1270 cl2 = ClassLoader.getSystemClassLoader(); 1271 } 1272 if (cl == cl2) { 1273 continue; 1275 } 1276 cl = cl2; 1277 try { 1278 catalog = ResourceBundle.getBundle(name, currentLocale, cl); 1279 catalogName = name; 1280 catalogLocale = currentLocale; 1281 return catalog; 1282 } catch (MissingResourceException ex) { 1283 } 1286 } 1287 1288 if (name.equals(catalogName)) { 1289 return catalog; 1292 } 1293 return null; 1295 } 1296 1297 private synchronized void setupResourceInfo(String name) { 1303 if (name == null) { 1304 return; 1305 } 1306 ResourceBundle rb = findResourceBundle(name); 1307 if (rb == null) { 1308 throw new MissingResourceException("Can't find " + name + " bundle", name, ""); 1310 } 1311 resourceBundleName = name; 1312 } 1313 1314 1327 public Logger getParent() { 1328 synchronized (treeLock) { 1329 return parent; 1330 } 1331 } 1332 1333 1343 public void setParent(Logger parent) { 1344 if (parent == null) { 1345 throw new NullPointerException (); 1346 } 1347 manager.checkAccess(); 1348 doSetParent(parent); 1349 } 1350 1351 private void doSetParent(Logger newParent) { 1354 1355 1358 synchronized (treeLock) { 1359 1360 if (parent != null) { 1362 for (Iterator iter = parent.kids.iterator(); iter.hasNext(); ) { 1364 WeakReference ref = (WeakReference ) iter.next(); 1365 Logger kid = (Logger ) ref.get(); 1366 if (kid == this) { 1367 iter.remove(); 1368 break; 1369 } 1370 } 1371 } 1373 1374 parent = newParent; 1376 if (parent.kids == null) { 1377 parent.kids = new ArrayList(2); 1378 } 1379 parent.kids.add(new WeakReference (this)); 1380 1381 updateEffectiveLevel(); 1384 1385 } 1386 } 1387 1388 1391 private void updateEffectiveLevel() { 1392 1394 int newLevelValue; 1396 if (levelObject != null) { 1397 newLevelValue = levelObject.intValue(); 1398 } else { 1399 if (parent != null) { 1400 newLevelValue = parent.levelValue; 1401 } else { 1402 newLevelValue = Level.INFO.intValue(); 1404 } 1405 } 1406 1407 if (levelValue == newLevelValue) { 1409 return; 1410 } 1411 1412 levelValue = newLevelValue; 1413 1414 1416 if (kids != null) { 1418 for (int i = 0; i < kids.size(); i++) { 1419 WeakReference ref = (WeakReference )kids.get(i); 1420 Logger kid = (Logger ) ref.get(); 1421 if (kid != null) { 1422 kid.updateEffectiveLevel(); 1423 } 1424 } 1425 } 1426 } 1427 1428 1429 private String getEffectiveResourceBundleName() { 1433 Logger target = this; 1434 while (target != null) { 1435 String rbn = target.getResourceBundleName(); 1436 if (rbn != null) { 1437 return rbn; 1438 } 1439 target = target.getParent(); 1440 } 1441 return null; 1442 } 1443 1444 1445} 1446 | Popular Tags |