5

I want to get logs of the whole device, the way logcat shows the processes running in the device. I want to capture it from within my android application.

surbhi verma
  • 357
  • 4
  • 11
  • https://stackoverflow.com/questions/2882253/how-do-i-get-the-logfile-from-an-android-device – AskNilesh Aug 23 '18 at 09:37
  • Possible duplicate of [Read logcat programmatically within application](https://stackoverflow.com/questions/12692103/read-logcat-programmatically-within-application) – jujka Aug 23 '18 at 09:38
  • These links only provide logs of my app. I basically want the system logs so that when any other app which is present in my device crashes, then I want to know its cause and app name. – surbhi verma Aug 23 '18 at 17:03
  • You can't get those, unless you are either rooted or have your app as a system app. Because the `READ_LOGS` permission is a *system level* permission as described [here](https://developer.android.com/guide/topics/permissions/overview). – not2qubit Apr 07 '19 at 20:02

1 Answers1

4

Your app needs to use the permission android.permission.READ_LOGS. (unsure if this works on unrooted device).

and you need to grant the permission to your app in runtime to access this permission with:

adb shell pm grant <pkg> android.permission.READ_LOGS

(or) programmatically with:

Runtime.getRuntime().exec("pm grant <pkg> android.permission.READ_LOGS");

Then you will be able to get the system logs using shell commands for logcat. For example, to get a logcat dump as a txt file in the device:

Runtime.getRuntime().exec("logcat -d -f" + " /sdcard/Logcat_file.txt");

will get the logs saved in a txt file in the android device , using which further validation can be done.

not2qubit
  • 14,531
  • 8
  • 95
  • 135
srm
  • 81
  • 6