Tuesday, June 16, 2026
HomeLanguagesJavaLogger exiting() method in Java with Examples

Logger exiting() method in Java with Examples

The exiting() method of a Logger class used to Log a method return.

There are two types of exiting() method depending upon the parameters passed.

  1. exiting(String sourceClass, String sourceMethod): This method is used to Log a method return. we need to log what method returns and this is a convenience method that can be used to log returning from a method. This method logs with the message “RETURN”, log level FINER, and the given sourceMethod and sourceClass are also logged.

    Syntax:

    public void exiting(String sourceClass,
                        String sourceMethod)
    

    Parameters: This method accepts two parameters:

    • sourceClass is the name of the class that issued the logging request,
    • sourceMethod is the name of the method

    Return value: This method returns nothing.

    Below program illustrate exiting(String sourceClass, String sourceMethod) method:
    Program 1:




    // Java program to demonstrate
    // exiting(String, String)  method
      
    import java.io.IOException;
    import java.util.logging.FileHandler;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.logging.SimpleFormatter;
      
    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 exiting methods with class
            // name =  GFG and method name = main
            logger.exiting(GFG.class.getName(),
                           GFG.class.getMethods()[0].getName());
        }
    }

    
    

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

  2. exiting(String sourceClass, String sourceMethod, Object result): This method is used to Log a method entry, with result object. This is a very helpful method to log entry related to a method of a class with its return value. This method logs with the message “RETURN {0}”, log level FINER, and the gives sourceMethod, sourceClass, and result object is logged.

    Syntax:

    public void exiting(String sourceClass,
                        String sourceMethod,
                        Object result)
    

    Parameters: This method accepts three parameters:

    • sourceClass is the name of the class that issued the logging request,
    • sourceMethod is the name of the method and
    • Object that is being returned.

    Return value: This method returns nothing.

    Below programs illustrate exiting(String sourceClass, String sourceMethod, Object result) method:
    Program 1:




    // Java program to demonstrate
    // exiting(String, String, Object)  method
      
    import java.io.IOException;
    import java.util.logging.FileHandler;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.logging.SimpleFormatter;
      
    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);
      
            // set Logger level()
            logger.setLevel(Level.FINER);
      
            // call exiting method with class
            // name =  GFG and method name = main
            logger.exiting(GFG.class.getName(),
                           GFG.class.getMethods()[0].getName(),
                           new String("Java is Platform Independent"));
        }
    }

    
    

    The output printed on log.txt is shown below.
    Output:

References:

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS