Chetan Kawade
Chetan Kawade

Reputation: 1

How can I automate logging

So idea is to write a code which would add some predefined code snippets

e.g. I want to

  1. Generate class field com.ck.MyClass.DEBUGGER initialized as org.apache.commons.logging.LogFactory.getLog("DEBUGGER." + MyClass.CNAME);

  2. add these below lines at entry point and exit point of all methods except constructors in a class through java code for all the classes present in my project

    final String methodName = "process()";
    
    if (MyClass.DEBUGGER.isDebugEnabled())  {
        MyClass.DEBUGGER.debug(CommonLoggingConstants.METHOD_ENTRY +   MyClass.CNAME + CommonLoggingConstants.HASH + methodName);
    }
    

What would be the best approach and API to address this requirement?

Upvotes: 0

Views: 258

Answers (2)

Aditya Gupta
Aditya Gupta

Reputation: 653

You could try to use AOP to intercept all the methods in your application and achieve your goal. You can read more about it here.

Upvotes: 1

Bhanu Pasrija
Bhanu Pasrija

Reputation: 137

Yo can use Aspect Oriented Programming in java to do anything before and after of any function call in java.

Follow the follwing link to implement it. You can use without Spring also. http://ganeshghag.blogspot.in/2012/10/demystifying-aop-getting-started-with.html
http://kamalmeet.com/tag/aop-2/

Upvotes: 1

Related Questions