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.
-
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() methodimportjava.util.logging.*;publicclassGFG {publicstaticvoidmain(String[] args){// Create a Logger with class name GFGLogger logger = Logger.getAnonymousLogger();// Call info methodlogger.info("Message 1");logger.info("Message 2");}}chevron_rightfilter_noneThe output printed on console is shown below.
Output:

Program 2:
// Java program to demonstrate Exception thrown by// Logger.getAnonymousLogger(java.lang.String) methodimportjava.util.logging.*;publicclassGFG {publicstaticvoidmain(String[] args){String LoggerName =null;// Create a Logger with a null valuetry{Logger logger= Logger.getAnonymousLogger(LoggerName);}catch(NullPointerException e) {System.out.println("Exception Thrown :"+ e);}}}chevron_rightfilter_noneThe output printed on console is shown below.
Output:

-
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) methodimportjava.util.ResourceBundle;importjava.util.logging.*;publicclassGFG {publicstaticvoidmain(String[] args){// Create ResourceBundle using getBundle// myResource is a properties fileResourceBundle bundle= ResourceBundle.getBundle("resourceBundle");// Create a Logger with resourceBundleLogger logger= Logger.getAnonymousLogger(bundle.getBaseBundleName());// Log the infologger.info("Message 1 for logger");logger.info("Message 1 for logger");}}chevron_rightfilter_noneFor the above program, there is a properties file name resourceBundle. we have to add this file alongside the class to execute the program.
References:
- https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html#getAnonymousLogger()
- https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html#getAnonymousLogger(java.lang.String)
Recommended Posts:
- Logger log() Method in Java with Examples
- Logger getLogger() Method in Java with Examples
- Logger fine() method in Java with Examples
- Logger setLevel() method in Java with Examples
- Logger getParent() Method in Java with Examples
- Logger config() method in Java with Examples
- Logger getName() Method in Java with Examples
- Logger setResourceBundle() method in Java with Examples
- Logger getResourceBundle() Method in Java with Examples
- Logger getGlobal() Method in Java with Examples
- Logger removeHandler() method in Java with Examples
- Logger getUseParentHandlers() Method in Java with Examples
- Logger getResourceBundleName() method in Java with Examples
- Logger setFilter() method in Java with Examples
- Logger addHandler() method in Java with Examples
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.



