在Java中提取用户友好的异常详细信息 [英] Extracting user-friendly exception details in Java
问题描述
使用 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屋!