Logger entering() method in Java with Examples
The entering() method of a Logger class used to Log a method entry.
There are three types of entering() method depending upon the parameters passed.
-
entering(String sourceClass, String sourceMethod): This method is used to Log a method entry.Actually many times in application development we need to log when we enter in a method of class so this is a convenience method that can be used to log entry to a method. This method logs with message “ENTRY”, log level FINER, and the given sourceMethod and sourceClass are also logged.
Syntax:
public void entering(String sourceClass,
String sourceMethod)
Parameters: This method accepts two parameters:
- sourceClass is the name of the class that issued the logging request and
- sourceMethod is the name of the method that is being entered.
Return value: This method returns nothing.
Below program illustrate entering(String sourceClass, String sourceMethod) method:
Program 1:
import java.io.IOException;
import java.util.List;
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
throws SecurityException, IOException
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
FileHandler handler
= new FileHandler( "logs.txt" );
handler.setFormatter( new SimpleFormatter());
logger.addHandler(handler);
logger.setLevel(Level.FINER);
logger.entering(GFG. class .getName(), "main" );
logger.entering(List. class .getName(), "toString()" );
}
}
|
The output printed on log.txg file is shown below.
Output:
-
entering(String sourceClass, String sourceMethod, Object param1): This method is used to Log a method entry, with one parameter where parameter passed is an object we want to log. Actually many times in application development we need to log when we enter in a method of class so this is a convenience method that can be used to log entry to a method. This method logs with message “ENTRY {0}”, log level FINER, and the given sourceMethod, sourceClass, and parameter are logged.
Syntax:
public void entering(String sourceClass,
String sourceMethod,
Object param1)
Parameters: This method accepts three parameters:
- sourceClass is the name of the class that issued the logging request and
- sourceMethod is the name of the method that is being entered.
- param1: is the parameter to the method being entered.
Return value: This method returns nothing.
Below programs illustrate entering(String sourceClass, String sourceMethod, Object param1) method:
Program 1:
import java.io.IOException;
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
throws SecurityException, IOException
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
FileHandler handler
= new FileHandler( "logs.txt" );
handler.setFormatter( new SimpleFormatter());
logger.addHandler(handler);
logger.setLevel(Level.FINER);
logger.entering(
GFG. class .getName(), "main" ,
new String( "Java is Platform Independent" ));
}
}
|
The output printed on log.txt is shown below.
-
entering(String sourceClass, String sourceMethod, Object[] params): This method is used to Log a method entry, with an array of parameters. Actually many times in application development we need to log when we enter in a method of class so this is a convenience method that can be used to log entry to a method. This method logs with message “ENTRY” (followed by a format {N} indicator for each entry in the parameter array), log level FINER, and the given sourceMethod, sourceClass, and parameter are logged.
Syntax:
public void entering(String sourceClass,
String sourceMethod,
Object[] params)
Parameters: This method accepts three parameters:
- sourceClass is the name of the class that issued the logging request and
- sourceMethod is the name of the method that is being entered.
- params: is an array of parameters to the method being entered.
Return value: This method returns nothing.
Below programs illustrate entering(String sourceClass, String sourceMethod, Object[] params) method:
Program 1:
import java.io.IOException;
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
throws SecurityException, IOException
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
FileHandler handler
= new FileHandler( "logs.txt" );
handler.setFormatter( new SimpleFormatter());
logger.addHandler(handler);
logger.setLevel(Level.FINER);
String[] methods = {
"main" , "ADD" , "get" , "set"
};
logger.entering(GFG. class .getName(), "main" ,
methods);
}
}
|
The output printed on log.txt is shown below.
References:
Last Updated :
28 Mar, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...