0

I am a little confused to differenate System.err/out, e.printStackTrace and log while logging RuntimeException.

At first, I thought that log information is not saved in a file if I use System.err/out, e.printStackTrace and just printed on console.

But System.err/out, e.printStackTrace also seem to be logged in a log buffer and we can see it through logcat.

Threfore, if we can save log in a file and also System.err/out, e.printStackTrace results can saved in a file.

Q1. What I understand is right?

Q2. If it is, which logging type is better to debug field issues after production?

Q3. If any RuntimeExceptions occur, where can I find the log file in android folder?

UPDATE

I got an answer for Q3.

In case of android, RuntimeException log seems to be saved in /data/system/dropbox.

Cœur
  • 37,241
  • 25
  • 195
  • 267
500004dolkong
  • 725
  • 3
  • 12
  • 19

2 Answers2

2

With Android, it's more efficient to log exceptions and write messages using the methods in Log. In emulators and on most devices, writing directly to System.out or System.err get redirected to Log.i. On some devices, writes to System.out/err are lost. From the Android docs:

By default, the Android system sends stdout and stderr (System.out and System.err) output to /dev/null.

Using e.printStackTrace also just writes to System.err, and I believe that it is less informative than the Log methods, since you can add a log tag to the latter.

As far as I know, all the logging information, including output from app crashes, is kept in the logs. (If you catch RuntimeExceptions, there is no output unless your code generates it.) You can find the logs, I believe, in /system/bin/logcat; however, it's not in plain text format. You can pull the information off the device in a readable form by attaching it to a development environment and entering the command:

adb logcat -d -v time > logfile.txt

There's also a logcat view in Eclipse. There are also some free utilities (aLogCat and CatLog come to mind) for looking at the logcat data on a device.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • When I write something like `System.out.println("test");` in my java code I see it printed in the logcat in eclipse - I do not remember setting any options there - does this not contradict the docs ? (NB: I have my device plugged in in my PC) see also : http://stackoverflow.com/a/17199704/281545 – Mr_and_Mrs_D Nov 20 '13 at 01:18
  • 1
    @Mr_and_Mrs_D - By default, System.out is routed to /dev/null, but with additional configuration it can be sent to logcat. I believe that the emulators do that additional configuration on their own. If you debug an app on a real device through Eclipse, you may or may not see System.out output in logcat, depending on the device. – Ted Hopp Nov 20 '13 at 03:14
1

What I understand is right?

Your understanding seems fine. Most things written to standard output or error are displayed in Logcat. As Ted Hopp noted, though, it's preferable to use the Log class for logging. This information is not saved to an easily-accessed file by default, but you could perform that step yourself.

If it is, which logging type is better to debug field issues after production?

It's difficult to collect debugging information if you're storing it on the device in a file. If you're deploying your app to the Google Play Store, you can use the Developer Console to view stack traces from your app (provided that your users have opted to send crash reports). A more reliable solution would be to use a service that tracks crashes in the wild and aggregates a lot of useful statistics for you (e.g., Crittercism).

If any RuntimeExceptions occur, where can I find the log file in android folder?

They're not stored in a log file, as far as I know. You could catch each exception and write something to a custom log file (whose location you can choose), but that tends to be unwieldy; the services that I mentioned above will be much more efficient for doing this.

acj
  • 4,821
  • 4
  • 34
  • 49