Logging a Method Call - Java Java Virtual Machine

Java examples for Java Virtual Machine:Logger

Description

Logging a Method Call

Demo Code

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

public class Main {
  public boolean myMethod(int p1, Object p2) {
    // Log entry//from   ww  w  .  j av a 2 s  . c o  m
    Logger logger = Logger.getLogger("com.mycompany.MyClass");
    if (logger.isLoggable(Level.FINER)) {
      logger.entering(this.getClass().getName(), "myMethod", new Object[] {
          new Integer(p1), p2 });
    }

    // Method body

    // Log exit
    boolean result = true;
    if (logger.isLoggable(Level.FINER)) {
      logger
          .exiting(this.getClass().getName(), "myMethod", new Boolean(result));

      // Use the following if the method does not return a value
      logger.exiting(this.getClass().getName(), "myMethod");
    }
    return result;
  }
}

Related Tutorials