Home » Core Java » util » logging » Log method call

About Ilias Tsagklis

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

Log method call

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:

  • The call(String arg1, Object arg2) method of the class takes a String argument and an Object argument and returns a boolean value.
  • The method creates a FileHandler to write logs to a specific file.
  • It creates a new Logger instance and adds the handler to the logger, with addHandler(Handler handler) API method.
  • It also adds a logging Level to the logger with setLevel(Level newLevel) API method.
  • The method invokes the entering(String sourceClass, String sourceMethod, Object[] params) API method of the logger, to log an entry to this method. It also invokes the exiting(String sourceClass, String sourceMethod, Object result) API method to log returning from the method. They both take as parameters the class and the method names, so whenever the method is invoked by another method or class, the FileHandler will write to the log file, its invocation entries and return values,

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

<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2011-11-19T15:50:10</date>
  <millis>1321710610361</millis>
  <sequence>0</sequence>
  <logger>com.javacodegeeks.snippets.core</logger>
  <level>FINER</level>
  <class>com.javacodegeeks.snippets.core.LogMethodCall</class>
  <method>method</method>
  <thread>10</thread>
  <message>ENTRY arg1 arg2</message>
</record>
<record>
  <date>2011-11-19T15:50:10</date>
  <millis>1321710610364</millis>
  <sequence>1</sequence>
  <logger>com.javacodegeeks.snippets.core</logger>
  <level>FINER</level>
  <class>com.javacodegeeks.snippets.core.LogMethodCall</class>
  <method>method</method>
  <thread>10</thread>
  <message>RETURN true</message>
</record>
</log>

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area from our partners over at ZipRecruiter

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns