Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Logger entering() method in Java with Examples

  • Last Updated : 28 Mar, 2019

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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  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:




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

    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:




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

    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:




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

    The output printed on log.txt is shown below.

References:




My Personal Notes arrow_drop_up
Recommended Articles
Page :