logging

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.

Ilias Tsagklis

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button