审计 Java:系统检测抛出/捕获的异常(aop?)

发布于 2024-10-07 07:03:59 字数 153 浏览 5 评论 0原文

由于受检查的异常,我们在生产中可能会遇到一些问题,因为所有异常都被捕获在正确的位置并正确记录。

我想知道是否有一些开源工具可以帮助审核这些问题。

例如,是否有一些 AOP 工具可以拦截所有抛出的异常并查看它们是否被重新抛出、包装或记录?这将有助于识别不良渔获。

Due to checked exceptions, we can have some problems in production having all exceptions caught in the right place and logged correctly.

I wonder if there is some opensource tool to help with auditing these problems.

For example, is there some AOP tool that would intercept all exceptions thrown and see if they are re-thrown, wrapped or logged? This would help identify the bad catches.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

铁轨上的流浪者 2024-10-14 07:03:59

如果您决定采用 AOP 路线,Spring 框架提供了一个易于使用的 AOP 框架。本质上,与 Spring 的大部分内容一样,您将使用 xml 配置文件和一些 java 代码的组合来定义您正在寻找的 AOP 功能。

就您而言,我相信您会希望定义一个“抛出后建议”,其中您当然可以访问抛出的异常。

就文档而言,一个很好的起点是 Spring 文档中的 AOP 章节:
http://static.springsource.org/spring/docs/2.5 .x/reference/aop.html

哦,我相信所有 Spring 项目也是开源的 =)

If you've decided that you would like to take the AOP route, the Spring Framework provides an easy to use AOP framework. Essentially, like much of Spring, you would use a combination of a xml config file and some java code to define the AOP functionality you are looking for.

In your case, I believe you would be looking to define an 'After Throwing Advice', in which you would of course have access to the exception thrown.

A good place to start in terms of documentation is the AOP Chapter in the Spring docs:
http://static.springsource.org/spring/docs/2.5.x/reference/aop.html

Oh, and I believe all Spring projects are open source as well =)

眉黛浅 2024-10-14 07:03:59

我知道这个问题需要一个开源解决方案。我不知道其中一个,但如果有该选项,那么 DynaTrace完全你想要什么。祝您搜索顺利。

I know the question asks for an open source solution. I don't know of one but if the option is there then DynaTrace does exactly what you want. Good luck on your search.

○闲身 2024-10-14 07:03:59

FindBugs、PMD 和 Checkstyle 等工具可以识别一些常见的异常处理问题。我从未见过专门分析异常处理的工具,如果有人知道我会感兴趣!

There are tools such as FindBugs, PMD and Checkstyle which can identify some common Exception handling issues. I'm never seen a tool that specifically analyses your exception handling, if anyone knows I'll be interested!

满地尘埃落定 2024-10-14 07:03:59

我有这个确切的问题,我尝试自己写一些东西,由于 AOP 嵌套代理和缺乏使用仪器/编织的能力,我放弃了,只是做了一个广泛的查找并替换了

我当时找到的工具之一对于 BMC 软件的 AppSight,但它是高成本是一个问题

I had this exact question, and I attempted to write something myself, and due to AOP nested proxying and lack of ability to use instrumenation / weaving, I gave up and just did a wide find and replace

One of he tools I did find back then for was AppSight by BMC software, but it's high cost was an issue

雨的味道风的声音 2024-10-14 07:03:59

IntelliJ 的 Inspector 可以在您编写代码时检查代码是否存在许多问题:

http://www.jetbrains。 com/idea/documentation/inspections.jsp

但您的问题听起来更多是关于教育而不是技术。您需要让您的团队了解正确的异常处理意味着什么、何时应该完成等等。工具会有所帮助,但首先不要将它们放入代码中更好。

我们在生产系统中使用 Spring 方面来进行日志记录、跟踪、性能计算等。之前、之后和异常建议创造了奇迹 - 它们将代码保留在一处,并在应用位置方面提供了声明性的灵活性。

请注意一点:方面不是免费的。它们会增加您应用它们的每种方法的成本,因此不要只是将它们堆积起来。凡事适度是关键。

IntelliJ's Inspector can check code for many problems as you write it:

http://www.jetbrains.com/idea/documentation/inspections.jsp

But your problem sounds like it's more about education than technology. You need to educate your team on what proper exception handling means, when it should be done, etc. Tools will help, but not putting them into the code is the first place is better.

We use Spring aspects for our production systems to do logging, tracing, performance calculations, etc. Before, after, and exception advice work wonders - they keep the code in one place and give declarative flexibility as to where they are applied.

Just one caution: aspects aren't free. They add cost to each method you apply them to, so don't just pile them on. Moderation in all things is the key.

窗影残 2024-10-14 07:03:59

我还没有考虑过这一点,但如果您不需要检测生产环境中引发的异常,那么一种解决方案是向您的 Java 应用程序附加一个自定义调试器,只要出现异常就可以触发该调试器提出。

