1

My question is somewhat similar to this one, but I have more specific needs. Here we go:

In my Android apps, I want to add the functionality of saving the Logcat into a log file. So when a user reports a problem with my apps, I can ask for the log file. Saving the Logcat into a file is quite straightforward, as discussed here. Now the question is: when to save?

My current plan is to call logcat -c in my app's onCreate, and save Logcat in onStop. By calling logcat -c up front, I remove information unrelated to my app.

However, I see two flaws in my plan:

  • If a user installs two different apps from me, there is a chance that the second one gets started before the first finishes. Because onStop of the 1st app is not called, the Logcat is not yet saved. The logcat -c in the 2nd app's onCreate will wipe out any logging information for the 1st app up to that point.

  • Even if only one app is running, its onStop is not called if it crashes. Or is it? I checked the activity's life cycle, but can't tell for sure whether or not onStop is called in the case of a crash. Assuming it is not called, then this beats my purpose. I need the Logcat to understand why my app crashes, but the Logcat is not saved if it crashes.

Any thoughts and/or suggestions? Thanks in advance!

Community
  • 1
  • 1
hubeir
  • 1,279
  • 2
  • 13
  • 24

2 Answers2

0

Enabling logs on the release version is not advisable. When the app crashes and user reports , you can get the crash stack trace from the developer console . Stack trace is well enough to analyze a crash.It will clearly point out the class , method and line number.

If you have enabled proguard in your app , you can retrace the stack trace using mapping file to analyze the crash.

Libin
  • 16,967
  • 7
  • 61
  • 83
  • Thanks. My development has not reached the point to be published, so the Developer Console is not yet an option. Besides the performance concern, are there other reasons why enabling logs on the release version is not advisable? – hubeir Mar 28 '14 at 20:13
0

There are a few other problems with trying to read out Logcat during Runtime: the important parts of the log may have exited the buffer by the time a report is sent and you need the READ_LOGS permission to read Logcat in the first place.

I would suggest implementing one of various free logging libraries. My preference goes to Crashlytics, which automatically takes care of crash reporting (by using UncaughtExceptionHandler) and allowing you to specify various tags and user logs before sending an exception.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187