Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AspectJ logging without Spring [closed]

I have just working on an old application which has poor log or no logs. It does not implement Spring framework.
Is it possible to implement AspectJ logging functionality without Spring?
If yes please suggest me some good tutorials.

like image 314
Himanshu Yadav Avatar asked Aug 13 '12 16:08

Himanshu Yadav


2 Answers

try this link for a simple application showing use of Load time weaving without using Spring http://ganeshghag.blogspot.in/2012/10/demystifying-aop-getting-started-with.html

all that would be needed is aspectj runtime and weaver jars, and a META-INF\aop.xml file containing proper config.

also refer link for details about using aspectj ltw without spring http://www.eclipse.org/aspectj/doc/next/devguide/ltw.html

like image 130
Ganesh Ghag Avatar answered Nov 11 '22 14:11

Ganesh Ghag


You can use aspectj without Spring (or log4j) to log messages at any aspectj supported joinpoints,

For example,

public aspect ListAllMethodExecution {
    private int callDepth; 

    private pointcut executionJoinPoints(): !within(ListAllMethodExecution) && execution (* *.*(..));

    before(): executionJoinPoints(){
        print("Before call " + thisJoinPoint);
        callDepth++;
    }

    after(): executionJoinPoints(){
        callDepth--;
        print("After call " + thisJoinPoint);
    }

    private void print(String s){
        for(int i=0; i<callDepth; i++)
            System.out.print("    ");
        System.out.println(s);
    }
}

You can modify the pointcut expression to log from a specific packages on specific events or other static joinpoints that you may be interested in. Also you can modify the print method as you wish to redirect logs.

You can use load time or compile time weaving as suits to you. Hope this helps.

like image 33
rsingh25 Avatar answered Nov 11 '22 15:11

rsingh25