java 在没有 Spring 的情况下使用 AspectJ 日志记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11938487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using AspectJ logging without Spring
提问by Himanshu Yadav
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.
我刚刚在一个旧的应用程序上工作,它的日志很差或没有日志。它没有实现 Spring 框架。
是否可以在没有 Spring 的情况下实现 AspectJ 日志记录功能?
如果是,请给我推荐一些好的教程。
采纳答案by Ganesh Ghag
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
试试这个链接,一个简单的应用程序显示使用加载时间编织而不使用 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.
所需要的只是aspectj 运行时和weaver jar,以及一个包含正确配置的META-INF\aop.xml 文件。
also refer link for details about using aspectj ltw without spring http://www.eclipse.org/aspectj/doc/next/devguide/ltw.html
另请参阅有关在没有 spring 的情况下使用 aspectj ltw 的详细信息的链接 http://www.eclipse.org/aspectj/doc/next/devguide/ltw.html
回答by rsingh25
You can use aspectj without Spring (or log4j) to log messages at any aspectj supported joinpoints,
您可以在没有 Spring(或 log4j)的情况下使用 aspectj 在任何 aspectj 支持的连接点记录消息,
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.
您可以使用适合您的加载时间或编译时间编织。希望这可以帮助。
回答by user2695490
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.
也许这个开源可以帮助你。https://code.google.com/p/perfspy/。它是一个运行时日志记录、性能监控和代码检查工具。它使用 ApsectJ 在运行时围绕您的应用程序代码编织,并记录每个方法的执行时间及其输入参数和值。它有一个 UI 应用程序,您可以在其中以树的形式查看方法调用及其输入和返回值。有了它,您可以发现性能瓶颈并了解复杂的代码流。
回答by Siddharth Sarathe
Try https://devkonline.com/tutorials/content/aspectj-java. It has step by step explanation to use aspectj in java
试试https://devkonline.com/tutorials/content/aspectj-java。在java中使用aspectj有分步说明