在基于JSF的应用程序中捕获并记录/通知未处理的异常 [英] Capture and log / notify unhandled exceptions in a JSF based application

查看:206
本文介绍了在基于JSF的应用程序中捕获并记录/通知未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用log4j检查并记录我的JSF Web应用程序中的所有未处理的异常。我阅读这篇文章使用log4j在Java中记录运行时异常并添加实现 Thread.UncaughtExceptionHandler 的类。但是该方法不会被触发。



正确的方法是什么?

解决方案

在Java EE Web应用程序中通常没有任何未被捕获的异常,因为服务器最终会捕获来自Web的异常应用程序并在HTTP 500错误页面中同步HTTP请求(在异步(ajax)请求的情况下)显示它,这反过来依赖于框架)。而且,如果真的是一个未被捕获的异常,这样就逃避了服务器的注意,那么它会杀死服务器的运行时线程,导致整个服务器停止。这显然不是一个理想的现实世界场景。



JSF为您提供 ExceptionHandler API来获取这些异常的手,如果需要的话可以控制它们。 p>

这是一个开始示例:

  public class YourExceptionHandler extends ExceptionHandlerWrapper {

私有的ExceptionHandler包装;

public YourExceptionHandler(ExceptionHandler wrapped){
this.wrapped = wrapped;
}

@Override
public void handle()throws FacesException {
FacesContext facesContext = FacesContext.getCurrentInstance();

for(Iterator< ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents()。iterator(); iter.hasNext();){
可抛出异常= iter.next()getContext() (); //那里是!

//现在用它做你的事情根据问题,您要使用log4j进行登录。
logger.error(发生异常!,异常);
}

getWrapped()。handle();
}

@Override
public ExceptionHandler getWrapped(){
return wrapped;
}

}

为了让它运行,创建自定义 ExceptionHandlerFactory 如下:

  public class YourExceptionHandlerFactory extends ExceptionHandlerFactory {

private ExceptionHandlerFactory父;

public YourExceptionHandlerFactory(ExceptionHandlerFactory parent){
this.parent = parent;
}

@Override
public ExceptionHandler getExceptionHandler(){
return new YourExceptionHandler(parent.getExceptionHandler());
}

}

哪些需要在 faces-config.xml 如下:

 < factory> 
< exception-handler-factory> com.example.YourExceptionHandlerFactory< / exception-handler-factory>
< / factory>



另请参见:




I would like to check and log all unhandled exceptions in my JSF web application using log4j. I read this post Log runtime Exceptions in Java using log4j and add a class that implements Thread.UncaughtExceptionHandler. but the method is not fired.

What is the correct way to achieve this?

解决方案

There's usually no means of an uncaught exception in a Java EE web application as the server ultimately catches any exception coming from the web application and displays it in case of synchronous HTTP requests in a HTTP 500 error page (in case of asynchronous (ajax) requests, this is in turn framework-dependent). Moreover, if there was really an uncaught exception which escaped the server's attention, it would have killed the server's runtime thread causing the whole server to stop. This is clearly not an ideal real world scenario.

JSF offers you the ExceptionHandler API to get hand of those exceptions and if necessary control them.

Here's a kickoff example:

public class YourExceptionHandler extends ExceptionHandlerWrapper {

    private ExceptionHandler wrapped;

    public YourExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public void handle() throws FacesException {
        FacesContext facesContext = FacesContext.getCurrentInstance();

        for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext();) {
            Throwable exception = iter.next().getContext().getException(); // There it is!

            // Now do your thing with it. As per the question, you want to log it using log4j. 
            logger.error("An exception occurred!", exception);
        }

        getWrapped().handle();
    }

    @Override
    public ExceptionHandler getWrapped() {
        return wrapped;
    }

}

In order to get it to run, create a custom ExceptionHandlerFactory as follows:

public class YourExceptionHandlerFactory extends ExceptionHandlerFactory {

    private ExceptionHandlerFactory parent;

    public YourExceptionHandlerFactory(ExceptionHandlerFactory parent) {
        this.parent = parent;
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        return new YourExceptionHandler(parent.getExceptionHandler());
    }

}

Which needs to be registered in faces-config.xml as follows:

<factory>
    <exception-handler-factory>com.example.YourExceptionHandlerFactory</exception-handler-factory>
</factory>

See also:

这篇关于在基于JSF的应用程序中捕获并记录/通知未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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