每当用户想要收集日志时,我想在一个文件中转储Android logcat。通过adb工具,我们可以使用adb logcat -f filename
将日志重定向到一个文件,但是我如何以编程方式做到这一点?
发布于 2011-05-30 18:53:57
这是一个读取日志的example。
您可以将其更改为写入文件,而不是写入TextView
。
需要AndroidManifest
中的权限
<uses-permission android:name="android.permission.READ_LOGS" />
代码:
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) {
}
}
}
发布于 2012-11-15 07:30:57
Logcat可以直接写入文件:
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());
}
有关logcat的更多信息:请参阅http://developer.android.com/tools/debugging/debugging-log.html
发布于 2015-08-03 23:56:30
或者你也可以试试这个变种
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();
}
https://stackoverflow.com/questions/6175002
复制相似问题