【发布时间】:2010-05-21 13:04:52
【问题描述】:
我想将日志文件从设备提取到我的 PC。我该怎么做?
【问题讨论】:
标签: android
我想将日志文件从设备提取到我的 PC。我该怎么做?
【问题讨论】:
标签: android
Logcollector 是一个不错的选择,但您需要先安装它。
当我想获取日志文件以通过邮件发送时,我通常会执行以下操作:
adb shell logcat > log.txt
【讨论】:
adb。通常在C:\Users\[your username]\AppData\Local\Android\Sdk\platform-tools\
我希望这段代码对某人有所帮助。我花了 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" />
【讨论】:
<uses-permission android:name="android.permission.READ_LOGS" />
我会使用这种东西:
$adb logcat -d > logcat.txt
-d 选项将整个循环缓冲区转储到文本文件中,如果您正在寻找特定的操作/意图,请尝试
$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt
希望这会有所帮助:)
【讨论】:
对于那些对 USB 调试或使用 adb 不感兴趣的人,有一个更简单的解决方案。在 Android 6(不确定以前的版本)中,开发者工具下有一个选项:Take Bug Report
单击此选项将准备错误报告并提示您将其保存到驱动器或通过电子邮件发送。
我发现这是获取日志的最简单方法。我不喜欢打开 USB 调试。
【讨论】:
编辑:
内部日志是内存中的一个循环缓冲区。实际上每个都有一些这样的循环缓冲区:广播、事件、主缓冲区。默认为 main。
要获取缓冲区的副本,一种技术是在设备上执行命令并以字符串变量的形式获取输出。
SendLog 是一个开源应用程序,它就是这样做的:http://www.l6n.org/android/sendlog.shtml
关键是在嵌入式操作系统的设备上运行logcat。这并不像听起来那么难,只需查看链接中的开源应用程序即可。
【讨论】:
我经常收到错误“logcat read: Invalid argument”。在读取日志之前,我必须清除日志。
我喜欢这样:
prompt> cd ~/Desktop
prompt> adb logcat -c
prompt> adb logcat | tee log.txt
【讨论】:
我知道这是一个老问题,但我相信即使在 2018 年仍然有效。
每台 Android 设备的开发者选项中都有一个获取错误报告选项。
注意:这会转储整个系统日志
如何启用开发者选项?见:https://developer.android.com/studio/debug/dev-options
什么对我有用:
如何阅读? 打开 bugreport-1960-01-01-hh-mm-ss.txt
你可能想寻找这样的东西:
------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of crash
06-13 14:37:36.542 19294 19294 E AndroidRuntime: FATAL EXCEPTION: main
或:
------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of main
【讨论】:
一种简单的方法是制作自己的日志收集器方法,甚至只是市场上现有的日志收集器应用程序。
对于我的应用程序,我创建了一个报告功能,可以将日志发送到我的电子邮件(甚至发送到另一个地方 - 一旦你得到日志,你可以随心所欲地处理它)。
这是一个关于如何从设备获取日志文件的简单示例:
【讨论】:
只需运行以下命令即可将输出发送到终端:
adb shell logcat
【讨论】:
感谢 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?
我创建了一个小型库 (.aar) 来通过电子邮件检索日志。您可以将其与 Gmail 帐户一起使用。这很简单,但很有效。你可以得到一份from here
该网站是西班牙语的,但有一个带有英文版产品说明的 PDF。
我希望它可以帮助。
【讨论】:
两步:
- 生成日志
- 加载 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 作为选项
- 成功附加文件
【讨论】:
首先通过将 PATH 设置为 android sdk platform-tools 来确保 adb 命令可执行:
export PATH=/Users/espireinfolabs/Desktop/soft/android-sdk-mac_x86/platform-tools:$PATH
然后运行:
adb shell logcat > log.txt
或首先移至 adb 平台工具:
cd /Users/user/Android/Tools/android-sdk-macosx/platform-tools
然后运行
./adb shell logcat > log.txt
【讨论】: