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.

- 13,315
- 46
- 162
- 291
-
7Yes you can use it without Spring. [Andrew](http://stackoverflow.com/users/278836/andrew-white) has a good [blog](http://www.andrewewhite.net/wordpress/2010/03/17/aspectj-annotation-tutorial/) on this. – CoolBeans Aug 13 '12 at 16:37
-
Thanks. How can it work with Log4j? – Himanshu Yadav Aug 13 '12 at 16:42
-
Not sure what you mean. You can replace the Sys outs with log4j. – CoolBeans Aug 13 '12 at 17:01
-
I am referring to my another question http://stackoverflow.com/questions/11938093/aspectj-logs-with-log4j – Himanshu Yadav Aug 13 '12 at 17:02
4 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

- 169
- 1
- 8
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.

- 356
- 3
- 9
Maybe this OpenSource can help you. https://code.google.com/p/perfspy/. It is a runtime logging, performance monitoring and code inspecting tool. It uses ApsectJ to weave around your application code at runtime, and logs the execution time of every method and its input parameters and values. It has a UI application, in which you can view the method invocations and their input and return values as trees. With it, you can spot performance bottlenecks and understand complex code flow.

- 77
- 2
Try https://devkonline.com/tutorials/content/aspectj-java . It has step by step explanation to use aspectj in java

- 40
- 1