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?
Write android logcat data to a file
https://mcmap.net/q/162052/-how-can-i-access-logcat-file-on-device –
Stabler
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) {
}
}
}
@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!! –
Buoyage
Great..But is there a way to filter these logs as per the Tags? –
Pitapat
i haven't tested this but i think you would change it to "logcat -d -s YourTagToFilterOn" –
Cozen
How can we put the package name filter. –
Draggle
This 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. –
Amblyopia
@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. –
Bonaventura
logcat command line options are described here developer.android.com/studio/command-line/logcat.html –
Swine
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. –
Roque
@Roque hi, how you know your app crashes? where you add this code? –
Yoder
@Yoder - 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. –
Roque 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
This 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 ? –
Diabetes
Try to put an & to the end make it background. –
Bait
logcat -df <file_name> sink all logs to the <file_name> and exits immediately. –
Forehand
After initially using this approach, I switched to using a buffered reader and writer because -f doesn't handle spaces in file names well. –
Cede
Yes, it should be -df. Also Note: This code dumps it in external cache directory –
Capelin
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();
}
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" />
© 2022 - 2024 — McMap. All rights reserved.