Using AspectJ logging without Spring

心不动则不痛 提交于 2019-11-30 19:51:32

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

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.

Try load time weaving with spring.

Load-time weaving (LTW) refers to the process of weaving AspectJ aspects into an application's class files as they are being loaded into the Java virtual machine (JVM).

That means that you can add an logging aspect to classes and by consecuence to objects outside the Spring IoC container.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!