将Logcat保存到Android设备中的文本文件

Nit*_*ael 24 java eclipse android logcat android-sdcard

我在Android设备中运行应用程序时发现了一些崩溃,这在模拟器中没有显示.所以我需要将Logcat保存在设备内存或SD卡的文本文件中.你能建议我这样做的好方法吗?

Hei*_*erg 72

在应用程序的开头使用Application类.这允许适当的文件和日志处理.这是一个例子.这段代码将一个名为"MyPersonalAppFolder"的新文件夹与另一个名为"log"的文件夹添加到公共外部存储中.之后,清除logcat输出并将新的logcat输出写入名为logcatXXX.txt的新文件中,其中XXX是此时的毫秒时间.

public class MyPersonalApp extends Application {

    /**
     * Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
     */
    public void onCreate() {
        super.onCreate();

        if ( isExternalStorageWritable() ) {

            File appDirectory = new File( Environment.getExternalStorageDirectory() + "/MyPersonalAppFolder" );
            File logDirectory = new File( appDirectory + "/log" );
            File logFile = new File( logDirectory, "logcat" + System.currentTimeMillis() + ".txt" );

            // create app folder
            if ( !appDirectory.exists() ) {
                appDirectory.mkdir();
            }

            // create log folder
            if ( !logDirectory.exists() ) {
                logDirectory.mkdir();
            }

            // clear the previous logcat and then write the new one to the file
            try {
                Process process = Runtime.getRuntime().exec("logcat -c");
                process = Runtime.getRuntime().exec("logcat -f " + logFile);
            } catch ( IOException e ) {
                e.printStackTrace();
            }

        } else if ( isExternalStorageReadable() ) {
            // only readable
        } else {
            // not accessible
        }
    }

    /* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    public boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if ( Environment.MEDIA_MOUNTED.equals( state ) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) ) {
            return true;
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

您需要.manifest文件中的正确权限:

<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:name=".MyPersonalApp"
    ... >
Run Code Online (Sandbox Code Playgroud)

现在运行您的应用程序并转到"/您的外部存储/ MyPersonalAppFolder/logs /"

你会在那里找到日志文件.

来源:http://www.journal.deviantdev.com/android-log-logcat-to-file-while-runtime/

编辑:

如果你想保存一些特定活动的日志..

更换:

process = Runtime.getRuntime().exec("logcat -f " + logFile);
Run Code Online (Sandbox Code Playgroud)

有:

process = Runtime.getRuntime().exec( "logcat -f " + logFile + " *:S MyActivity:D MyActivity2:D");
Run Code Online (Sandbox Code Playgroud)

  • 你如何阻止logcat写作?或者在调用onDestroy()时它会停止吗? (3认同)
  • 此代码特定于您的应用程序.只要您的应用程序正在运行,它就会写入日志. (2认同)
  • 使用标志 `-d` 转储日志而不是连续流式传输它。 (2认同)

小智 14

adb shell logcat -t 500 > D:\logcat_output.txt
Run Code Online (Sandbox Code Playgroud)

转到终端/命令提示符并导航到包含adb的文件夹(如果尚未添加到环境变量中)并粘贴此命令.

t是您需要查看的数字行

D:\ logcat_output.txt是您的logcat将被存储的位置.

  • 我发现这个答案实际上被问题的作者所接受,这一点非常值得注意! (10认同)
  • 当你的设备没有连接到我的电脑时,你可以给我的设备写一个解决方案吗? (7认同)
  • @smophos - 海报想要启动它,断开电缆,让它继续录制. (4认同)

Gre*_*lin 10

在类中使用-cat选项和logcat:

Runtime.getRuntime().exec("logcat -f" + " /sdcard/Logcat.txt");
Run Code Online (Sandbox Code Playgroud)

这会将日志转储到文件存储设备.

请注意,路径"/ sdcard /"可能并非在所有设备中都可用.您应该使用标准API来访问外部存储.


小智 6

由于我还没有评论,我会发布这个作为答案

我做了@HeisenBerg说,对我来说很好,但是从Android 6.0开始我们必须在运行时请求许可,我不得不添加以下内容:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }
}
Run Code Online (Sandbox Code Playgroud)

并致电

process = Runtime.getRuntime().exec("logcat -f " + logFile);
Run Code Online (Sandbox Code Playgroud)

仅在回调上 onRequestPermissionsResult


Pab*_*des 5

显然android.permission.READ_LOGS仅授予最新版本 Android 中的系统应用程序。