44

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?

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
jax
  • 37,735
  • 57
  • 182
  • 278

8 Answers8

50

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.

Community
  • 1
  • 1
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • 11
    it looks like `Util.stackTraceWriter` is not there anymore. Anyway there's this [`Log.getStackTraceString`](http://developer.android.com/reference/android/util/Log.html#getStackTraceString%28java.lang.Throwable%29) – superjos Dec 14 '11 at 09:46
  • You are not allowed using Logs when you want to publishing the app! – Soheil Setayeshi Jul 16 '13 at 07:32
  • @SoheilSetayeshi What makes you think that? Check the logs of your phone; you will see a lot of logs from installed apps. – Christopher Orr Jul 16 '13 at 09:53
  • 1
    @ChristopherOrr : Check [this](http://developer.android.com/tools/publishing/publishing_overview.html) out dude. The first step in "Preparing Your Application for Release" – Soheil Setayeshi Jul 16 '13 at 11:21
  • 3
    @SoheilSetayeshi Yeah, there's a difference between "not allowed" to use logs, and a list of recommendations :) Google Play will block apps which are debuggable (I believe), but they're not going to prevent people from debugging their apps in production. – Christopher Orr Jul 17 '13 at 12:04
  • Actually the stack trace is printed WITH the app name, at least as of v. 21 – igorepst Mar 02 '15 at 07:29
3

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.

Mark B
  • 183,023
  • 24
  • 297
  • 295
2

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

Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54
1

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());
      }
}
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Brandon
  • 29
  • 3
0

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.

helios
  • 13,574
  • 2
  • 45
  • 55
0

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"));
              }
          });
    }
}
Dori
  • 18,283
  • 17
  • 74
  • 116
0

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));
 }
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
-1

Use this to remove the logs from release apk

if (BuildConfig.DEBUG) Log.d(TAG, "your meseage");
codeskraps
  • 1,461
  • 2
  • 12
  • 13