This Java tip illustrates an example of logging a method call. Method calls are logged by using functions entering() and existing() of the logger class.

 package com.library;
    
    class Books{

        public boolean myMethod(int p1, Object p2) {
            // Log object entry
            Logger logger = Logger.getLogger("com.library.Books");
            if (logger.isLoggable(Level.FINER)) {
                logger.entering(this.getClass().getName(), "Method",
                                new Object[]{new Integer(p1), p2});
            }
    
            // Method body
    
            // Log exiting
            boolean result = true;

            if (logger.isLoggable(Level.FINER)) {
                logger.exiting(this.getClass().getName(), "Method", new Boolean(result));
    
                //If the method does not return a value
                logger.exiting(this.getClass().getName(), "Method");
            }

            return result;
         }
    }

Here is a sample of the output using a simple formatter and the following call:

 new com.library.Books().Method(123, "hello");
    
    Jan 10, 2002 7:59:48 PM com.library.Books Method
    FINER: ENTRY 123 hello
    Jan 10, 2002 7:59:49 PM com.library.Books Method
    FINER: RETURN true
    Jan 10, 2002 7:59:49 PM com.library.Books Method
    FINER: RETURN