java:在生产环境中使用printStackTrace [英] java : Using printStackTrace in Production Environment

查看:567
本文介绍了java:在生产环境中使用printStackTrace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在生产服务器下部署我们的Web应用程序. 在生产环境的catch块下包含printStackTrace是否可以接受? (因为catch块下的日志无助于了解错误的确切原因) 所以请告诉我在catch块下是否有printStackTrace可以接受?

I am going to deploy our Web Application under Production server. Is including printStackTrace under catch block in production environment acceptable? (becuase the logs under catch block doesn't help to know the exact cause for the error) So please tell me if is it aceptable to have printStackTrace under catch block?

例如,我有意放置了无效的端口号,而printStackTrace()向我提供了此信息.

For instance, I purposefully put an invalid port number and printStackTrace() gives me this information.

printStackTrace():

java.lang.IllegalArgumentException: port out of range:80800
        at java.net.InetSocketAddress.<init>(InetSocketAddress.java:118)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:234)
        at sun.net.www.http.HttpClient.New(HttpClient.java:307)
        at sun.net.www.http.HttpClient.New(HttpClient.java:324)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1014)
        at com.tata.util.XmlClient.execute(HttpXmlClient.java:83)

日志样式:

以及日志显示的位置(我正在使用Apache commons日志记录机制进行记录)

And where as the log gives this (I am using Apache commons logging mechanism for logging)

 Log.write("**EXCEPTION INSIDE execute " + e, Log.INFO);

06/Jan/2012 16:25:55   - main:http-8080-2 <> <60990020>**EXCEPTION INSIDE execute  java.lang.IllegalArgumentException: port out of range:80800

所以请告诉我,在catch块下面有printStackTrace是否可以接受?

So please tell me if is it acceptable to have printStackTrace under catch block?

推荐答案

日志还将显示stacktrace,但是您没有正确使用它.试试:

The log will show the stacktrace as well, but you're not using it correctlly. Try:

log.error(Object message, Throwable t);

当您执行Log.write("**EXCEPTION INSIDE execute " + e, Log.INFO);时,用作String连接器的加号实际上将在e对象上调用toString()方法,并将其附加到"**EXCEPTION INSIDE execute " String上,永远不会更改以获取异常对象还是堆栈跟踪.

When you do Log.write("**EXCEPTION INSIDE execute " + e, Log.INFO); the plus sign used as String concatenator will in fact call the toString() method on the e object, and append that to the "**EXCEPTION INSIDE execute " String, never getting a change to get to the exception object or it's stack trace.

http://commons.apache.org/logging/guide.html#Logging 消息

这篇关于java:在生产环境中使用printStackTrace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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