我应该在发布之前从代码中删除 e.printStackTrace()

发布于 2024-08-17 19:28:25 字数 147 浏览 6 评论 0原文

我正在阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

久隐师 2024-08-24 19:28:25

无论如何,您不应该直接使用 e.printStackTrace() — 这样做会将信息发送到 Android 日志,而不显示它来自哪个应用程序(日志标签)。

正如其他人提到的,继续捕获有问题的异常,但使用 android.util.Log 方法之一进行日志记录。您可以仅记录消息,而不记录堆栈跟踪,或者对堆栈跟踪使用详细日志记录:

try {
    Object foo = null;
    foo.toString();
} catch (NullPointerException ex) {
    Log.w(LOG_TAG, "Foo didn't work: "+ ex.getMessage());
    Log.d(LOG_TAG, Util.stackTraceWriter(ex));
}

您应该从生产版本中删除 DEBUGVERBOSE 日志消息。最简单的方法是使用 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 the android.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:

try {
    Object foo = null;
    foo.toString();
} catch (NullPointerException ex) {
    Log.w(LOG_TAG, "Foo didn't work: "+ ex.getMessage());
    Log.d(LOG_TAG, Util.stackTraceWriter(ex));
}

You should strip DEBUG or VERBOSE log messages from your production builds. The easiest way is to use ProGuard to remove Log.[dv] calls from your code.

若水般的淡然安静女子 2024-08-24 19:28:25

如果您允许异常传播到操作系统,那么操作系统将记录它并弹出一个“强制关闭”窗口,从而终止您的应用程序。如果您发现它,则可以防止您的应用程序被强制关闭。

如果您希望您的用户能够向您发送他们收到的错误,那么我会记录堆栈跟踪。然后,他们可以通过 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.

秋风の叶未落 2024-08-24 19:28:25

我会使用 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

层林尽染 2024-08-24 19:28:25

那么 printStackTrace() 会将其记录到操作系统中,导致您的 andorid (或计算机)应用程序终止(强制关闭),相反,请执行以下操作:

public void nullPointerExceptionCauser()
{
      try
      {
           Object example = null;
           example.toString();
      }
      catch (Exception e)
      {
           Logger.log(Level.SEVERE, "Caught Exception: {0}", e.getStackTrace());
      }
}

Well printStackTrace() will log it into the OS, causing your andorid (or computer) app to terminate (force close), instead, do something like this:

public void nullPointerExceptionCauser()
{
      try
      {
           Object example = null;
           example.toString();
      }
      catch (Exception e)
      {
           Logger.log(Level.SEVERE, "Caught Exception: {0}", e.getStackTrace());
      }
}
自我难过 2024-08-24 19:28:25

以我的谦虚观点(我不是 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.

烧了回忆取暖 2024-08-24 19:28:25

如果你想要安全,即不允许任何人窥探读取异常日志,你可以这样做

private void hideExceptionsInReleaseMode()
{
    final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();

    if(!BuildConfig.DEBUG)
    {
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
          {
              @Override
              public void uncaughtException(Thread thread, Throwable ex)
              {
                  defaultHandler.uncaughtException(thread, new RuntimeException("Something went wrong :p"));
              }
          });
    }
}

If you want to be secure i.e. not allow anyone snooping to read exception logs you can do something like

private void hideExceptionsInReleaseMode()
{
    final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();

    if(!BuildConfig.DEBUG)
    {
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
          {
              @Override
              public void uncaughtException(Thread thread, Throwable ex)
              {
                  defaultHandler.uncaughtException(thread, new RuntimeException("Something went wrong :p"));
              }
          });
    }
}
ζ澈沫 2024-08-24 19:28:25

为了以更安全的方式使用 printStackTrace,我将使用 StringWritePrintWriter

    ...
catch (final Exception e)
{
   final StringWriter sw = new StringWriter();
   final PrintWriter pw = new PrintWriter(sw);
   e.printStackTrace(pw);
   Log.e("TAG", sw.toString());
}

或者:

 catch (final Exception e)
 {
    Log.e(TAG, Log.getStackTraceString(e));
 }

In order to use printStackTrace in a safer way I would use StringWrite and PrintWriter:

    ...
catch (final Exception e)
{
   final StringWriter sw = new StringWriter();
   final PrintWriter pw = new PrintWriter(sw);
   e.printStackTrace(pw);
   Log.e("TAG", sw.toString());
}

Or alternatively:

 catch (final Exception e)
 {
    Log.e(TAG, Log.getStackTraceString(e));
 }
不…忘初心 2024-08-24 19:28:25

使用它从发布的 apk 中删除日志

if (BuildConfig.DEBUG) Log.d(TAG, "your meseage");

Use this to remove the logs from release apk

if (BuildConfig.DEBUG) Log.d(TAG, "your meseage");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文