我想从设备上提取日志文件到我的电脑。如何操作?
Logcollector 是一个不错的选择,但需要先安装它。
当我想通过邮件发送日志文件时,通常会执行以下步骤:
adb shell logcat > log.txt
我希望这段代码能够帮助到别人。花了我两天时间才搞清楚如何从设备中记录日志并进行过滤:
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" />
。 - mehdok我会使用类似下面这样的东西:
$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。
点击此选项会准备一个 bug 报告,并提示您将其保存到驱动器或通过电子邮件发送。
我发现这是获取日志最简单的方法。我不喜欢开启 USB 调试。
编辑:
内部日志是内存中的循环缓冲区。实际上,每个缓冲区都有几个这样的循环缓冲区,分别为:无线电、事件、主要。默认为主要。
获取缓冲区副本的一种技术是在设备上执行命令并将输出作为字符串变量获取。
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
两步操作:
- 生成日志
- 加载Gmail发送日志
.
Generate the log
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;
}
Load Gmail to send the log
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://dev59.com/nm025IYBdhLWcg3wAxB9#22367055提供正确可行的答案。
adb
。通常在C:\Users\[你的用户名]\AppData\Local\Android\Sdk\platform-tools\
。 - lolololol ol