这篇法国博客文章讨论了如何做到这一点:
http://blog.xebia。 fr/2011/12/12/legacy-code-gestion-des-exceptions-avec-jpda/

这是代码:

使用调试运行:
Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

连接到 JVM:

public static VirtualMachine connect(String port) throws IOException, IllegalConnectorArgumentsException {
        AttachingConnector connector = null;
        VirtualMachineManager vmManager = Bootstrap.virtualMachineManager();
        for (Connector aconnector : vmManager.allConnectors()) {
            if ("com.sun.jdi.SocketAttach".equals(aconnector.name())) {
  connector = (AttachingConnector) aconnector;
  break;
     }
 }
 Map<String, Connector.Argument> args = connector.defaultArguments();
        Connector.Argument pidArgument = args.get("port");
        pidArgument.setValue(port);
        return connector.attach(args);
 }

创建断点。示例:

public static void createExceptionBreakPoint(VirtualMachine vm) {
        EventRequestManager erm = vm.eventRequestManager();
        List<ReferenceType> referenceTypes = vm.classesByName("java.lang.Throwable");
        for (ReferenceType refType : referenceTypes){
  ExceptionRequest exceptionRequest = erm.createExceptionRequest(refType, true, true);
  exceptionRequest.setEnabled(true);
 }
}

然后处理异常:

public static void handleExceptionEvent(ExceptionEvent exceptionEvent) throws Exception {
        ObjectReference remoteException = exceptionEvent.exception();
        ThreadReference thread = exceptionEvent.thread();
        List<Value> paramList = new ArrayList<Value>(1);
        paramList.add(dumpFileName);
        //crer un printStream dans la JVM cible
        ObjectReference printStreamRef = printStreamClassType.newInstance(thread, printStreamConstructor, paramList,
            ObjectReference.INVOKE_SINGLE_THREADED);
    ReferenceType remoteType = remoteException.referenceType();
    Method printStackTrace = (Method) remoteType.methodsByName("printStackTrace").get(1);
    paramList.clear();
    paramList.add(printStreamRef);
    remoteException.invokeMethod(thread, printStackTrace, paramList, ObjectReference.INVOKE_SINGLE_THREADED);
    Scanner scanner = new Scanner(new File(dumpFileName.value()));
    while (scanner.hasNextLine()){
  System.out.println(scanner.nextLine());
 }
}

有点重,但它有效,现在如何捕获记录的异常和其他异常?

I didn't though about that yet but one solution, if you do not need to detect exceptions thrown on production envirionment, is to attach to your Java application a custom debugger that can be triggered whenever an exception is raised.

This french blog article talk about how to do it:
http://blog.xebia.fr/2011/12/12/legacy-code-gestion-des-exceptions-avec-jpda/

Here is the code:

Run with debug:
Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

Connect to the JVM:

public static VirtualMachine connect(String port) throws IOException, IllegalConnectorArgumentsException {
        AttachingConnector connector = null;
        VirtualMachineManager vmManager = Bootstrap.virtualMachineManager();
        for (Connector aconnector : vmManager.allConnectors()) {
            if ("com.sun.jdi.SocketAttach".equals(aconnector.name())) {
  connector = (AttachingConnector) aconnector;
  break;
     }
 }
 Map<String, Connector.Argument> args = connector.defaultArguments();
        Connector.Argument pidArgument = args.get("port");
        pidArgument.setValue(port);
        return connector.attach(args);
 }

Create your breakpoints. Exemple:

public static void createExceptionBreakPoint(VirtualMachine vm) {
        EventRequestManager erm = vm.eventRequestManager();
        List<ReferenceType> referenceTypes = vm.classesByName("java.lang.Throwable");
        for (ReferenceType refType : referenceTypes){
  ExceptionRequest exceptionRequest = erm.createExceptionRequest(refType, true, true);
  exceptionRequest.setEnabled(true);
 }
}

And then handle the exceptions:

public static void handleExceptionEvent(ExceptionEvent exceptionEvent) throws Exception {
        ObjectReference remoteException = exceptionEvent.exception();
        ThreadReference thread = exceptionEvent.thread();
        List<Value> paramList = new ArrayList<Value>(1);
        paramList.add(dumpFileName);
        //crer un printStream dans la JVM cible
        ObjectReference printStreamRef = printStreamClassType.newInstance(thread, printStreamConstructor, paramList,
            ObjectReference.INVOKE_SINGLE_THREADED);
    ReferenceType remoteType = remoteException.referenceType();
    Method printStackTrace = (Method) remoteType.methodsByName("printStackTrace").get(1);
    paramList.clear();
    paramList.add(printStreamRef);
    remoteException.invokeMethod(thread, printStackTrace, paramList, ObjectReference.INVOKE_SINGLE_THREADED);
    Scanner scanner = new Scanner(new File(dumpFileName.value()));
    while (scanner.hasNextLine()){
  System.out.println(scanner.nextLine());
 }
}

A bit heavy but it works, now how to catch the exceptions that are logged and the others?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文