< prev index next >

src/java.base/share/classes/java/lang/System.java

Print this page




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


< prev index next >