Saravanakumar
Saravanakumar

Reputation: 69

How to read logcat written through my application in all android os

I need to capture all the logs written through my application. I know from Jetllybean OS we can read need only our application log. But when I tried by using command "logcat -d" using exec method by application and I did not get any data.

Please help me on this.

Thanks,

Saravanakumar

Upvotes: 0

Views: 996

Answers (1)

wangyif2
wangyif2

Reputation: 2863

This is the example that I was playing around with before that will generate a log text file in local storage:

private static String generateLogcatLogCommond = "logcat -d > /sdcard/IssueReport/log.txt";

public static String generateLogcatLog() throws InterruptedException {
    try {
        File issueReport = new File(Environment.getExternalStorageDirectory(), "IssueReport");
        if (!issueReport.exists())
            issueReport.mkdir();
        File logFile = new File(issueReport,"log.txt");
        logFile.createNewFile();

        Process process = Runtime.getRuntime().exec("/system/bin/sh -");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(generateLogcatLogCommond);

        logLocation = "/sdcard/IssueReport/log.txt";
        Log.d("Client", logLocation);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return logLocation;
}

What the above code is doing is using 'sh' to run 'logcat -d' command and save it as a file locally. This will get ALL the logcat log. For you, you can change that to 'logcat -s ""' and it will save all logcat log of your application to a file.

Upvotes: 1

Related Questions