jeudi 20 août 2015

Java- How to automatically call a method before and after each method call

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.





Aucun commentaire:

Enregistrer un commentaire