Launch Logcat programmatically in Android

less than 1 minute read

Today I wanted to write logcat output to a file while my application is running. To do so I executed the following code:

public void executeLogcat(){
	File logFile = new File("/mnt/sdcard/arxeion.log"); // log file name
	int sizePerFile = 60; // size in kilobytes
	int rotationCount = 10; // file rotation count
	String filter = "D"; // Debug priority

	String[] args = new String[] { "logcat",
					"-v", "time",
					"-f",logFile.getAbsolutePath(),
					"-r", Integer.toString(sizePerFile),
					"-n", Integer.toString(rotationCount),
					"*:" + filter };

	try {
		Runtime.getRuntime().exec(args);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

To execute logcat process we are obliged to add

xml

uses permission in AndroidManifest.xml file.

WARNING!!

  1. This code should NOT be used in a release version of your application.
  2. You should control when to kill logcat process, as it will run forever

Comments