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


129

我想将日志文件从设备拉到我的PC。我怎样才能做到这一点?

Answers:


112

Logcollector是一个不错的选择,但您需要先安装它。

当我想通过邮件发送日志文件时,通常执行以下操作:


此命令行不适用于我,我得到此输出。logcat读取:无效的参数
neoneye'Aug

5
请注意,logcollector不适用于4.1以上的android版本,因为现在仅允许应用程序读取其自己的日志条目。(groups.google.com/forum/#!topic/android-log-collector/...
马修洛韦

11
是否没有任何简单的方法,通过USB将其作为外部存储设备通过设备从设备中拉出日志文件,而无需安装adb或类似的工具?
或Mapper

确保您在正确的目录中运行adb。通常在C:\Users\[your username]\AppData\Local\Android\Sdk\platform-tools\
lolololol ol '18

30

我希望这段代码可以对某人有所帮助。我花了两天的时间弄清楚如何从设备登录,然后对其进行过滤:

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" />

1
这可以正常工作,但可以在您需要的每台设备上工作<uses-permission android:name="android.permission.READ_LOGS" />
mehdok

27

我会用这种东西:

$adb logcat -d > logcat.txt

-d选项将整个循环缓冲区转储到文本文件中,如果您正在寻找特定的动作/意图,请尝试

$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt

希望这可以帮助 :)


13

对于那些对USB调试或使用不感兴趣的人,adb有一个更简单的解决方案。在Android 6(不确定先前版本)中,开发人员工具下有一个选项:记录错误报告

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

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


1
正是我想要的!
YYamil

1
我在哪里可以找到它?
gurehbgui

11

编辑:

内部日志是内存中的循环缓冲区。实际上,每个广播都有一些这样的循环缓冲区:无线电,事件,主要。默认值为main。

为了获得缓冲区的副本,一种技术涉及在设备上执行命令并获得输出作为字符串变量。

SendLog是一个开源应用程序,它可以执行以下操作: http

关键是要logcat在嵌入式OS的设备上运行。它并不像听起来那么难,只需查看链接中的开源应用即可。


这会将输出转储到屏幕上,我需要一个文件。
Pentium10

您可以使用aLogCat发送日志。或者只是从DDMS复制粘贴:P
molnarm,2010年

@molnarm我承认,如果那是他需要做的,那么我的回答会有点过分设计=)
Brad Hein 2010年

@BradHein您说SendLog是开源的,但我似乎找不到源代码。知道在哪里吗?
smith324 2012年

8

我经常收到错误“ logcat read: Invalid argument”。从日志读取之前,我必须清除日志。

我喜欢这样:

prompt> cd ~/Desktop
prompt> adb logcat -c
prompt> adb logcat | tee log.txt

谢谢!我已经有很长时间了,那个logcat -c立即修复了它!
乔德斯,2012年

4

一种简单的方法是制作您自己的日志收集器方法,或者只是制作市场上现有的日志收集器应用程序。

对于我的应用程序,我做了报告功能,可以将日志发送到我的电子邮件(甚至发送到另一个地方-一旦获得日志,就可以随心所欲地进行操作)。

这是有关如何从设备获取日志文件的简单示例:



3

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

还有一种选择以一个错误报告隐藏在开发者选择在每一个Android设备。

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

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

什么对我有用:

  1. 重新启动设备(以便创建最少的垃圾日志供开发人员分析)
  2. 重现您的错误
  3. 转到设置->开发人员选项->提交错误报告
  4. 等待Android系统收集日志(观看通知中的进度条)
  5. 完成后,点击通知即可共享(您可以使用gmail或其他)

怎么看?打开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

2

多亏了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
Youngjae 2014年

2

我创建了一个小型库(.aar),以通过电子邮件检索日志。您可以将其用于Gmail帐户。这很简单,但是可以。您可以从这里获取副本

该网站是西班牙文,但是有一个PDF,其中包含产品说明的英文版。

希望对您有所帮助。


2

两步:

  • 生成日志
  • 加载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. 加载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作为一种选择
  • 成功附加文件

非常感谢https://stackoverflow.com/a/22367055/2162226的正确答案


0

首先通过将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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.