如何从 Android 设备获取日志文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2882253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the logfile from an Android device?
提问by Pentium10
I would like to pull the log file from a device to my PC. How can I do that?
我想将日志文件从设备拉到我的 PC。我怎样才能做到这一点?
采纳答案by Macarse
Logcollectoris a good option but you need to install it first.
Logcollector是一个不错的选择,但您需要先安装它。
When I want to get the logfile to send by mail, I usually do the following:
当我想通过邮件发送日志文件时,我通常会执行以下操作:
- connect the device to the pc.
- Check that I already setup my os for that particular device.
- Open a terminal
- Run
adb shell logcat > log.txt
- 将设备连接到电脑。
- 检查我是否已经为该特定设备设置了操作系统。
- 打开终端
- 跑
adb shell logcat > log.txt
回答by user1354692
I hope this code will help someone. It took me 2 days to figure out how to log from device, and then filter it:
我希望这段代码会对某人有所帮助。我花了 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;
}
as pointed by @mehdok
正如@mehdok 所指出的
add the permission to the manifest for reading logs
将权限添加到清单以读取日志
<uses-permission android:name="android.permission.READ_LOGS" />
回答by Rohit
I would use something of this sort :
我会使用这样的东西:
$adb logcat -d > logcat.txt
The -d option dumps the entire circular buffer into the text file and if you are looking for a particular action/intent try
-d 选项将整个循环缓冲区转储到文本文件中,如果您正在寻找特定的操作/意图,请尝试
$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt
Hope this helps :)
希望这可以帮助 :)
回答by jjhavokk
For those not interested in USB debugging or using adb
there is an easier solution. In Android 6 (Not sure about prior version) there is an option under developer tools: Take Bug Report
对于那些对 USB 调试或使用不感兴趣的人,adb
有一个更简单的解决方案。在 Android 6(不确定之前的版本)中,开发者工具下有一个选项:Take Bug Report
Clicking this option will prepare a bug report and prompt you to save it to drive or have it sent in email.
单击此选项将准备错误报告并提示您将其保存到驱动器或通过电子邮件发送。
I found this to be the easiest way to get logs. I don't like to turn on USB debugging.
我发现这是获取日志的最简单方法。我不喜欢打开USB调试。
回答by Brad Hein
EDIT:
编辑:
The internal log is a circular buffer in memory. There are actually a few such circular buffers for each of: radio, events, main. The default is main.
内部日志是内存中的循环缓冲区。实际上每个循环缓冲区都有几个这样的循环缓冲区:radio、events、main。默认是主要的。
To obtain a copy of a buffer, one technique involves executing a command on the device and obtaining the output as a string variable.
要获取缓冲区的副本,一种技术涉及在设备上执行命令并获取作为字符串变量的输出。
SendLog is an open source App which does just this: http://www.l6n.org/android/sendlog.shtml
SendLog 是一个开源应用程序,它就是这样做的:http://www.l6n.org/android/sendlog.shtml
The key is to run logcat
on the device in the embedded OS. It's not as hard as it sounds, just check out the open source app in the link.
关键是要logcat
在嵌入式操作系统的设备上运行。这并不像听起来那么难,只需查看链接中的开源应用程序即可。
回答by neoneye
Often I get the error "logcat read: Invalid argument
". I had to clear the log, before reading from the log.
我经常收到错误“ logcat read: Invalid argument
”。在从日志中读取之前,我必须清除日志。
I do like this:
我喜欢这样:
prompt> cd ~/Desktop
prompt> adb logcat -c
prompt> adb logcat | tee log.txt
回答by Moss
A simple way is to make your own log collector methods or even just an existing log collector app from the market.
一种简单的方法是制作自己的日志收集器方法,甚至只是市场上现有的日志收集器应用程序。
For my apps I made a report functionality which sends the logs to my email (or even to another place - once you get the log you can do whether you want with it).
对于我的应用程序,我制作了一个报告功能,将日志发送到我的电子邮件(甚至发送到另一个地方 - 一旦你得到日志,你可以随意使用它)。
Here is a simple example about how to get the log file from a device:
这是一个关于如何从设备获取日志文件的简单示例:
回答by Weston Ganger
Simple just run the following command to get the output to your terminal:
简单只需运行以下命令即可将输出发送到您的终端:
adb shell logcat
回答by Piotr Z
I know it's an old question, but I believe still valid even in 2018.
我知道这是一个老问题,但我相信即使在 2018 年仍然有效。
There is an option to Take a bug reporthidden in Developer optionsin every android device.
还有一种选择以一个错误报告隐藏在开发者选择在每一个Android设备。
NOTE: This would dump whole system log
注意:这将转储整个系统日志
How to enable developer options? see: https://developer.android.com/studio/debug/dev-options
如何开启开发者选项?请参阅:https: //developer.android.com/studio/debug/dev-options
What works for me:
什么对我有用:
- Restart your device (in order to create minimum garbage logs for developer to analyze)
- Reproduce your bug
- Go to Settings -> Developer options -> Take a bug report
- Wait for Android system to collect the logs (watch the progressbar in notification)
- Once it completes, tap the notification to share it (you can use gmail or whetever else)
- 重新启动您的设备(以创建最少的垃圾日志供开发人员分析)
- 重现你的错误
- 转到设置 -> 开发人员选项 -> 提交错误报告
- 等待Android系统收集日志(在通知中查看进度条)
- 完成后,点击通知进行分享(您可以使用 gmail 或其他任何方式)
how to read this? open bugreport-1960-01-01-hh-mm-ss.txt
怎么读这个?打开bugreport-1960-01-01-hh-mm-ss.txt
you probably want to look for something like this:
你可能想要寻找这样的东西:
------ 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
or:
或者:
------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of main
回答by Gene Bo
Two steps:
- Generate the log
- Load Gmail to send the log
两个步骤:
- 生成日志
- 加载 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);
生成日志
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);
References for #1
#1 的参考
~~
~~
For #2 - there are many different answers out there for how to load the log file to view and send. Finally, the solution here actually worked to both:
- load Gmail as an option
- attaches the file successfully
Big thanks to https://stackoverflow.com/a/22367055/2162226for the correctly working answer
对于#2 - 关于如何加载日志文件以查看和发送,有许多不同的答案。最后,这里的解决方案实际上对两者都有效:
- 加载 Gmail 作为选项
- 成功附加文件
非常感谢https://stackoverflow.com/a/22367055/2162226的正确工作答案