Logger getAnonymousLogger() Method in Java with Examples

The getAnonymousLogger() method of a Logger class used to create an anonymous Logger.

There are two types of getAnonymousLogger() method depending upon no of the parameter passed.

  1. getAnonymousLogger(): This method is used to create an anonymous Logger. This newly created anonymous Logger is not registered in the LogManager namespace and no access checks on updates to the logger. So the question is why we need this logger if there are no access checks because the factory method is primarily intended for use from applets. Because the resulting Logger is anonymous it can be kept private by the creating class. This removes the need for normal security checks, which in turn allows untrusted applet code to update the control state of the Logger. For example, an applet can do a setLevel or an addHandler on an anonymous Logger.

    This logger is configured to have the root logger (“”) as its parent. It inherits its effective level and handlers from the root logger. Changing its parent via the setParent method will still require the security permission specified by that method.



    Syntax:

    public static Logger getAnonymousLogger()
    

    Parameters: This method accepts nothing

    Return value: This method returns a newly created private Logger.

    Below programs illustrate getAnonymousLogger(java.lang.String) method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // Logger.getAnonymousLogger() method
      
    import java.util.logging.*;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Create a Logger with class name GFG
            Logger logger = Logger.getAnonymousLogger();
      
            // Call info method
            logger.info("Message 1");
            logger.info("Message 2");
        }
    }

    chevron_right

    
    

    The output printed on console is shown below.
    Output:

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate Exception thrown by
    // Logger.getAnonymousLogger(java.lang.String) method
      
    import java.util.logging.*;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            String LoggerName = null;
      
            // Create a Logger with a null value
            try {
                Logger logger
                    = Logger
                          .getAnonymousLogger(LoggerName);
            }
            catch (NullPointerException e) {
                System.out.println("Exception Thrown :" + e);
            }
        }
    }

    chevron_right

    
    

    The output printed on console is shown below.
    Output:

  2. getAnonymousLogger(String resourceBundleName): This method is used to create an anonymous Logger. This newly created anonymous Logger is not registered in the LogManager namespace and no access checks on updates to the logger. This Logger has ResourceBundle passed as a parameter to be used for localizing messages for this logger.

    Syntax:

    public static Logger getAnonymousLogger(String resourceBundleName)
    

    Parameters: This method accepts single Parameter resourceBundleName which is the name of ResourceBundle to be used for localizing messages for this logger.


    Return value: This method returns a suitable Logger.

    Exception: This method will throw MissingResourceException: if the resourceBundleName is non-null and no corresponding resource can be found.

    Below programs illustrate getAnonymousLogger(String resourceBundleName) method:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // getAnonymousLogger(String resourceBundleName) method
      
    import java.util.ResourceBundle;
    import java.util.logging.*;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Create ResourceBundle using getBundle
            // myResource is a properties file
            ResourceBundle bundle
                = ResourceBundle
                      .getBundle("resourceBundle");
      
            // Create a Logger with resourceBundle
            Logger logger
                = Logger
                      .getAnonymousLogger(
                          bundle.getBaseBundleName());
      
            // Log the info
            logger.info("Message 1 for logger");
            logger.info("Message 1 for logger");
        }
    }

    chevron_right

    
    

    For the above program, there is a properties file name resourceBundle. we have to add this file alongside the class to execute the program.

References:



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.