在安卓系统中保存LogCat数据

6
我想在Android中将所有Log Cat的内容保存到特定文件中。我使用Eclipse IDE开发Android应用程序。
我该如何实现这一点?
谢谢。
5个回答

10

以下是一种以编程方式获取 Logcat 内容的方法。

 Process process = Runtime.getRuntime().exec("logcat -d");

 BufferedReader bufferedReader = new BufferedReader(
     new InputStreamReader(process.getInputStream()));

 StringBuilder log=new StringBuilder();
 String line = "";
 while ((line = bufferedReader.readLine()) != null) {
     log.append(line);
 }

查看此链接以获取更多详细信息。


7
在logcat选项卡中,选择所有行。然后在右上角点击向下的小三角形,称为“查看菜单”,并选择“将选择导出为文本...”。

4
I want to save all the contents of log cat into specific file in Android.

LogCat文件以循环内存缓冲区的形式存储在设备上。

如果您在主机系统上运行“adb logcat > myfile”,则可以将内容检索到文件中。

请参见: https://sites.google.com/site/androidhowto/how-to-1/save-logcat-to-a-text-file

提取LogCat的其他方法:

  1. @Blundell的方式:

    选择要保存的LogCat行,然后只需按Ctrl + C(复制),然后在任何文本文件中使用Ctrl + V(粘贴)即可。

  2. @Niek的方式:

    在LogCat选项卡中,选择要保存的行。然后在右上角,单击名为“View Menu”的向下小三角形,选择“Export Selection as text ..” 它会询问您要保存LogCat文件的位置。


0

虽然@Kartik向您展示了正确的方法。

为了快捷起见,您可以复制并粘贴。 确保logcat视图已打开Eclipse> DDMS> logcat

然后单击要开始的行

按住Shift键(进行多行选择)

单击要完成的行

然后只需ctrl+c(复制)

ctrl+v(粘贴到任何位置)


0

我的方法允许在设备未连接到ADB时,在测试情况下获取日志

只需创建LogWrapper的克隆, 并在构造函数中创建外部存储器中的文件。 我选择DIRECTORY_PICTURES,因为它存在于所有设备上。 构造函数 public LogWrapperExt(){ sdf = new SimpleDateFormat("\n hh:mm:ss.SSS"); String path; isValid = true; boolean isPresent; try { topDirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if(topDirPath != null ) path = topDirPath.getCanonicalPath();

    } catch (java.io.IOException e) {
        isValid = false;
    } catch (java.lang.NoSuchFieldError e) {
        isValid = false;
    }
    if(isValid){
        logsFolder =   new File(topDirPath + File.separator + "MyLogs");// put your name
        if (!logsFolder.exists()) {
            isPresent = logsFolder.mkdir();
        }
        try {
            String formattedDate = new Date().toString() ;  
            formattedDate = formattedDate.replaceAll(" ", "_");
            String fileName = "test_"+ formattedDate + ".txt";
            logFile = new File(logsFolder.getAbsolutePath(),fileName);
            outputStreamLogFile = new FileOutputStream(logFile);
        } catch (java.io.FileNotFoundException e) {

        }
    }
}

然后在public void println中,也让你的打印输出。

    Log.println(priority, tag, useMsg);

    String myp =  dateStr + " " + level_name[priority] + "  " + tag + " " + useMsg;
    try{
        outputStreamLogFile.write(myp.getBytes());
    } catch (java.io.IOException e) {

    }

有一些额外的开销 - 是的,但在所有情况下都是你的数据

别忘了将 LogWrapper 名称替换为你自己的


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接