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:
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
{
Logger logger = Logger.getAnonymousLogger();
logger.info( "Message 1" );
logger.info( "Message 2" );
}
}
|
The output printed on console is shown below.
Output:
Program 2:
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
{
String LoggerName = null ;
try {
Logger logger
= Logger
.getAnonymousLogger(LoggerName);
}
catch (NullPointerException e) {
System.out.println( "Exception Thrown :" + e);
}
}
}
|
The 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:
import java.util.ResourceBundle;
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
{
ResourceBundle bundle
= ResourceBundle
.getBundle( "resourceBundle" );
Logger logger
= Logger
.getAnonymousLogger(
bundle.getBaseBundleName());
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
Share your thoughts in the comments
Please Login to comment...