Log method call (original) (raw)

In this example we shall show you how to log a method call. We have implemented the LogMethodCall Class, with a simple method to log its messages. The basic steps of the example are described below:

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger;

public class LogMethodCall {

public static void main(String[] args) throws Exception {
    LogMethodCall call = new LogMethodCall();
    call.method("arg1", new String("arg2"));
}

public boolean method(String arg1, Object arg2) throws Exception {


    boolean append = false;
    FileHandler handler = new FileHandler("default.log", append);

    Logger logger = Logger.getLogger("com.javacodegeeks.snippets.core");
    logger.setLevel(Level.FINEST);
    logger.addHandler(handler);
    

logger.entering(this.getClass().getName(), "method", new Object[]{arg1, arg2});

boolean result = true;

logger.exiting(this.getClass().getName(), "method", new Boolean(result));

return result;

 }

}

default.log

2011-11-19T15:50:10 1321710610361 0 com.javacodegeeks.snippets.core FINER com.javacodegeeks.snippets.core.LogMethodCall method 10 ENTRY arg1 arg2 2011-11-19T15:50:10 1321710610364 1 com.javacodegeeks.snippets.core FINER com.javacodegeeks.snippets.core.LogMethodCall method 10 RETURN true

This was an example of how to log a method call in Java.

Photo of Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Back to top button