How to redirect my log output from logcat to the SD-Card on an android device?
use logcat -f <filename>
in order to dump it to a file in the filesystem.
Make sure the filename you're using is in the sdcard, i.e. starts with /sdcard/...
.
Also, in order to pass arguments to the logcat
program, you should pass the exec
method an array of strings (and not one string):
String[] cmd = new String[] { "logcat", "-f", "/sdcard/myfilename", "-v", "time", "ActivityManager:W", "myapp:D" };
Finally, if all else fails, use the full path for logcat: /system/bin/logcat
instead of just logcat
.
Logcat output on SDCard
I think this is not directly possible. What you could do it to override the standard Log.x()
methods like this
class MyLog {
boolean sdLog = false;
public void d(String arg1, String arg2) {
if (sdLog) {
someLogFile.append(arg1).append(":").append(arg2);
}
else {
Log.d(arg1,arg2);
}
}
}
and then in your code replace all calls to Log.d()
with calls to MyLog.d()
. The user can then e.g. in preferences select to log to the sd card or use the standard mechanism.
Generally logging everything to the SD card was not chosen (afair) by Android, as this would mean huge amounts of writes to the card, which would reduce its lifetime.
How to redirect logcat output?
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>"))
redirect adb logcat to android avd
You can simply do
adb shell "logcat -v time -f /mnt/sdcard/log.txt packageName:F *:E"
to accomplish it all in one command from the host shell. You do not need the redirect when you use the -f flag, in fact the redirect would not capture anything if you have directed the output of logcat to a file rather than to stdout.
If that is not working, it is either because you are using a version of Android which mounts the external storage at some other path, or you do not have an emulated sdcard attached to your virtual device.
You can investigate either of these problems by examining the output of
adb shell mount
If you do not have an sdcard at all on your AVD, follow the emulator documentation instructions for creating and attaching one.
For testing purposes only there may be other paths than the sdcard at which you can write, particularly on an emulator where the adb shell runs as root, for example on some versions /data/local
or similar.
Catch LogCat programmatically or export it to file?
File filename = new File(Environment.getExternalStorageDirectory()+"/mylog.log");
filename.createNewFile();
String cmd = "logcat -d -f"+filename.getAbsolutePath();
Runtime.getRuntime().exec(cmd);
it works for me. but for all logcat output not for special tag(mytag).
Save app event (logcat) in file on sd
You can get logcat via the following:
static final int BUFFER_SIZE = 1024;
public String getLogCat() {
String[] logcatArgs = new String[] {"logcat", "-v", "time"};
Process logcatProc = null;
try {
logcatProc = Runtime.getRuntime().exec(logcatArgs);
}
catch (IOException e) {
return null;
}
BufferedReader reader = null;
String response = null;
try {
String separator = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), BUFFER_SIZE);
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(separator);
}
response = sb.toString();
}
catch (IOException e) {
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (IOException e) {}
}
}
return response;
}
You can then save this String
to the sdcard.
Related Topics
Callback to a Fragment from a Dialogfragment
How to Set Opacity (Alpha) for View in Android
How to Add a Button to Preferencescreen
How to Properly Highlight Selected Item on Recyclerview
Mapview in a Fragment (Honeycomb)
Find Distance Between Two Points on Map Using Google Map API V2
Android Studio Does Not Show Layout Preview
Where Is Android.Os.Systemproperties
Border for an Image View in Android
Enabling Proguard in Eclipse for Android
Best Practice: Asynctask During Orientation Change
Error Android Emulator Gets Killed in Android Studio
Best Method to Download Image from Url in Android
Tabwidget Current Tab Bottom Line Color