如何从Android设备获取日志文件?
Logcollector是一个不错的选择,但你必须先安装它。
当我想要得到的日志文件通过邮件发送,我通常会做到以下几点:
- 将设备连接到PC。
- Check that I already setup my os for that particular device。
- 打开一个终端
- 运行
adb shell logcat > log.txt
编辑:
内部日志是内存中的循环缓冲区。实际上有几个这样的循环缓冲器用于无线电,事件,主要。默认是主。
为了得到缓冲的副本,一种技术涉及到设备上执行的命令,并获得输出作为一个字符串变量。
SendLog是一个开源应用,该应用不只是这样的:http://www.l6n.org/android/sendlog.shtml
的关键是在嵌入式OS的设备上运行logcat
。它不像听起来那么难,只需查看链接中的开源应用即可。
这转储输出到屏幕上,我需要它在一个文件中。 – Pentium10 2010-05-21 14:45:35
您可以使用aLogCat发送日志。或者直接从DDMS复制粘贴:P – 2010-05-21 18:33:55
@molnarm我承认我的答案稍微过度设计,如果这就是他需要做的=) – 2010-05-21 18:55:49
一个简单的方法就是让自己的日志收集方法,甚至只是一个现有的从市场上收集日志应用。
对于我的应用我做它发送日志到我的电子邮件报告功能(甚至到另一个地方 - 一旦你的日志,你可以做你是否想用它)。
下面是有关如何从设备获取日志文件一个简单的例子:
我经常遇到错误 “logcat read: Invalid argument
”。在阅读日志之前,我必须清除日志。
我这样做:
prompt> cd ~/Desktop
prompt> adb logcat -c
prompt> adb logcat | tee log.txt
谢谢!我长时间有这个错误,logcat -c立即修复它! – Jords 2012-01-31 01:16:32
我会用这种东西:
$adb logcat -d > logcat.txt
-d选项转储整个循环缓冲区的文本文件,如果你正在寻找特定的动作/意图尝试
$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt
希望这有助于:)
我希望这个代码将会帮助别人。我花了2天弄清楚如何从设备登录,然后将其过滤:
public File extractLogToFileAndWeb(){
//set a file
Date datum = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ITALY);
String fullName = df.format(datum)+"appLog.log";
File file = new File (Environment.getExternalStorageDirectory(), fullName);
//clears a file
if(file.exists()){
file.delete();
}
//write log to file
int pid = android.os.Process.myPid();
try {
String command = String.format("logcat -d -v threadtime *:*");
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder result = new StringBuilder();
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
if (currentLine != null && currentLine.contains(String.valueOf(pid))) {
result.append(currentLine);
result.append("\n");
}
}
FileWriter out = new FileWriter(file);
out.write(result.toString());
out.close();
//Runtime.getRuntime().exec("logcat -d -v time -f "+file.getAbsolutePath());
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
//clear the log
try {
Runtime.getRuntime().exec("logcat -c");
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
return file;
}
通过@mehdok
为指向的权限添加到清单读取日志
<uses-permission android:name="android.permission.READ_LOGS" />
这工作正常,但要在你需要的每个设备上工作'' – mehdok 2014-07-19 11:43:58
对我来说不起作用 – NinjaCoder 2017-10-24 18:44:20
感谢user1354692我可以让它更简单,只有一行!他曾这样评价了一句:
try {
File file = new File(Environment.getExternalStorageDirectory(), String.valueOf(System.currentTimeMillis()));
Runtime.getRuntime().exec("logcat -d -v time -f " + file.getAbsolutePath());}catch (IOException e){}
哪里是放这段代码的最佳位置?在'OnCreate'? – Youngjae 2014-03-31 01:15:31
当你想要的日志:P – 2015-01-12 13:30:33
首先要确保亚行命令是通过设置PATH到Android SDK平台的工具可执行文件:
export PATH=/Users/espireinfolabs/Desktop/soft/android-sdk-mac_x86/platform-tools:$PATH
然后运行:
adb shell logcat > log.txt
OR第一招to adb platform-tools:
cd /Users/user/Android/Tools/android-sdk-macosx/platform-tools
然后运行
./adb shell logcat > log.txt
简单只需运行以下命令来获取输出到终端:
adb shell logcat
两个步骤:
- 生成日志
- 负载Gmail发送日志
。
-
生成日志
File generateLog() { File logFolder = new File(Environment.getExternalStorageDirectory(), "MyFolder"); if (!logFolder.exists()) { logFolder.mkdir(); } String filename = "myapp_log_" + new Date().getTime() + ".log"; File logFile = new File(logFolder, filename); try { String[] cmd = new String[] { "logcat", "-f", logFile.getAbsolutePath(), "-v", "time", "ActivityManager:W", "myapp:D" }; Runtime.getRuntime().exec(cmd); Toaster.shortDebug("Log generated to: " + filename); return logFile; } catch (IOException ioEx) { ioEx.printStackTrace(); } return null; }
-
载入Gmail的日志发送
File logFile = generateLog(); Intent intent = new Intent(Intent.ACTION_SEND); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(logFile)); intent.setType("multipart/"); startActivity(intent);
参考#1
~~
#2 - 有许多不同的答案在那里为如何加载日志文件查看和发送。最后,这里的解决方案实际工作既:
- 负载Gmail作为一个选项
- 附加文件成功
非常感谢https://stackoverflow.com/a/22367055/2162226的正常工作答案
这个命令行不适合我,我得到这个输出。 logcat读取:无效参数 – neoneye 2011-08-24 09:23:33
in win:adb.exe – Macarse 2011-08-24 11:38:58
请注意,logcollector不适用于4.1以上版本的Android,因为应用程序现在只允许读取自己的日志条目。 (https://groups.google.com/forum/#!topic/android-log-collector/vgYXYbg-O1U) – 2013-07-01 12:19:51