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