collect icon indicating copy to clipboard operation
collect copied to clipboard

Clean up printing stack trace call when exception occured

Open nribeka opened this issue 9 years ago • 36 comments

Exception should be logged by calling: Log.e(String, String, Throwable) or some others Log's method.

In the codebase, we have a lot of call to e.printStackTrace(). Those calls should be replaced by the appropriate Log.x methods.

nribeka avatar Mar 10 '17 17:03 nribeka

@lognaturel or @yanokwa, do you have preference to do this as separate PR per files or separate commit per file? This could change a lot of files.

nribeka avatar Mar 10 '17 17:03 nribeka

@nribeka We've struggled with this kind of change. What's your recommendation?

lognaturel avatar Mar 10 '17 17:03 lognaturel

Initial search showed 158 occurrence of printStackTrace and it's all over the place. Maybe at the minimum we go with separate commits per file? Separating per PR seems to be too much.

screen shot 2017-03-10 at 12 12 50 pm

nribeka avatar Mar 10 '17 17:03 nribeka

To whoever going to do this, don't just do find and replace in the code. You need to read the context of the stacktrace and decide which Log's method will be appropriate. I don't think all should become a Log.e calls.

nribeka avatar Mar 10 '17 17:03 nribeka

I was hoping you'd have some kind of magic solution we hadn't thought of! 😉 We need a process because there are a lot of these kinds of issues. A single PR with a commit per file seems ok to me.

I think @batkinson had some opinions on this kind of situation.

lognaturel avatar Mar 10 '17 17:03 lognaturel

And folks who will do this can propose a simplified catch blocks too when we're doing the same thing for each type of exception. One example is the following codes:

screen shot 2017-03-10 at 12 20 48 pm

Since every catch block treat the exception the same way, there's no benefits of separating it into different blocks.

nribeka avatar Mar 10 '17 17:03 nribeka

Can i work on this issue? @nribeka i assume that what you propose in the last message is to combine catch blocks wherever possible like the one you have shown?

Midhun07 avatar Mar 11 '17 13:03 Midhun07

@Midhun07 I am working on this issue.

parthuest avatar Mar 11 '17 13:03 parthuest

@Midhun07 The real issue is that printStackTrace method has been used everywhere for catching the try blocks. printStaceTrace prints the stack trace in Android Monitor as an error because of the exception caused.

At some places eg. GoogleDriveActivity, this stack trace is not a critical exception so it unneccessarily clutters the error logs.

So instead of calling e.printStackTrace(), relevant Log method Log.d, Log.e, Log.v, Log.w, Log.i should be used. This would help filtering the logs so that relevant logs are not missed. Also as suggested by @nribeka, multiple catch blocks which are handling the exception exactly the same way should be squashed into one catch block.

shobhitagarwal1612 avatar Mar 12 '17 06:03 shobhitagarwal1612

@yanokwa @nribeka I stumbled across a great library by Jake Wharton, Timber. A logger with a small, extensible API which provides utility on top of Android's normal Log class. http://jakewharton.github.io/timber/

Pros :

  • Development - Logging to logcat
  • Production - Crash reporting to Crashlytics

Timber and Crashlytics can be used along with AppCrashHandler which elegently handles unhandled exceptions by restarting the app instead of showing the system dialogs displaying an error has occurred. Read the article here link Also addresses #655

shobhitagarwal1612 avatar Mar 12 '17 06:03 shobhitagarwal1612

Is somebody already assigned on the issue? I am willing to help, we can have only 1 catch block for exceptions wherever necessary.

rijuldhir avatar Mar 12 '17 19:03 rijuldhir

I have tried my best to justify the task. @nribeka can you please check it in my profile and let me know if I have done something wrong.

parthuest avatar Mar 14 '17 15:03 parthuest

@parthuest please submit a pull request as described in the contribution guide.

lognaturel avatar Mar 14 '17 16:03 lognaturel

#663 might actually be a nicer way to address this since it could let us address better logging in both development and production.

lognaturel avatar Mar 17 '17 18:03 lognaturel

Hi, @lognaturel Can i work on this issue ? I am familiar with timber.

Ishmeetsingh97 avatar Mar 19 '17 09:03 Ishmeetsingh97

Let's break this down to make sure we get it right. It would be good to get a first pull request JUST removing all calls on e.printStackTrace. If you'd like to do that, go ahead and comment and then take it on.

lognaturel avatar Mar 20 '17 15:03 lognaturel

Hi, @lognaturel Can i work on it?

Ishmeetsingh97 avatar Mar 20 '17 16:03 Ishmeetsingh97

@lognaturel : I can work on it. So basically there will be 2 PR's 1 to just remove all e.printStackTrace and 2nd one to have commits with the appropriate Log.x methods.

anudeepti2004 avatar Mar 20 '17 16:03 anudeepti2004

@Ishmeetsingh97 Since you know Timber, how about you work on adding a Tree for Firebase Crash and verify that it works.

@anudeepti2004 That's right. Go ahead and do just the removal one. Then once we have @Ishmeetsingh97's Tree ready you can go straight to using Timber methods to do the logging right.

lognaturel avatar Mar 20 '17 16:03 lognaturel

Okay, @lognaturel I will give this a try and let you know my developments.

Ishmeetsingh97 avatar Mar 20 '17 16:03 Ishmeetsingh97