2

I have a class that have some methods for which I would like to add logs of the method name before and after each method.

private void log(String msg){
    logger.info("=================================");
    logger.info(msg);
    logger.info("=================================");
}

private void method1(){
    log("Here I want to print method name" + " :start");
    //do stuff
    log("Here I want to print method name" + " :end");
}

private void method2(){
    log("Here I want to print method name" + " :start");
    //do stuff
    log("Here I want to print method name" + " :end");
}

Ideally, I want every method to automatically log before and after without really having to call log each time for each method.

Is there a way to achieve this? I know annotation may be one option but I could not think I am the first one encountering this problem.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
as3rdaccount
  • 3,711
  • 12
  • 42
  • 62
  • 6
    You may look at [aspect-oriented programming](https://en.wikipedia.org/wiki/Aspect-oriented_programming). Back in college, I had a class on [AspectJ](https://en.wikipedia.org/wiki/AspectJ), which allows things like the one you're asking here. – watery Aug 21 '15 at 01:33
  • possible duplicate of [Run a method before and after a called method in Java](http://stackoverflow.com/questions/9596991/run-a-method-before-and-after-a-called-method-in-java) – Swapnil Aug 21 '15 at 01:39
  • One possibility (like @watery mentioned) is to use aspect-oriented programming. In this case, since you want to do something before and after a method call, you could use [`@Around`](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-ataspectj-around-advice) advice. I would recommend on reading the [Spring AOP](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html) documentation (as I think it is easier to understand than the regular AspectJ documentation) to get a good understanding of the concepts. – mkobit Aug 21 '15 at 01:40
  • You can try btrace: My question has some example lines: http://stackoverflow.com/questions/4142845/how-can-write-logs-to-a-file-in-btrace – Jayan Aug 21 '15 at 02:52

1 Answers1

1

There are a number of different ways to achieve it.

First of all, you can use a Decorator Design Pattern. It allows you to add some additional behavior to object. Here is short example:

public interface Car {
  public String drive();
}

public class SomeCar implements Car {
  @Override
  public String drive() {
    return "Drive some car";
  }
}

public abstract class CarDecorator implements Car {
  Car car;

  public CarDecorator(Car newPizza) {
    this.car = newPizza;
  }

  @Override
  public String drive() {
    //do somthing before

    String result = car.drive();

    //do someething after

    return result;
  }
}

You can use it like this, or make all instances via some Factory of Car Objects:

Car car = new CarDecorator(new SomeCar());
car.drive();

Furthermore, you can use aspect-oriented programming (AOP) to achieve your goal. For example, with AspectJ or with if you use a Spring Framework - Spring AOP.

One more solution is to use Java EE Interceptors, if it acceptable in your project.

Stanislav
  • 27,441
  • 9
  • 87
  • 82