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.

  1. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // entering(String, String) method
      
    import java.io.IOException;
    import java.util.List;
    import java.util.logging.*;
      
    public class GFG {
      
        public static void main(String[] args)
            throws SecurityException, IOException
        {
      
            // Create a Logger
            Logger logger
                = Logger.getLogger(
                    GFG.class.getName());
      
            // Create a file handler object
            FileHandler handler
                = new FileHandler("logs.txt");
            handler.setFormatter(new SimpleFormatter());
      
            // Add file handler as
            // handler of logs
            logger.addHandler(handler);
      
            // set Logger level()
            logger.setLevel(Level.FINER);
      
            // call entering methods with class
            // name =  GFG and method name = main
            logger.entering(GFG.class.getName(), "main");
      
            // calling again for List class toString()
            logger.entering(List.class.getName(), "toString()");
        }
    }

    chevron_right

    
    

    The output printed on log.txg file is shown below.
    Output:

  2. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // entering(String, String, Object) method
      
    import java.io.IOException;
    import java.util.logging.*;
      
    public class GFG {
      
        public static void main(String[] args)
            throws SecurityException, IOException
        {
      
            // Create a Logger
            Logger logger
                = Logger.getLogger(
                    GFG.class.getName());
      
            // Create a file handler object
            FileHandler handler
                = new FileHandler("logs.txt");
            handler.setFormatter(new SimpleFormatter());
      
            // Add file handler as
            // handler of logs
            logger.addHandler(handler);
      
            // set Logger level()
            logger.setLevel(Level.FINER);
      
            // call entering method with class
            // name =  GFG and method name = main
            logger.entering(
                GFG.class.getName(), "main",
                new String("Java is Platform Independent"));
        }
    }

    chevron_right

    
    

    The output printed on log.txt is shown below.

  3. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // entering(String, String, Object[])  method
      
    import java.io.IOException;
    import java.util.logging.*;
      
    public class GFG {
      
        public static void main(String[] args)
            throws SecurityException, IOException
        {
      
            // Create a Logger
            Logger logger
                = Logger.getLogger(
                    GFG.class.getName());
      
            // Create a file handler object
            FileHandler handler
                = new FileHandler("logs.txt");
            handler.setFormatter(new SimpleFormatter());
      
            // Add file handler as
            // handler of logs
            logger.addHandler(handler);
      
            // set Logger level()
            logger.setLevel(Level.FINER);
      
            // create a array of String object
            String[] methods = {
                "main", "ADD", "get", "set"
            };
      
            // call entering method with class
            // name =  GFG and method name = main
            logger.entering(GFG.class.getName(), "main",
                            methods);
        }
    }

    chevron_right

    
    

    The output printed on log.txt is shown below.

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.