< prev index next >

jdk/src/java.logging/share/classes/java/util/logging/Logger.java

Print this page




 430     //
 431     // As an interim solution, if the immediate caller whose caller loader is
 432     // null, we assume it's a system logger and add it to the system context.
 433     // These system loggers only set the resource bundle to the given
 434     // resource bundle name (rather than the default system resource bundle).
 435     private static class SystemLoggerHelper {
 436         static boolean disableCallerCheck = getBooleanProperty("sun.util.logging.disableCallerCheck");
 437         private static boolean getBooleanProperty(final String key) {
 438             String s = AccessController.doPrivileged(new PrivilegedAction<String>() {
 439                 @Override
 440                 public String run() {
 441                     return System.getProperty(key);
 442                 }
 443             });
 444             return Boolean.valueOf(s);
 445         }
 446     }
 447 
 448     private static Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
 449         LogManager manager = LogManager.getLogManager();
 450         SecurityManager sm = System.getSecurityManager();
 451         if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
 452             if (caller.getClassLoader() == null) {
 453                 return manager.demandSystemLogger(name, resourceBundleName, caller);
 454             }
 455         }
 456         return manager.demandLogger(name, resourceBundleName, caller);
 457         // ends up calling new Logger(name, resourceBundleName, caller)
 458         // iff the logger doesn't exist already
 459     }
 460 
 461     /**
 462      * Find or create a logger for a named subsystem.  If a logger has
 463      * already been created with the given name it is returned.  Otherwise
 464      * a new logger is created.
 465      * <p>
 466      * If a new logger is created its log level will be configured
 467      * based on the LogManager configuration and it will configured
 468      * to also send logging output to its parent's Handlers.  It will
 469      * be registered in the LogManager global namespace.
 470      * <p>
 471      * Note: The LogManager may only retain a weak reference to the newly


1268      *                         can be {@code null}.
1269      * @param   msg     The string message (or a key in the message catalog)
1270      * @param   params  Parameters to the message (optional, may be none).
1271      * @since 1.8
1272      */
1273     public void logrb(Level level, String sourceClass, String sourceMethod,
1274                       ResourceBundle bundle, String msg, Object... params) {
1275         if (!isLoggable(level)) {
1276             return;
1277         }
1278         LogRecord lr = new LogRecord(level, msg);
1279         lr.setSourceClassName(sourceClass);
1280         lr.setSourceMethodName(sourceMethod);
1281         if (params != null && params.length != 0) {
1282             lr.setParameters(params);
1283         }
1284         doLog(lr, bundle);
1285     }
1286 
1287     /**






























1288      * Log a message, specifying source class, method, and resource bundle name,
1289      * with associated Throwable information.
1290      * <p>
1291      * If the logger is currently enabled for the given message
1292      * level then the given arguments are stored in a LogRecord
1293      * which is forwarded to all registered output handlers.
1294      * <p>
1295      * The msg string is localized using the named resource bundle.  If the
1296      * resource bundle name is null, or an empty String or invalid
1297      * then the msg string is not localized.
1298      * <p>
1299      * Note that the thrown argument is stored in the LogRecord thrown
1300      * property, rather than the LogRecord parameters property.  Thus it is
1301      * processed specially by output Formatters and is not treated
1302      * as a formatting parameter to the LogRecord message property.
1303      *
1304      * @param   level   One of the message level identifiers, e.g., SEVERE
1305      * @param   sourceClass    name of class that issued the logging request
1306      * @param   sourceMethod   name of method that issued the logging request
1307      * @param   bundleName     name of resource bundle to localize msg,


1346      * @param   sourceClass    Name of the class that issued the logging request
1347      * @param   sourceMethod   Name of the method that issued the logging request
1348      * @param   bundle         Resource bundle to localize {@code msg},
1349      *                         can be {@code null}
1350      * @param   msg     The string message (or a key in the message catalog)
1351      * @param   thrown  Throwable associated with the log message.
1352      * @since 1.8
1353      */
1354     public void logrb(Level level, String sourceClass, String sourceMethod,
1355                       ResourceBundle bundle, String msg, Throwable thrown) {
1356         if (!isLoggable(level)) {
1357             return;
1358         }
1359         LogRecord lr = new LogRecord(level, msg);
1360         lr.setSourceClassName(sourceClass);
1361         lr.setSourceMethodName(sourceMethod);
1362         lr.setThrown(thrown);
1363         doLog(lr, bundle);
1364     }
1365 


































