本文介绍了使用log4j与Web容器(J2EE服务器的一部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们的应用程序抛出我们抓到的错误时,我们将消息和stacktrace放在专为我们的应用程序(myapp.log)创建的日志文件中。例如:

When our application throws errors that we catch, we put the message and stacktrace in a log file created specifically for our application (myapp.log). For example:

public class SomeClass {
    OurLogger log = OurLogger.getLogger ("myapp", SomeClass.class);
    public void someMethod {
        try {
        //code
        }
        catch (DataAccessException e)
        {
            log.error(e.getMessage(), e);
        }
    }
}

我们这样做是因为我们在多个应用程序驻留在应用程序服务器上的环境中...我们和其他应用程序日志应与 server.log 分开。

We do this because since we are in a environment where multiple apps reside on an application server...ours and every other applications logs should be seperate from the server.log.

但是,对于某些我们没有捕获错误的情况,堆栈跟踪正在 server.log 中写入。在这些情况下我们想将堆栈错误发送到 myapp.log 。我知道我们可以在web.xml中定义一个异常并转发到jsp页面,但是有一种方法,在这种情况下,不要将堆栈跟踪发送到 server.log 而是转到 myapp.log ?当然,除了通过代码更改捕获异常。

However, for some cases where we are not catching the error...the stack trace is being written on server.log In these cases also we would like to send stack errors to myapp.log. I know we can define an exception in web.xml and forward to a jsp page but is there a way, in this case, to not send the stack trace to server.log but instead to myapp.log? Other than catching the exception by code change of course.

推荐答案

对我来说,这是一个正常的行为。一些例外,例如 RuntimeException ,被应用程序服务器捕获并登录到应用程序服务器日志文件(这是全局到具有GlassFish的域)。而且您不会看到它们全部抓住,您希望容器在这种情况下执行其工作,例如回滚一个交易,否则你会遇到讨厌的错误。

To me, this is a normal behavior. Some exceptions, for example RuntimeException, are caught by the application server and logged in the app server log file (which is global to a domain with GlassFish). And you don't watch to catch them all, you want the container to do its job in such situation, for example rollbacking a transaction, or you'll get nasty bugs.

在这些情况下,我们也希望将堆栈错误发送到myapp.log。我知道我们可以在web.xml中定义一个异常并转发到一个jsp页面,但是在这种情况下,是不是将堆栈跟踪发送到server.log,而是发送到myapp.log?

据我所知,这是不可能的。即使您添加了一个< error-page> ,它仍然是应用程序服务器,将捕获异常,因此这将无法解决您的问题。即使您使用servlet过滤器捕获 Throwable (我不会这样做),并在应用程序级别登录,您必须重新启动它才能让容器做我上面提到的工作。所以不会完全解决你的问题。

To my knowledge, this is not possible. And even if you add an <error-page>, it's still the app server that will catch the exception so this won't solve your "issue". And even if you use a servlet filter to catch Throwable (I wouldn't do that) and log it at the application level, you'll have to rethrow it to let the container do its job as I mentioned above. So it won't solve your "issue" entirely neither.

不要这样做,你做想要抓住他们!

Don't do that, you do not want to catch them all!

这篇关于使用log4j与Web容器(J2EE服务器的一部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:43