Open In App

Logger getAnonymousLogger() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




    // 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");
        }
    }

    
    

    The output printed on console is shown below.
    Output:

    Program 2:




    // 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);
            }
        }
    }

    
    

    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:




    // 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");
        }
    }

    
    

    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:



Last Updated : 24 Apr, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads