如何从 Android 设备获取日志文件?

我想把日志文件从一个设备拉到我的电脑。我怎样才能做到这一点?

309317 次浏览

编辑:

内部日志是内存中的循环缓冲区。实际上,每个缓冲区都有一些这样的循环缓冲区: 单选、事件、主缓冲区。默认值为 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

我会用这样的东西:

$adb logcat -d > logcat.txt

D 选项将整个循环缓冲区转储到文本文件中,如果您正在寻找特定的操作/意图,则尝试这样做

$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.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" />

多亏了 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){}

首先,通过将 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

我创建了一个小型图书馆(。Aar)通过电子邮件检索日志。你可以把它用在 Gmail 账户上。这很简单,但是很有效。你可以复印一份 从这里

该网站是在西班牙语,但有一个 PDF 与英文版本的产品说明。

希望能帮上忙。

简单,只需运行以下命令就可以将输出输入到您的终端:

adb shell logcat

对于那些不感兴趣的 USB 调试或使用 adb有一个更容易的解决方案。在 Android6(不确定是否是以前的版本)中,开发者工具下面有一个选项: 接受错误报告

单击此选项将准备错误报告,并提示您将其保存到驱动器或通过电子邮件发送。

我发现这是获取日志最简单的方法。我不喜欢打开 USB 调试。

两个步骤:

  • 生成日志
  • 载入 Gmail 发送日志

.

  1. 生成日志

    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;
    }
    
  2. 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);
    

References for #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/2162226 for the correctly working answer

我知道这是个老问题,但我相信即使在2018年仍然有效。

在每个机器人设备的 开发商选择中都有一个隐藏的 做个漏洞报告选项。

注意: 这会转储整个系统日志

如何启用开发人员选项? 参见: https://developer.android.com/studio/debug/dev-options

对我有效的方法:

  1. 重新启动设备(以便为开发人员创建要分析的最小垃圾日志)
  2. 复制你的虫子
  3. 进入设置-> 开发者选项-> 错误报告
  4. 等待 Android 系统收集日志(注意通知中的进度条)
  5. 一旦完成,点击通知来分享它(你可以使用 gmail 或其他任何方式)

怎么读这个? Open < strong > < em > 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