Android Studio查看整个LogCat日志

时间:2018-03-15 11:02:13

标签: java android android-studio logcat

有没有办法看到完整的logcat日志?我的应用程序中有错误但我没有时间查看它,因为错误后出现了大量文本并超过了logcat中的行限制。所以即使我走到顶端,错误也已经消失了。

3 个答案:

答案 0 :(得分:3)

您应该在Android Studio中使用过滤器。以下是您的一些选择:

  1. 仅将输出限制在您的应用中:
  2. enter image description here

    1. 仅将输出限制为错误消息:
    2. enter image description here

      1. 将自定义TAG添加到引发错误的位置并按此过滤:

        Content={Binding Text}

      2. 此外,如果您不希望自动滚动logcat,只需在中间选择一段输出(或向上滚动)。

答案 1 :(得分:1)

好的,所以当我做一些测试时,我找到了解决方案。我通过选择这样做它只显示错误,这样我就看不到其他消息了。

编辑:

要更改为错误模式,请选择屏幕底部的LogCat(或按Alt + 6),然后在第三个下拉菜单中选择“错误”选项。

enter image description here

答案 2 :(得分:0)

这是另一个存储logcat详细信息以用于测试目的的想法 请尝试下面的代码

public static void extractLogToFile() {

    PackageManager manager = getContext().getPackageManager();
    PackageInfo info = null;
    try {
        info = manager.getPackageInfo(getContext().getPackageName(), 0);
    } catch (NameNotFoundException e2) {
    }
    String model = Build.MODEL;
    if (!model.startsWith(Build.MANUFACTURER))
        model = Build.MANUFACTURER + " " + model;

    String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "your App name" + File.separator;
    File folder = new File(filePath);
    if (!folder.exists()) {
        folder.mkdir();
    }
    String fileName = "Logcat-" + (new SimpleDateFormat("dd-MM-yyyy").format(Calendar.getInstance().getTime())) + ".txt";
    File file = new File(folder, fileName);

    InputStreamReader reader = null;
    FileWriter writer = null;

    try {
        // For Android 4.0 and earlier, you will get all app's log output,
        // so filter it to
        // mostly limit it to your app's output. In later versions, the
        // filtering isn't needed.
        String cmd = (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) ? "logcat -d -v time MyApp:v dalvikvm:v System.err:v *:s" : "logcat -d -v time";

        // get input stream
        Process process = Runtime.getRuntime().exec(cmd);
        reader = new InputStreamReader(process.getInputStream());

        // write output stream
        writer = new FileWriter(file, true);
        writer.write("Android version: " + Build.VERSION.SDK_INT + "\n");
        writer.write("Device: " + model + "\n");
        writer.write("App version: " + (info == null ? "(null)" : info.versionCode) + "\n");

        char[] buffer = new char[10000];
        do {
            int n = reader.read(buffer, 0, buffer.length);
            if (n == -1)
                break;
            writer.write(buffer, 0, n);
        } while (true);

        reader.close();
        writer.close();
    } catch (IOException e) {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e1) {
            }
        if (reader != null)
            try {
                reader.close();
            } catch (IOException e1) {
            }
    }
}

希望这会有用

通过此代码,您可以将logcat详细信息存储在手机存储中。 确保清单文件中提到的存储权限以及实现运行时权限。