JSP 中的 Apache Commons 日志记录

发布于 2024-12-29 02:03:20 字数 1366 浏览 0 评论 0原文

我已经阅读了许多关于此的不同文章/线程,但尚未确定在 JSP 中执行 Apache Commons 日志记录的最佳方法。

为了提供一些上下文,我向最初使用 Spring 2 开发的应用程序添加了改进的错误逻辑。因此,现在我们拥有使用 Spring 2 和 Spring 3 创建的控制器。

因此,与其在控制器级别使用 Spring MVC 3 的异常处理,我认为最好在 web.xml 中配置一个 JSP,记录所有控制器的所有传播异常。

令人惊讶的是,我没有看到使用标签库在 JSP 中执行日志记录的简单方法。 Apache Commons 似乎有一个已退役的日志记录标签库,但它基于 log4j,而不是 Apache Commons Logging 本身。

因此,我在 JSP 中以编程方式执行以下操作:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="org.apache.commons.logging.Log" %>
<%@ page import="org.apache.commons.logging.LogFactory" %>
<%@ page isErrorPage="true" %>
<% Log logger = LogFactory.getLog(this.getClass()); %>
<html>
<head>
</head>
<body>
    <% 
        StringBuilder stringBuilder = new StringBuilder("\n" + exception); 
        for(StackTraceElement element : exception.getStackTrace())
        {
            stringBuilder.append("\n" + element.toString());
        }
        logger.error("Unhandled exception caught: " + stringBuilder.toString()); 
    %>
    <p>Text here</p>
</body>
</html>

所以我的问题是......是否有更好的方法来执行此日志记录?上面的方法有效,但对于一项常见任务来说似乎不必要地尴尬

I've read many different articles/threads on this, but have yet to determine the best way to perform Apache Commons logging in my JSP.

To provide some context, I am adding improved error logic to an application that was originally developed using Spring 2. So now we have Controllers that were created using Spring 2 and Spring 3.

So rather than using Spring MVC 3's Exception Handling at Controller level, I think it would be best to have a JSP configured in the web.xml that would log all propogated exceptions for all Controllers.

Surprisingly, I don't see an easy way to perform logging within a JSP using tag libraries. Apache Commons seems to have a retired tag library for logging, but this is based on log4j as opposed to Apache Commons Logging itself.

So, instead I'm performing the following programmatically within my JSP:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="org.apache.commons.logging.Log" %>
<%@ page import="org.apache.commons.logging.LogFactory" %>
<%@ page isErrorPage="true" %>
<% Log logger = LogFactory.getLog(this.getClass()); %>
<html>
<head>
</head>
<body>
    <% 
        StringBuilder stringBuilder = new StringBuilder("\n" + exception); 
        for(StackTraceElement element : exception.getStackTrace())
        {
            stringBuilder.append("\n" + element.toString());
        }
        logger.error("Unhandled exception caught: " + stringBuilder.toString()); 
    %>
    <p>Text here</p>
</body>
</html>

So my question is this....is there a better way to perform this logging? The above works, but seems unnecessarily awkward for what should be a common task

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

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

发布评论

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

评论(1

追我者格杀勿论 2025-01-05 02:03:20

将原始 Java 代码放入 JSP 文件中确实很尴尬。至于您的具体功能需求,

记录所有控制器的所有传播异常

常见的做法是使用过滤器 表示映射到 /* 上的内容。

例如,

@WebFilter("/*")
public class ExceptionFilter implements Filter {

    private Log logger = LogFactory.getLog(getClass());

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(request, response);
        } catch (ServletException e) {
            logger.error("Unhandled exception caught", e);
            throw e;
        }
    }

    // ...
}

请注意,您不需要自己构建堆栈跟踪。如果您将异常作为第二个参数传递,记录器已经这样做了。尽管如此,您仍然可以使用 Throwable#printStackTrace(PrintWriter) 为此。

Putting raw Java code in a JSP file is indeed awkward. As to your concrete functional requirement,

log all propogated exceptions for all Controllers

the common practice is to use a Filter for that which is mapped on /*.

E.g.

@WebFilter("/*")
public class ExceptionFilter implements Filter {

    private Log logger = LogFactory.getLog(getClass());

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(request, response);
        } catch (ServletException e) {
            logger.error("Unhandled exception caught", e);
            throw e;
        }
    }

    // ...
}

Note that you don't need to build the stack trace yourself. The logger already does that if you pass the exception as 2nd argument. Still then, you could have used among others Throwable#printStackTrace(PrintWriter) for that.

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