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
1237 * java.lang.Object...)} instead.
1238 */
1239 @Deprecated
1240 public void logrb(Level level, String sourceClass, String sourceMethod,
1241 String bundleName, String msg, Object params[]) {
1242 if (!isLoggable(level)) {
1243 return;
1244 }
1245 LogRecord lr = new LogRecord(level, msg);
1246 lr.setSourceClassName(sourceClass);
1247 lr.setSourceMethodName(sourceMethod);
1248 lr.setParameters(params);
1249 doLog(lr, bundleName);
1250 }
1251
1252 /**
1253 * Log a message, specifying source class, method, and resource bundle,
1254 * with an optional list of message parameters.
1255 * <p>
1256 * If the logger is currently enabled for the given message
1257 * level then a corresponding LogRecord is created and forwarded
1258 * to all the registered output Handler objects.
1259 * <p>
1260 * The {@code msg} string is localized using the given resource bundle.
1261 * If the resource bundle is {@code null}, then the {@code msg} string is not
1262 * localized.
1263 *
1264 * @param level One of the message level identifiers, e.g., SEVERE
1265 * @param sourceClass Name of the class that issued the logging request
1266 * @param sourceMethod Name of the method that issued the logging request
1267 * @param bundle Resource bundle to localize {@code msg},
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,
1313 * java.lang.Throwable)} instead.
1314 */
1315 @Deprecated
1316 public void logrb(Level level, String sourceClass, String sourceMethod,
1317 String bundleName, String msg, Throwable thrown) {
1318 if (!isLoggable(level)) {
1319 return;
1320 }
1321 LogRecord lr = new LogRecord(level, msg);
1322 lr.setSourceClassName(sourceClass);
1323 lr.setSourceMethodName(sourceMethod);
1324 lr.setThrown(thrown);
1325 doLog(lr, bundleName);
1326 }
1327
1328 /**
1329 * Log a message, specifying source class, method, and resource bundle,
1330 * with associated Throwable information.
1331 * <p>
1332 * If the logger is currently enabled for the given message
1333 * level then the given arguments are stored in a LogRecord
1334 * which is forwarded to all registered output handlers.
1335 * <p>
1336 * The {@code msg} string is localized using the given resource bundle.
1337 * If the resource bundle is {@code null}, then the {@code msg} string is not
1338 * localized.
1339 * <p>
1340 * Note that the thrown argument is stored in the LogRecord thrown
1341 * property, rather than the LogRecord parameters property. Thus it is
1342 * processed specially by output Formatters and is not treated
1343 * as a formatting parameter to the LogRecord message property.
1344 *
1345 * @param level One of the message level identifiers, e.g., SEVERE
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
1236 * java.lang.Object...)} instead.
1237 */
1238 @Deprecated
1239 public void logrb(Level level, String sourceClass, String sourceMethod,
1240 String bundleName, String msg, Object params[]) {
1241 if (!isLoggable(level)) {
1242 return;
1243 }
1244 LogRecord lr = new LogRecord(level, msg);
1245 lr.setSourceClassName(sourceClass);
1246 lr.setSourceMethodName(sourceMethod);
1247 lr.setParameters(params);
1248 doLog(lr, bundleName);
1249 }
1250
1251 /**
1252 * Log a message, specifying source class, method, and resource bundle,
1253 * with an optional list of message parameters.
1254 * <p>
1255 * If the logger is currently enabled for the given message
1256 * {@code level} then a corresponding {@code LogRecord} is created and
1257 * forwarded to all the registered output {@code Handler} objects.
1258 * <p>
1259 * The {@code msg} string is localized using the given resource bundle.
1260 * If the resource bundle is {@code null}, then the {@code msg} string is not
1261 * localized.
1262 *
1263 * @param level One of the message level identifiers, e.g., {@code SEVERE}
1264 * @param sourceClass Name of the class that issued the logging request
1265 * @param sourceMethod Name of the method that issued the logging request
1266 * @param bundle Resource bundle to localize {@code msg},
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 * {@code level} then a corresponding {@code LogRecord} is created
1292 * and forwarded to all the registered output {@code 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., {@code 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,
1342 * java.lang.Throwable)} instead.
1343 */
1344 @Deprecated
1345 public void logrb(Level level, String sourceClass, String sourceMethod,
1346 String bundleName, String msg, Throwable thrown) {
1347 if (!isLoggable(level)) {
1348 return;
1349 }
1350 LogRecord lr = new LogRecord(level, msg);
1351 lr.setSourceClassName(sourceClass);
1352 lr.setSourceMethodName(sourceMethod);
1353 lr.setThrown(thrown);
1354 doLog(lr, bundleName);
1355 }
1356
1357 /**
1358 * Log a message, specifying source class, method, and resource bundle,
1359 * with associated Throwable information.
1360 * <p>
1361 * If the logger is currently enabled for the given message
1362 * {@code level} then the given arguments are stored in a {@code LogRecord}
1363 * which is forwarded to all registered output handlers.
1364 * <p>
1365 * The {@code msg} string is localized using the given resource bundle.
1366 * If the resource bundle is {@code null}, then the {@code msg} string is not
1367 * localized.
1368 * <p>
1369 * Note that the {@code thrown} argument is stored in the {@code LogRecord}
1370 * {@code thrown} property, rather than the {@code LogRecord}
1371 * {@code parameters} property. Thus it is
1372 * processed specially by output {@code Formatter} objects and is not treated
1373 * as a formatting parameter to the {@code LogRecord} {@code message} property.
1374 *
1375 * @param level One of the message level identifiers, e.g., {@code SEVERE}
1376 * @param sourceClass Name of the class that issued the logging request
1377 * @param sourceMethod Name of the method that issued the logging request
1378 * @param bundle Resource bundle to localize {@code msg},
1379 * can be {@code null}
1380 * @param msg The string message (or a key in the message catalog)
1381 * @param thrown Throwable associated with the log message.
1382 * @since 1.8
1383 */
1384 public void logrb(Level level, String sourceClass, String sourceMethod,
1385 ResourceBundle bundle, String msg, Throwable thrown) {
1386 if (!isLoggable(level)) {
1387 return;
1388 }
1389 LogRecord lr = new LogRecord(level, msg);
1390 lr.setSourceClassName(sourceClass);
1391 lr.setSourceMethodName(sourceMethod);
1392 lr.setThrown(thrown);
1393 doLog(lr, bundle);
1394 }
1395
1396 /**
1397 * Log a message, specifying source class, method, and resource bundle,
1398 * with associated Throwable information.
1399 * <p>
1400 * If the logger is currently enabled for the given message
1401 * {@code level} then the given arguments are stored in a {@code LogRecord}
1402 * which is forwarded to all registered output handlers.
1403 * <p>
1404 * The {@code msg} string is localized using the given resource bundle.
1405 * If the resource bundle is {@code null}, then the {@code msg} string is not
1406 * localized.
1407 * <p>
1408 * Note that the {@code thrown} argument is stored in the {@code LogRecord}
1409 * {@code thrown} property, rather than the {@code LogRecord}
1410 * {@code parameters} property. Thus it is
1411 * processed specially by output {@code Formatter} objects and is not treated
1412 * as a formatting parameter to the {@code LogRecord} {@code message}
1413 * property.
1414 * <p>
1415 * @param level One of the message level identifiers, e.g., {@code SEVERE}
1416 * @param bundle Resource bundle to localize {@code msg};
1417 * can be {@code null}.
1418 * @param msg The string message (or a key in the message catalog)
1419 * @param thrown Throwable associated with the log message.
1420 * @since 1.9
1421 */
1422 public void logrb(Level level, ResourceBundle bundle, String msg,
1423 Throwable thrown) {
1424 if (!isLoggable(level)) {
1425 return;
1426 }
1427 LogRecord lr = new LogRecord(level, msg);
1428 lr.setThrown(thrown);
1429 doLog(lr, bundle);
1430 }
1431
1432 //======================================================================
1433 // Start of convenience methods for logging method entries and returns.
1434 //======================================================================
1435
1436 /**
1437 * Log a method entry.
1438 * <p>
1439 * This is a convenience method that can be used to log entry
1440 * to a method. A LogRecord with message "ENTRY", log level
1441 * FINER, and the given sourceMethod and sourceClass is logged.
1442 *
1443 * @param sourceClass name of class that issued the logging request
1444 * @param sourceMethod name of method that is being entered
1445 */
1446 public void entering(String sourceClass, String sourceMethod) {
1447 logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
1448 }
1449
1450 /**
1451 * Log a method entry, with one parameter.
|