1550 PrivilegedAction<LoggerFinder> pa =
1551 () -> LoggerFinderLoader.getLoggerFinder();
1552 service = AccessController.doPrivileged(pa, null,
1553 LOGGERFINDER_PERMISSION);
1554 }
1555 return service;
1556 }
1557
1558 }
1559
1560
1561 /**
1562 * Returns an instance of {@link Logger Logger} for the caller's
1563 * use.
1564 *
1565 * @implSpec
1566 * Instances returned by this method route messages to loggers
1567 * obtained by calling {@link LoggerFinder#getLogger(java.lang.String,
1568 * java.lang.reflect.Module) LoggerFinder.getLogger(name, module)}, where
1569 * {@code module} is the caller's module.
1570 *
1571 * @apiNote
1572 * This method may defer calling the {@link
1573 * LoggerFinder#getLogger(java.lang.String, java.lang.reflect.Module)
1574 * LoggerFinder.getLogger} method to create an actual logger supplied by
1575 * the logging backend, for instance, to allow loggers to be obtained during
1576 * the system initialization time.
1577 *
1578 * @param name the name of the logger.
1579 * @return an instance of {@link Logger} that can be used by the calling
1580 * class.
1581 * @throws NullPointerException if {@code name} is {@code null}.
1582 *
1583 * @since 9
1584 */
1585 @CallerSensitive
1586 public static Logger getLogger(String name) {
1587 Objects.requireNonNull(name);
1588 final Class<?> caller = Reflection.getCallerClass();
1589 return LazyLoggers.getLogger(name, caller.getModule());
1590 }
1591
1592 /**
1593 * Returns a localizable instance of {@link Logger
1594 * Logger} for the caller's use.
1595 * The returned logger will use the provided resource bundle for message
1596 * localization.
1597 *
1598 * @implSpec
1599 * The returned logger will perform message localization as specified
1600 * by {@link LoggerFinder#getLocalizedLogger(java.lang.String,
1601 * java.util.ResourceBundle, java.lang.reflect.Module)
1602 * LoggerFinder.getLocalizedLogger(name, bundle, module}, where
1603 * {@code module} is the caller's module.
1604 *
1605 * @apiNote
1606 * This method is intended to be used after the system is fully initialized.
1607 * This method may trigger the immediate loading and initialization
1608 * of the {@link LoggerFinder} service, which may cause issues if the
1609 * Java Runtime is not ready to initialize the concrete service
1610 * implementation yet.
1611 * System classes which may be loaded early in the boot sequence and
1612 * need to log localized messages should create a logger using
1613 * {@link #getLogger(java.lang.String)} and then use the log methods that
1614 * take a resource bundle as parameter.
1615 *
1616 * @param name the name of the logger.
1617 * @param bundle a resource bundle.
1618 * @return an instance of {@link Logger} which will use the provided
1619 * resource bundle for message localization.
1620 * @throws NullPointerException if {@code name} is {@code null} or
1621 * {@code bundle} is {@code null}.
1622 *
1623 * @since 9
1624 */
1625 @CallerSensitive
1626 public static Logger getLogger(String name, ResourceBundle bundle) {
1627 final ResourceBundle rb = Objects.requireNonNull(bundle);
1628 Objects.requireNonNull(name);
1629 final Class<?> caller = Reflection.getCallerClass();
1630 final SecurityManager sm = System.getSecurityManager();
1631 // We don't use LazyLoggers if a resource bundle is specified.
1632 // Bootstrap sensitive classes in the JDK do not use resource bundles
1633 // when logging. This could be revisited later, if it needs to.
1634 if (sm != null) {
1635 final PrivilegedAction<Logger> pa =
1636 () -> LoggerFinder.accessProvider()
1637 .getLocalizedLogger(name, rb, caller.getModule());
1638 return AccessController.doPrivileged(pa, null,
1639 LoggerFinder.LOGGERFINDER_PERMISSION);
1640 }
1641 return LoggerFinder.accessProvider()
1642 .getLocalizedLogger(name, rb, caller.getModule());
1643 }
1644
1645 /**
1646 * Terminates the currently running Java Virtual Machine. The
1647 * argument serves as a status code; by convention, a nonzero status
1648 * code indicates abnormal termination.
1649 * <p>
|
1550 PrivilegedAction<LoggerFinder> pa =
1551 () -> LoggerFinderLoader.getLoggerFinder();
1552 service = AccessController.doPrivileged(pa, null,
1553 LOGGERFINDER_PERMISSION);
1554 }
1555 return service;
1556 }
1557
1558 }
1559
1560
1561 /**
1562 * Returns an instance of {@link Logger Logger} for the caller's
1563 * use.
1564 *
1565 * @implSpec
1566 * Instances returned by this method route messages to loggers
1567 * obtained by calling {@link LoggerFinder#getLogger(java.lang.String,
1568 * java.lang.reflect.Module) LoggerFinder.getLogger(name, module)}, where
1569 * {@code module} is the caller's module.
1570 * In cases where {@code System.getLogger} is called from a context where
1571 * there is no caller frame on the stack (e.g when called directly
1572 * from a JNI attached thread), {@code IllegalCallerException} is thrown.
1573 * To obtain a logger in such a context, use an auxiliary class that will
1574 * implicitly be identified as the caller, or use the system {@link
1575 * LoggerFinder#getLoggerFinder() LoggerFinder} to obtain a logger instead.
1576 * Note that doing the latter may eagerly initialize the underlying
1577 * logging system.
1578 *
1579 * @apiNote
1580 * This method may defer calling the {@link
1581 * LoggerFinder#getLogger(java.lang.String, java.lang.reflect.Module)
1582 * LoggerFinder.getLogger} method to create an actual logger supplied by
1583 * the logging backend, for instance, to allow loggers to be obtained during
1584 * the system initialization time.
1585 *
1586 * @param name the name of the logger.
1587 * @return an instance of {@link Logger} that can be used by the calling
1588 * class.
1589 * @throws NullPointerException if {@code name} is {@code null}.
1590 * @throws IllegalCallerException if there is no Java caller frame on the
1591 * stack.
1592 *
1593 * @since 9
1594 */
1595 @CallerSensitive
1596 public static Logger getLogger(String name) {
1597 Objects.requireNonNull(name);
1598 final Class<?> caller = Reflection.getCallerClass();
1599 if (caller == null) {
1600 throw new IllegalCallerException("no caller frame");
1601 }
1602 return LazyLoggers.getLogger(name, caller.getModule());
1603 }
1604
1605 /**
1606 * Returns a localizable instance of {@link Logger
1607 * Logger} for the caller's use.
1608 * The returned logger will use the provided resource bundle for message
1609 * localization.
1610 *
1611 * @implSpec
1612 * The returned logger will perform message localization as specified
1613 * by {@link LoggerFinder#getLocalizedLogger(java.lang.String,
1614 * java.util.ResourceBundle, java.lang.reflect.Module)
1615 * LoggerFinder.getLocalizedLogger(name, bundle, module)}, where
1616 * {@code module} is the caller's module.
1617 * In cases where {@code System.getLogger} is called from a context where
1618 * there is no caller frame on the stack (e.g when called directly
1619 * from a JNI attached thread), {@code IllegalCallerException} is thrown.
1620 * To obtain a logger in such a context, use an auxiliary class that
1621 * will implicitly be identified as the caller, or use the system {@link
1622 * LoggerFinder#getLoggerFinder() LoggerFinder} to obtain a logger instead.
1623 * Note that doing the latter may eagerly initialize the underlying
1624 * logging system.
1625 *
1626 * @apiNote
1627 * This method is intended to be used after the system is fully initialized.
1628 * This method may trigger the immediate loading and initialization
1629 * of the {@link LoggerFinder} service, which may cause issues if the
1630 * Java Runtime is not ready to initialize the concrete service
1631 * implementation yet.
1632 * System classes which may be loaded early in the boot sequence and
1633 * need to log localized messages should create a logger using
1634 * {@link #getLogger(java.lang.String)} and then use the log methods that
1635 * take a resource bundle as parameter.
1636 *
1637 * @param name the name of the logger.
1638 * @param bundle a resource bundle.
1639 * @return an instance of {@link Logger} which will use the provided
1640 * resource bundle for message localization.
1641 * @throws NullPointerException if {@code name} is {@code null} or
1642 * {@code bundle} is {@code null}.
1643 * @throws IllegalCallerException if there is no Java caller frame on the
1644 * stack.
1645 *
1646 * @since 9
1647 */
1648 @CallerSensitive
1649 public static Logger getLogger(String name, ResourceBundle bundle) {
1650 final ResourceBundle rb = Objects.requireNonNull(bundle);
1651 Objects.requireNonNull(name);
1652 final Class<?> caller = Reflection.getCallerClass();
1653 if (caller == null) {
1654 throw new IllegalCallerException("no caller frame");
1655 }
1656 final SecurityManager sm = System.getSecurityManager();
1657 // We don't use LazyLoggers if a resource bundle is specified.
1658 // Bootstrap sensitive classes in the JDK do not use resource bundles
1659 // when logging. This could be revisited later, if it needs to.
1660 if (sm != null) {
1661 final PrivilegedAction<Logger> pa =
1662 () -> LoggerFinder.accessProvider()
1663 .getLocalizedLogger(name, rb, caller.getModule());
1664 return AccessController.doPrivileged(pa, null,
1665 LoggerFinder.LOGGERFINDER_PERMISSION);
1666 }
1667 return LoggerFinder.accessProvider()
1668 .getLocalizedLogger(name, rb, caller.getModule());
1669 }
1670
1671 /**
1672 * Terminates the currently running Java Virtual Machine. The
1673 * argument serves as a status code; by convention, a nonzero status
1674 * code indicates abnormal termination.
1675 * <p>
|