Is there a way to redirect the logcat's output towards an InputStream or something like that? I'm thinking of something in the lines of how you can redirect the stderr in C. I want to do it so its redirected when my app starts and everything that happends get dumped to a file.
-
take a look here http://www.helloandroid.com/tutorials/reading-logs-programatically with proper permission you are able to read logs – Korniltsev Anatoly Oct 22 '12 at 07:51
5 Answers
If you can connect the device for debugging, you can this from the command line
$ adb logcat > textfile.txt

- 6,613
- 2
- 30
- 26
-
How long does this last? I mean, if I reboot the device, will it still be redirected? – Moises Jimenez Oct 22 '12 at 07:54
-
No, It will redirect as long as 'adb' is active. On reboot, 'adb' protocol does not work, hence it will not be redirected at that time. – Royston Pinto Oct 22 '12 at 07:56
-
@rodkarom No, adb will disconnect when you reboot but you can start again or send to a different file when you reboot. – spatulamania Oct 22 '12 at 07:58
-
and if the device is unplugged from the USB? is that logcat will turned off as well? @spatulamania – gumuruh Aug 22 '14 at 00:35
The easiest way I found is to use System.setErr
. I allows you to easily redirect the error output to a file.
Example:
System.setErr(new PrintStream(new File("<file_path>"))

- 1,962
- 3
- 21
- 43
-
2This will only capture output printed to `System.err` (and potentially `System.out` if you redirect it in the same manner). However, the output will not include the prefix Android ads to each line (timestamp, PID, TID etc.), and anything logged through the `Log` class will also not be redirected here. – user149408 Feb 28 '19 at 15:22
-
how do you redirect the Log to a file, run some crashes with Log.e(), and then read the file? – Phlip Feb 17 '22 at 21:59
The only redirecting possibilites of LogCat output are documentend here and here.
It is not possible to reuse the LogCat output in your app itself. You can however, export it to a file like you ask.

- 1
- 1

- 1,184
- 9
- 15
to filter logcat just from your app try this:
int pid = android.os.Process.myPid();
File outputFile = new File(Environment.getExternalStorageDirectory() + "/logcat.txt");
Log.d("zzz","outputFile: " + outputFile);
try {
String command = "logcat | grep " + pid + " > " + outputFile.getAbsolutePath();
Log.d("zzz","command: " + command);
Process p = Runtime.getRuntime().exec("su");
OutputStream os = p.getOutputStream();
os.write((command + "\n").getBytes("ASCII"));
} catch (IOException e) {
}

- 5,511
- 4
- 32
- 53
-
1There may be an easier way: run `logcat` without `su` and without granting the `READ_LOGS` permission to the app, which constrains the entries returned to the caller’s UID (i.e. your app, including earlier instances and subprocesses). This seems to vary between Android distributions, though. Otherwise, `logcat` also has the `-e` option to filter by regular expression. – user149408 Mar 04 '19 at 10:17
In Kotlin, this is what I used and it seems to create and log to the logcat.txt file (located in the app directory) whether the app is being debugged or run directly from the tablet. (Sidenote: For some reason the directory doesn't refresh or show up in file explorer until I stop the app and reconnect the device.) (thanks for the help from the previous contributors)
located in utilities...
val publicDirectory = globalContext.getExternalFilesDir(null)
publicDirectoryName = publicDirectory?.toString() + "/"
fun redirectLogcatToFile() {
try {
val filename =
File(Utilities.publicDirectoryName + "logcat.txt")
filename.createNewFile()
val cmd = "logcat -v time -f " + filename.getAbsolutePath() +" -s " + logcatTag
Runtime.getRuntime().exec(cmd)
} catch (ex: Exception ) {
Utilities.toastOnAnyThread("Utilities.redirectLogcatToFile: " + ex.message)
}
}
fun Log( message : String ){
Log.i(logcatTag, message )
}