English 中文(简体)
Should I remove e.printStackTrace() from my code before publishing
原标题:

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?

最佳回答

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.

问题回答

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.

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

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());
      }
}

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

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"));
              }
          });
    }
}

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));
 }

Use this to remove the logs from release apk

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




相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签