struts - 每层的日志记录(Action、DAO)
我是 struts 新手,我想了解如何处理应用程序每一层(JSP/ActionForm、Action、DAO)的日志记录。
我注意到随着时间的推移,错误日志文件可能会变得过于混乱且不友好地查看。如何改进日志记录模式以分离应用程序每一层引发的错误?
我也找不到处理 jsp 页面上遇到的异常的方法。
操作的代码模板
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
try {
// calls the DAO
}
catch (CustomDAOException e) {
log.error(e);
// inform application there is an error
}
catch (Exception e) {
log.error(e)
// inform application there is an error
}
}
DAO 的代码模板
public void update() throw CustomDAOException {
try {
// Prepare a statement to insert a record
String sql = "INSERT INTO my_table (col_string) VALUES(?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
// Insert 10 rows
for (int i=0; i<10; i++) {
// Set the value
pstmt.setString(1, "row "+i);
// Insert the row
pstmt.executeUpdate();
}
} catch (SQLException e) {
throw new CustomDAOException(e);
}
I am new to struts and I would like to understand how do I handle logging for each layer(JSP/ActionForm, Action, DAO) of the application.
I notice as time goes by the error log file maybe too cluttered and unfriendly to view. How can I improve on the logging patterns to separate the errors thrown by each layer of the application?
and I couldnt find a way to handle exceptions encounter on jsp page as well.
Code Template for Action
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
try {
// calls the DAO
}
catch (CustomDAOException e) {
log.error(e);
// inform application there is an error
}
catch (Exception e) {
log.error(e)
// inform application there is an error
}
}
Code Template for DAO
public void update() throw CustomDAOException {
try {
// Prepare a statement to insert a record
String sql = "INSERT INTO my_table (col_string) VALUES(?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
// Insert 10 rows
for (int i=0; i<10; i++) {
// Set the value
pstmt.setString(1, "row "+i);
// Insert the row
pstmt.executeUpdate();
}
} catch (SQLException e) {
throw new CustomDAOException(e);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的 struts-config 中设置一个异常处理程序,让所有异常都冒泡到该处理程序,并将它们记录在那里。这将使所有异常捕获和记录代码变得不必要。
至于JSP,不要在JSP中做任何容易出错的事情,在action中做,然后将结果添加到请求属性中。
我猜您可能使用的是 Struts 1 的古老版本,我见过类似的代码,它们必须捕获操作中的异常,显然是因为异常处理程序不适用于早期版本的 struts。现在这个问题已经解决了,您应该做的第一件事就是将应用程序至少升级到 1.2,最好是 1.3。使用过时版本的 Web 框架是一件坏事,您错过的修复程序中有一些安全补丁,因此您的应用程序可能不安全。
看看你的示例代码,它看起来确实没有那么糟糕。我不认为在你的操作中捕获不同类型的异常有什么意义(如果我知道你省略了什么,这可能会更清楚),但我没有看到像在每一层中记录事物然后重新抛出它们这样的不良做法(这样相同的堆栈跟踪重复出现),或者记录异常并返回 null(因此调用代码可能不知道出了什么问题),或者只是在没有跟踪的情况下吃掉异常。如果您更具体地说明您想要解决的问题,也许我或其他人可以提供更多帮助。
Set up an exception-handler in your struts-config, let all exceptions bubble up to that, and log them there. That will make all your exception-catching-and-logging code unnecessary.
As for the JSP, don't do anything error-prone in the JSP, do it in the action, then add the results to the request attributes.
I'm guessing you might be using an ancient version of Struts 1, I've seen similar code where they had to catch exceptions in the actions, apparently because exception-handlers didn't work with early versions of struts. That's fixed now, the first thing you should do is upgrade the application to at least 1.2, preferably 1.3. Using an obsolete version of a web framework is a bad thing, there are security patches among the fixes you're missing out on, so your application may be insecure.
Looking at your sample code, it really doesn't seem that bad. I don't see the point of catching different types of exception in your actions (which might be clearer if I knew what you elided), but I don't see bad practices like logging things in each layer and then rethrowing them (so that the same stack trace shows up repeatedly), or logging exceptions and returning null (so the calling code may not know anything went wrong), or just eating exceptions without a trace. If you give more specifics on what problems you want to solve maybe I or somebody else can be more helpful.