我应该在发布之前从代码中删除 e.printStackTrace()
我正在阅读 Android Publishing 文档,他们说从我的代码中删除所有 Log 调用。我的代码中对 e.printStackTrace() 进行了一些调用,这些调用可以作为程序正常运行的一部分进行打印(即,如果文件尚不存在)。
我还应该删除这些电话吗?
I was reading the the Android Publishing docs and they said to remove all Log calls from my code. I have some calls to e.printStackTrace()
in my code that can be printed as part of the normal running of my program (ie. if a file does not exist yet).
Should I also remove these calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
无论如何,您不应该直接使用
e.printStackTrace()
— 这样做会将信息发送到 Android 日志,而不显示它来自哪个应用程序(日志标签)。正如其他人提到的,继续捕获有问题的异常,但使用 android.util.Log 方法之一进行日志记录。您可以仅记录消息,而不记录堆栈跟踪,或者对堆栈跟踪使用详细日志记录:
您应该从生产版本中删除
DEBUG
或VERBOSE
日志消息。最简单的方法是使用 ProGuard 删除Log.[dv]
调用 来自您的代码。You shouldn't be using
e.printStackTrace()
directly anyway — doing so will send the info to the Android log without displaying which application (log tag) it came from.As others have mentioned, continue to catch the
Exception
in question, but use one of theandroid.util.Log
methods to do the logging. You could log only the message, but not the stack trace, or use verbose logging for the stack trace:You should strip
DEBUG
orVERBOSE
log messages from your production builds. The easiest way is to use ProGuard to removeLog.[dv]
calls from your code.如果您允许异常传播到操作系统,那么操作系统将记录它并弹出一个“强制关闭”窗口,从而终止您的应用程序。如果您发现它,则可以防止您的应用程序被强制关闭。
如果您希望您的用户能够向您发送他们收到的错误,那么我会记录堆栈跟踪。然后,他们可以通过 Log 等应用程序向您发送日志收集器。
如果您想避免将堆栈跟踪信息暴露给用户,请捕获异常并且不记录它。
If you allow an Exception to propagate up to the OS then the OS will log it and also pop up a Force Close window, killing your application. If you catch it, then you can prevent your application from being force closed.
If you want your users to have the ability to send you errors that they are getting, then I would log the stack trace. They can then send you the log via an app like Log Collector.
If you want to avoid the possibility of exposing your stack trace information to your users, then catch the exception and don't log it.
我会使用 Log 类来输出消息。对于您认为保留在应用程序中很重要的日志 - 使用 Log.i
错误警告 - Log.e Log.w
供您调试 Log.d - 如果您的应用程序处于调试模式,您可以关闭它。
http://developer.android.com/reference/android/util/DebugUtils。 html
I would use Log class for message out put. For logs that you think are important to stay in the app - use Log.i
for errors warning - Log.e Log.w
For you debug Log.d - and that you can turnoff on base on if your application is in debug mode.
http://developer.android.com/reference/android/util/DebugUtils.html
那么 printStackTrace() 会将其记录到操作系统中,导致您的 andorid (或计算机)应用程序终止(强制关闭),相反,请执行以下操作:
Well
printStackTrace()
will log it into the OS, causing your andorid (or computer) app to terminate (force close), instead, do something like this:以我的谦虚观点(我不是 Android 开发者)
它应该很好。我不知道 Android 的日志记录选项,但我确信您有一些可配置的东西来输出(或不输出)您的跟踪。
如果你不执行 printStackTrace() Android 将不会做忽略它的肮脏工作。
:)
这只是一种感觉(风格)良好的事情。
in my modest opinion (I'm not an Android developer)
It should be nice. I don't know the logging options for Android but I'm sure you have some configurable thing to output (or not) your traces.
And if you don't do printStackTrace() Android will not be doing the dirty work of ignoring it.
:)
It's only a good-feeling (style) thing.
如果你想要安全,即不允许任何人窥探读取异常日志,你可以这样做
If you want to be secure i.e. not allow anyone snooping to read exception logs you can do something like
为了以更安全的方式使用
printStackTrace
,我将使用StringWrite
和PrintWriter
:或者:
In order to use
printStackTrace
in a safer way I would useStringWrite
andPrintWriter
:Or alternatively:
使用它从发布的 apk 中删除日志
Use this to remove the logs from release apk