1366     //======================================================================
1367     // Start of convenience methods for logging method entries and returns.
1368     //======================================================================
1369 
1370     /**
1371      * Log a method entry.
1372      * <p>
1373      * This is a convenience method that can be used to log entry
1374      * to a method.  A LogRecord with message "ENTRY", log level
1375      * FINER, and the given sourceMethod and sourceClass is logged.
1376      *
1377      * @param   sourceClass    name of class that issued the logging request
1378      * @param   sourceMethod   name of method that is being entered
1379      */
1380     public void entering(String sourceClass, String sourceMethod) {
1381         logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
1382     }
1383 
1384     /**
1385      * Log a method entry, with one parameter.




 430     //
 431     // As an interim solution, if the immediate caller whose caller loader is
 432     // null, we assume it's a system logger and add it to the system context.
 433     // These system loggers only set the resource bundle to the given
 434     // resource bundle name (rather than the default system resource bundle).
 435     private static class SystemLoggerHelper {
 436         static boolean disableCallerCheck = getBooleanProperty("sun.util.logging.disableCallerCheck");
 437         private static boolean getBooleanProperty(final String key) {
 438             String s = AccessController.doPrivileged(new PrivilegedAction<String>() {
 439                 @Override
 440                 public String run() {
 441                     return System.getProperty(key);
 442                 }
 443             });
 444             return Boolean.valueOf(s);
 445         }
 446     }
 447 
 448     private static Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
 449         LogManager manager = LogManager.getLogManager();
 450         if (!SystemLoggerHelper.disableCallerCheck) {

 451             if (caller.getClassLoader() == null) {
 452                 return manager.demandSystemLogger(name, resourceBundleName, caller);
 453             }
 454         }
 455         return manager.demandLogger(name, resourceBundleName, caller);
 456         // ends up calling new Logger(name, resourceBundleName, caller)
 457         // iff the logger doesn't exist already
 458     }
 459 
 460     /**
 461      * Find or create a logger for a named subsystem.  If a logger has
 462      * already been created with the given name it is returned.  Otherwise
 463      * a new logger is created.
 464      * <p>
 465      * If a new logger is created its log level will be configured
 466      * based on the LogManager configuration and it will configured
 467      * to also send logging output to its parent's Handlers.  It will
 468      * be registered in the LogManager global namespace.
 469      * <p>
 470      * Note: The LogManager may only retain a weak reference to the newly


1267      *                         can be {@code null}.
1268      * @param   msg     The string message (or a key in the message catalog)
1269      * @param   params  Parameters to the message (optional, may be none).
1270      * @since 1.8
1271      */
1272     public void logrb(Level level, String sourceClass, String sourceMethod,
1273                       ResourceBundle bundle, String msg, Object... params) {
1274         if (!isLoggable(level)) {
1275             return;
1276         }
1277         LogRecord lr = new LogRecord(level, msg);
1278         lr.setSourceClassName(sourceClass);
1279         lr.setSourceMethodName(sourceMethod);
1280         if (params != null && params.length != 0) {
1281             lr.setParameters(params);
1282         }
1283         doLog(lr, bundle);
1284     }
1285 
1286     /**
1287      * Log a message, specifying source class, method, and resource bundle,
1288      * with an optional list of message parameters.
1289      * <p>
1290      * If the logger is currently enabled for the given message
1291      * level then a corresponding LogRecord is created and forwarded
1292      * to all the registered output Handler objects.
1293      * <p>
1294      * The {@code msg} string is localized using the given resource bundle.
1295      * If the resource bundle is {@code null}, then the {@code msg} string is not
1296      * localized.
1297      * <p>
1298      * @param   level   One of the message level identifiers, e.g., SEVERE
1299      * @param   bundle         Resource bundle to localize {@code msg},
1300      *                         can be {@code null}.
1301      * @param   msg     The string message (or a key in the message catalog)
1302      * @param   params  Parameters to the message (optional, may be none).
1303      * @since 1.9
1304      */
1305     public void logrb(Level level, ResourceBundle bundle, String msg, Object... params) {
1306         if (!isLoggable(level)) {
1307             return;
1308         }
1309         LogRecord lr = new LogRecord(level, msg);
1310         if (params != null && params.length != 0) {
1311             lr.setParameters(params);
1312         }
1313         doLog(lr, bundle);
1314     }
1315 
1316     /**
1317      * Log a message, specifying source class, method, and resource bundle name,
1318      * with associated Throwable information.
1319      * <p>
1320      * If the logger is currently enabled for the given message
1321      * level then the given arguments are stored in a LogRecord
1322      * which is forwarded to all registered output handlers.
1323      * <p>
1324      * The msg string is localized using the named resource bundle.  If the
1325      * resource bundle name is null, or an empty String or invalid
1326      * then the msg string is not localized.
1327      * <p>
1328      * Note that the thrown argument is stored in the LogRecord thrown
1329      * property, rather than the LogRecord parameters property.  Thus it is
1330      * processed specially by output Formatters and is not treated
1331      * as a formatting parameter to the LogRecord message property.
1332      *
1333      * @param   level   One of the message level identifiers, e.g., SEVERE
1334      * @param   sourceClass    name of class that issued the logging request
1335      * @param   sourceMethod   name of method that issued the logging request
1336      * @param   bundleName     name of resource bundle to localize msg,


1375      * @param   sourceClass    Name of the class that issued the logging request
1376      * @param   sourceMethod   Name of the method that issued the logging request
1377      * @param   bundle         Resource bundle to localize {@code msg},
1378      *                         can be {@code null}
1379      * @param   msg     The string message (or a key in the message catalog)
1380      * @param   thrown  Throwable associated with the log message.
1381      * @since 1.8
1382      */
1383     public void logrb(Level level, String sourceClass, String sourceMethod,
1384                       ResourceBundle bundle, String msg, Throwable thrown) {
1385         if (!isLoggable(level)) {
1386             return;
1387         }
1388         LogRecord lr = new LogRecord(level, msg);
1389         lr.setSourceClassName(sourceClass);
1390         lr.setSourceMethodName(sourceMethod);
1391         lr.setThrown(thrown);
1392         doLog(lr, bundle);
1393     }
1394 
1395     /**
1396      * Log a message, specifying source class, method, and resource bundle,
1397      * with associated Throwable information.
1398      * <p>
1399      * If the logger is currently enabled for the given message
1400      * level then the given arguments are stored in a LogRecord
1401      * which is forwarded to all registered output handlers.
1402      * <p>
1403      * The {@code msg} string is localized using the given resource bundle.
1404      * If the resource bundle is {@code null}, then the {@code msg} string is not
1405      * localized.
1406      * <p>
1407      * Note that the thrown argument is stored in the LogRecord thrown
1408      * property, rather than the LogRecord parameters property.  Thus it is
1409      * processed specially by output Formatters and is not treated
1410      * as a formatting parameter to the LogRecord message property.
1411      * <p>
1412      * @param   level   One of the message level identifiers, e.g., SEVERE
1413      * @param   bundle         Resource bundle to localize {@code msg},
1414      *                         can be {@code null}
1415      * @param   msg     The string message (or a key in the message catalog)
1416      * @param   thrown  Throwable associated with the log message.
1417      * @since 1.9
1418      */
1419     public void logrb(Level level, ResourceBundle bundle, String msg,
1420             Throwable thrown) {
1421         if (!isLoggable(level)) {
1422             return;
1423         }
1424         LogRecord lr = new LogRecord(level, msg);
1425         lr.setThrown(thrown);
1426         doLog(lr, bundle);
1427     }
1428 
1429     //======================================================================
1430     // Start of convenience methods for logging method entries and returns.
1431     //======================================================================
1432 
1433     /**
1434      * Log a method entry.
1435      * <p>
1436      * This is a convenience method that can be used to log entry
1437      * to a method.  A LogRecord with message "ENTRY", log level
1438      * FINER, and the given sourceMethod and sourceClass is logged.
1439      *
1440      * @param   sourceClass    name of class that issued the logging request
1441      * @param   sourceMethod   name of method that is being entered
1442      */
1443     public void entering(String sourceClass, String sourceMethod) {
1444         logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
1445     }
1446 
1447     /**
1448      * Log a method entry, with one parameter.


< prev index next >