GWT 服务异常日志记录的最佳实践 [英] Best Practices for GWT services exceptions logging

查看:30
本文介绍了GWT 服务异常日志记录的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定将日志系统添加到我的 gwt 服务层.首先,我想记录从该层抛出的所有异常.我有一个类似于 Spring 的 ServletDispatcher 的对象,它调用其他服务.我以为我可以在那里添加日志记录,但我意识到 GWT 服务在 ServletResponse 中包装了已检查的异常,而未检查到 UnexpectedException.

I decided to add logging system to my gwt service layer. First of all I wanted to log all exceptions that are thrown from that layer. I had an object similar to Spring's ServletDispatcher, which calls other services. I thought I could add logging there, but I realized that GWT services wraps checked exceptions in ServletResponse and unchecked into UnexpectedException.

有人可以分享他在这个问题上的经验吗?记录所有 GWT 服务的已检查和未检查异常的最佳方法是什么.

Can anybody share his experience with that problem? What is the best way to log checked and unchecked exceptions for all GWT Services.

我找到了建议扩展 RemoteServiceServlet 并覆盖默认异常流的解决方案.但我觉得这个解决方案太耗时了.有人知道更简单的变体吗?

I found solution which suggests to extend RemoteServiceServlet and override default exceptions flow. But I find this solution too time-consuming. Does anybode know any easier variant?

推荐答案

在服务器端,我们有一个用于所有服务实现的 RemoteServiceServlet 子类.您提到这似乎很耗时,但代码如下所示.你做一次就大功告成了.

On the server side, we have a subclass of RemoteServiceServlet that we use for all service implementations. You mention that it seems time-consuming, but here's what the code looks like. You do it once and you're done.

@Override
protected void doUnexpectedFailure(Throwable t) {
    t.printStackTrace(System.err);
    super.doUnexpectedFailure(t);
}

注意:我们实际上并未将其发送到 System.err,您可能也不应该这样做,但您明白了.

Note: We don't actually send it to System.err and you probably shouldn't either, but you get the idea.

在客户端,我们使用名为 AsyncSuccessCallback 的 AsyncCallback 子类.对于我们的大多数 RPC 调用,它统一处理 onFailure 情况.我们的大部分回调代码都可以处理 onSuccess 情况,因为知道 onFailure 已被处理.它还提供了一个地方可以在以后更改此实现.

On the client side, we use a subclass of AsyncCallback called AsyncSuccessCallback. It handles the onFailure case uniformly for most of our RPC calls. Most of our callback code can deal with the onSuccess case knowing that onFailure is handled. It also provides a single place to change this implementation later.

public abstract class AsyncSuccessCallback<T> implements AsyncCallback<T> {

    public void onFailure(Throwable t) {
        handleException(t);
    }

    protected void handleException(Throwable t) {
         Window.alert(t.getMessage());
    }

}

注意:我们实际上并没有使用 Window.alert,但您还是明白了.在这种情况下,我们所做的是显示一个 GWT 对话框,该对话框显示一个表单,该表单向接受错误报告的不同服务器执行 POST.该表单允许用户输入错误发生时他们正在做什么的描述.

Note: We don't actually use Window.alert, but again, you get the idea. What we do in this case is display a GWT DialogBox that displays a form that does a POST to a different server that accepts error reports. The form allows the user to type a description of what they were doing when the error occurred.

在客户端,如果要获取堆栈跟踪,则需要编写一些额外的代码:

On the client side, if you want to get the stack trace, you need to write a little extra code:

// for lineEnding, use "<br>" for HTML, "
" for text
public static final String getStackTrace(Throwable t, String lineEnding) {
    Object[] stackTrace = t.getStackTrace();
    if (stackTrace != null) {
        StringBuilder output = new StringBuilder();
        for (Object line : stackTrace) {
            output.append(line);
            output.append(lineEnding);
        }
        return output.toString();
    } else {
        return "[stack unavailable]";
    }
}

这篇关于GWT 服务异常日志记录的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