Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read system log file in Android?

Tags:

android

I am trying to read system logs in my code to generate something like an error report. Similar to adb logcat, but in a programming way.

How can I achieve this?

like image 451
Jimmy Avatar asked Jul 22 '10 17:07

Jimmy


People also ask

Is there a system log on Android?

Android allows collecting system logs using Logcat. Log messages can be viewed in a Logcat window in Android Studio, or you can use the command line tool to pull them. Several Android apps are also available in the Google Play store that allow easy access to these tools.


1 Answers

Log Collector has their source code available on Google Code. They actually just call logcat. See here: android-log-collector - SendLogActivity.java

Here is the key part:

ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("logcat");//$NON-NLS-1$
commandLine.add("-d");//$NON-NLS-1$
ArrayList<String> arguments = ((params != null) && (params.length > 0)) ? params[0] : null;

if (null != arguments){
    commandLine.addAll(arguments);
}

Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;
while ((line = bufferedReader.readLine()) != null){ 
     log.append(line);
     log.append(App.LINE_SEPARATOR); 
}
like image 193
EboMike Avatar answered Oct 07 '22 02:10

EboMike