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.
Asked
Active
Viewed 7,047 times
5
-
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 Answers
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.
-
1this does not work on the non-rooted devices and only gives logs of the app itself and not of the whole device. – surbhi verma Aug 30 '18 at 05:46