I want to dump Android logcat in a file whenever user wants to collect logs. Through adb tools we can redirect logs to a file using adb logcat -f filename
, but how can I do this programmatically?
Asked
Active
Viewed 5.9k times
71

JonasVautherin
- 7,297
- 6
- 49
- 95

Brijesh Masrani
- 1,439
- 3
- 16
- 27
-
http://stackoverflow.com/a/8417757/1012284 – Padma Kumar Jul 03 '12 at 09:22
4 Answers
126
Here is an example of reading the logs.
You could change this to write to a file instead of to a TextView
.
Need permission in AndroidManifest
:
<uses-permission android:name="android.permission.READ_LOGS" />
Code:
public class LogTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) {
}
}
}
-
3@jkhouw1... +60 Best answer i ever seen and great way to post an answer, (also i learn from you) This answer will be always helpful even link got dead,all i need to add just Scroll View and i have done what i wanted, because of your answer, Cheers!! – swiftBoy Jul 03 '12 at 05:58
-
-
1i haven't tested this but i think you would change it to "logcat -d -s YourTagToFilterOn" – jkhouw1 Aug 28 '12 at 11:28
-
-
4This permission was removed in Android 4.3, so apps can no longer use it beyond their process. Also, many devices have taken it a step further and this outputs nothing. I don't have a list because I see it fairly often. – Tom Sep 19 '14 at 20:32
-
@Tom: Do we have any other way to get logs and save theme in a file. My app have a option to report problem, where i want to upload that file to server. – Max Jun 05 '15 at 10:26
-
logcat command line options are described here https://developer.android.com/studio/command-line/logcat.html – jayeffkay Dec 07 '16 at 10:22
-
Very nice! This is exactly what I am looking for. I changed it slightly to dump the log output to a file whenever my application crashes. – Shadoninja Apr 07 '17 at 04:34
-
@Shadoninja hi, how you know your app crashes? where you add this code? – flankechen Aug 19 '20 at 09:25
-
@flankechen - I don't remember exactly how I did it, but I have some ideas for you. One way you could do this is to have your `main` function execute the rest of your program in a try/catch block and `catch` the generic `Exception` class. When you do this, any time your program crashes, you could output the exception to logcat. – Shadoninja Aug 23 '20 at 15:14
43
Logcat can write directly to a file:
public static void saveLogcatToFile(Context context) {
String fileName = "logcat_"+System.currentTimeMillis()+".txt";
File outputFile = new File(context.getExternalCacheDir(),fileName);
@SuppressWarnings("unused")
Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath());
}
more info on logcat: see http://developer.android.com/tools/debugging/debugging-log.html

Stéphane
- 6,920
- 2
- 43
- 53
-
1This command seems to stall since logcat -f doesn't automatically exit. Any way to write the log to a file and automatically exit the process similar to logcat -d ? – Phillip Jan 08 '14 at 06:39
-
-
7
-
After initially using this approach, I switched to using a buffered reader and writer because -f doesn't handle spaces in file names well. – LordParsley Jun 22 '16 at 09:27
-
Yes, it should be -df. Also **Note: This code dumps it in external cache directory** – Atul Jul 27 '16 at 18:01
1
Or you can try this variant
try {
final File path = new File(
Environment.getExternalStorageDirectory(), "DBO_logs5");
if (!path.exists()) {
path.mkdir();
}
Runtime.getRuntime().exec(
"logcat -d -f " + path + File.separator
+ "dbo_logcat"
+ ".txt");
} catch (IOException e) {
e.printStackTrace();
}
0
public static void writeLogToFile(Context context) {
String fileName = "logcat.txt";
File file= new File(context.getExternalCacheDir(),fileName);
if(!file.exists())
file.createNewFile();
String command = "logcat -f "+file.getAbsolutePath();
Runtime.getRuntime().exec(command);
}
Above method will write all logs into the file. Also please add below permissions in Manifest file
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Ijas Ahamed N
- 5,632
- 5
- 31
- 53