< prev index next >

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

Print this page

        

@@ -1577,17 +1577,31 @@
      *
      * @param name the name of the logger.
      * @return an instance of {@link Logger} that can be used by the calling
      *         class.
      * @throws NullPointerException if {@code name} is {@code null}.
+     * @throws IllegalCallerException if there is no {@linkplain
+     *         StackWalker#getCallerClass() caller} frame, i.e.
+     *         when this {@code getLogger} method is called from JNI
+     *         and there is no Java frame on the stack.<br>
+     *         Note: To obtain a logger in such a context, then the caller
+     *         should either use an auxiliary class that will implicitly
+     *         be identified as the caller, or access the system {@link
+     *         LoggerFinder#getLoggerFinder() LoggerFinder} instance and
+     *         obtain a logger from it. Note however that doing the latter
+     *         may force the eager initialization of the underlying logging
+     *         system.
      *
      * @since 9
      */
     @CallerSensitive
     public static Logger getLogger(String name) {
         Objects.requireNonNull(name);
         final Class<?> caller = Reflection.getCallerClass();
+        if (caller == null) {
+            throw new IllegalCallerException("no caller frame");
+        }
         return LazyLoggers.getLogger(name, caller.getModule());
     }
 
     /**
      * Returns a localizable instance of {@link Logger

@@ -1597,11 +1611,11 @@
      *
      * @implSpec
      * The returned logger will perform message localization as specified
      * by {@link LoggerFinder#getLocalizedLogger(java.lang.String,
      * java.util.ResourceBundle, java.lang.reflect.Module)
-     * LoggerFinder.getLocalizedLogger(name, bundle, module}, where
+     * LoggerFinder.getLocalizedLogger(name, bundle, module)}, where
      * {@code module} is the caller's module.
      *
      * @apiNote
      * This method is intended to be used after the system is fully initialized.
      * This method may trigger the immediate loading and initialization

@@ -1617,18 +1631,32 @@
      * @param bundle  a resource bundle.
      * @return an instance of {@link Logger} which will use the provided
      * resource bundle for message localization.
      * @throws NullPointerException if {@code name} is {@code null} or
      *         {@code bundle} is {@code null}.
+     * @throws IllegalCallerException if there is no {@linkplain
+     *         StackWalker#getCallerClass() caller} frame, i.e.
+     *         when this {@code getLogger} method is called from JNI
+     *         and there is no Java frame on the stack.<br>
+     *         Note: To obtain a logger in such a context, then the caller
+     *         should either use an auxiliary class that will implicitly
+     *         be identified as the caller, or access the system {@link
+     *         LoggerFinder#getLoggerFinder() LoggerFinder} instance and
+     *         obtain a logger from it. Note however that doing the latter
+     *         may force the eager initialization of the underlying logging
+     *         system.
      *
      * @since 9
      */
     @CallerSensitive
     public static Logger getLogger(String name, ResourceBundle bundle) {
         final ResourceBundle rb = Objects.requireNonNull(bundle);
         Objects.requireNonNull(name);
         final Class<?> caller = Reflection.getCallerClass();
+        if (caller == null) {
+            throw new IllegalCallerException("no caller frame");
+        }
         final SecurityManager sm = System.getSecurityManager();
         // We don't use LazyLoggers if a resource bundle is specified.
         // Bootstrap sensitive classes in the JDK do not use resource bundles
         // when logging. This could be revisited later, if it needs to.
         if (sm != null) {
< prev index next >