Answers:
Logcollector是一个不错的选择,但您需要先安装它。
当我想通过邮件发送日志文件时,通常执行以下操作:
adb shell logcat > log.txtadb。通常在C:\Users\[your username]\AppData\Local\Android\Sdk\platform-tools\
我希望这段代码可以对某人有所帮助。我花了两天的时间弄清楚如何从设备登录,然后对其进行过滤:
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" />
编辑:
内部日志是内存中的循环缓冲区。实际上,每个广播都有一些这样的循环缓冲区:无线电,事件,主要。默认值为main。
为了获得缓冲区的副本,一种技术涉及在设备上执行命令并获得输出作为字符串变量。
SendLog是一个开源应用程序,它可以执行以下操作: http
关键是要logcat在嵌入式OS的设备上运行。它并不像听起来那么难,只需查看链接中的开源应用即可。
一种简单的方法是制作您自己的日志收集器方法,或者只是制作市场上现有的日志收集器应用程序。
对于我的应用程序,我做了报告功能,可以将日志发送到我的电子邮件(甚至发送到另一个地方-一旦获得日志,就可以随心所欲地进行操作)。
这是有关如何从设备获取日志文件的简单示例:
我知道这是一个古老的问题,但我相信即使在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
多亏了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?
两步:
- 生成日志
- 加载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