views:

31

answers:

2

Does anyone know how to get the android device system log programatically using Java? This would be something similar to what is available on lower panel on the Dalvik Debug Monitor.

Thanks in advance.

A: 

does this help?

http://developer.android.com/reference/android/util/Log.html

f0ster
This class logs 'out' but I don't think it can read previous log lines in.
Mathias Lin
not sure if http://developer.android.com/reference/android/util/LogPrinter.html could be used and the system out redirected somehow. I use adb as in my code sample above, although not tested for the logcat that frequently updates.
Mathias Lin
+2  A: 

Untested with 'adb shell logcat', but I've used this to get other things from via adb:

public static String[] getAdbLogCat() {

    try {
        Process p = Runtime.getRuntime().exec("/path/to/adb shell logcat");
        InputStream is = p.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        final StringBuffer output = new StringBuffer();
        String line;
        ArrayList<String> arrList = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        return (String[])arrList.toArray(new String[0]);
    } catch (IOException e) {
        System.err.println(e);
        e.printStackTrace();
        return new String[]{};
    }
}
Mathias Lin
Thanks Mathias, that's exactly what I needed :) You rock!
phillyville
Just for a quick info, 'adb shell logcat' works just fine, and so does 'adb logcat' :)
phillyville