Reading logcat programmatically for an application

I have already read this article . But it seems that most of the text that it shows is not in my application. How can I filter a message and let it show only the log for my application. In other words, I want to show it as in Android Studio (only show error log from my application, show timestamp, etc.):

I tried something like "logcat -d -v time" but it doesn't work. Any ideas? Thanks.

enter image description here

+6
source share
1 answer

Try the following code to get only the logs of your application.

public final class MyAppLogReader { private static final String TAG = MyAppLogReader.class.getCanonicalName(); private static final String processId = Integer.toString(android.os.Process .myPid()); public static StringBuilder getLog() { StringBuilder builder = new StringBuilder(); try { String[] command = new String[] { "logcat", "-d", "-v", "threadtime" }; Process process = Runtime.getRuntime().exec(command); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); String line; while ((line = bufferedReader.readLine()) != null) { if (line.contains(processId)) { builder.append(line); //Code here } } } catch (IOException ex) { Log.e(TAG, "getLog failed", ex); } return builder; } } 

Edit

Add permission below to your AndroidManifest file

 <uses-permission android:name="android.permission.READ_LOGS" /> 
+15
source

Source: https://habr.com/ru/post/981032/


All Articles