在Java中提取用户友好的异常详细信息 [英] Extracting user-friendly exception details in Java

查看:357
本文介绍了在Java中提取用户友好的异常详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在开发的J2EE Web应用程序,当发生异常时,我想获得一些关于异常的基本细节并记录它。我正在记录的消息应该是相当基本的,这可能意味着运行Web服务器的人的东西。



使用 e.getMessage()是最好的日志记录?谢谢。

解决方案

我默认得到整个堆栈跟踪,这样我可以看到更多的上下文发生了什么发生错误。



如果您使用log4j并将异常传递到其记录方法之一中,它将打印您给出的任何消息,加上异常的消息及其stacktrace你:

  log.error(catch an exception,throwable); 

如果您遇到容易出错的事情,如对Web服务的调用,可以将异常抛出异常你分配一个非常明确的消息,所以当管理员看到它的消息会告诉他们发生了什么。



对于这个问题,你可以保留单独的日志,一个与开发人员的堆栈跟踪,另一个你写管理员友好的消息。您可以有一个异常处理程序尝试将异常映射到友好的消息,然后将该消息写入管理日志。


I've got a J2EE web application that I'm working on and when an exception occurs, I'd like to get some basic details about the exception and log it. The message that I'm logging should be pretty basic, something that might mean something to the people running the web server(s).

Would using e.getMessage() be the best thing to log? Thanks.

解决方案

I default to getting the whole stacktrace, that way I can see more of the context of what was going on when the error occurred.

If you use log4j and pass the exception into one of its logging methods it will print whatever message you give it, plus the exception's message and its stacktrace for you:

log.error("caught an exception", throwable);

If you have error-prone things like calls to web services, you could wrap exceptions thrown in an exception that you assign a very clear message to, so when the admins see it that message will tell them what's going on.

For that matter, you could keep separate logs, one with stacktraces for the developers and another that you write admin-friendly messages to. You could have an exception handler that tries to map the exception to a friendly message, then write that message to an admin log.

这篇关于在Java中提取用户友好的异常详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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