Skip to main content

Android: To Display the system log inside your application

To display the system log inside your application, filtered for your own application-name, you could insert the following as an inner class:


private static final String LogCatCommand = "logcat ActivityManager:I *:S";
private static final String ClearLogCatCommand = "logcat -c";

private class MonitorLogThread extends Thread{
    public MonitorLogThread(){
    }

    BufferedReader br;

    @Override
    public void run() {
        try {
            Process process;
            process = Runtime.getRuntime().exec(ClearLogCatCommand);
                process = Runtime.getRuntime().exec(LogCatCommand);
                br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                // Check if it matches the pattern
                while(((line=br.readLine()) != null) && !this.isInterrupted()){

                // Filter for your app-line
                if (line.contains("your-filter-string")){
                    Log.i("myAppTag", "Found log-entry for my app:" + line);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

And in your onCreate-Method:

Thread mThread = new MonitorLogThread();
mThread.start();

Comments