JSP 中的 Apache Commons 日志记录
我已经阅读了许多关于此的不同文章/线程,但尚未确定在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将原始 Java 代码放入 JSP 文件中确实很尴尬。至于您的具体功能需求,
常见的做法是使用
过滤器
表示映射到/*
上的内容。例如,
请注意,您不需要自己构建堆栈跟踪。如果您将异常作为第二个参数传递,记录器已经这样做了。尽管如此,您仍然可以使用
Throwable#printStackTrace(PrintWriter)
为此。Putting raw Java code in a JSP file is indeed awkward. As to your concrete functional requirement,
the common practice is to use a
Filter
for that which is mapped on/*
.E.g.
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.