'Saving Logcat to a text file in Android Device

I had found some crashes while running the application in android device, which is not showing in emulator. So i need to save the Logcat in a text file in my device's memory or SD card. Could you please suggest me good method to do this?



Solution 1:[1]

adb shell logcat -t 500 > D:\logcat_output.txt

Go onto your terminal/command prompt and navigate to the folder with adb in it, if its not already added to your environmental variables and paste this command.

t is the number lines you need to view

D:\logcat_output.txt is where your logcat will get stored.

Solution 2:[2]

Use -f option with logcat in your class:

Runtime.getRuntime().exec("logcat -f" + " /sdcard/Logcat.txt");

This will dump the logs to the file stored device.

Note that the path "/sdcard/" may not be available in all devices. You should use the standard APIs to access the external storage.

Solution 3:[3]

As I cannot comment yet, I`ll post this as an answer

I did as @HeisenBerg said, worked fine for me, but since from android 6.0 on we have to ask for permission at Run Time, I had to add the following:

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

And call

process = Runtime.getRuntime().exec("logcat -f " + logFile);

Only on the callback onRequestPermissionsResult

Solution 4:[4]

Apparently android.permission.READ_LOGS is only granted to system apps in latest versions of Android.

Solution 5:[5]

Add the manifest permission:

uses-permission android:name="android.permission.READ_LOGS" 


private static final String COMMAND = "logcat -d -v time";


public static void fetch(OutputStream out, boolean close) throws IOException {
    byte[] log = new byte[1024 * 2];
    InputStream in = null;
    try {
        Process proc = Runtime.getRuntime().exec(COMMAND);
        in = proc.getInputStream();
        int read = in.read(log);
        while (-1 != read) {
            out.write(log, 0, read);
            read = in.read(log);
        }
    }
    finally {
        if (null != in) {
            try {
                in.close();
            }
            catch (IOException e) {
                // ignore
            }
        }

        if (null != out) {
            try {
                out.flush();
                if (close)
                    out.close();
            }
            catch (IOException e) {
                // ignore
            }
        }
    }
}

public static void fetch(File file) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    fetch(fos, true);
}

Solution 6:[6]

If you only need to save the logcat (without your coding) you can use aLogrec or aLogcat applications from Google Play.

Google Play Store: aLogcat & aLogrec

Solution 7:[7]

I adjusted Drunken Daddy's answer to not need Permissions and migrated it to Kotlin.

Use an Application class at the beginning of your app. That allows a proper file and log handling.

Code below creates a log file at the following location:

/Android/data/com.your.app/files/logs/logcat_XXX.txt

XXX is the current time in milliseconds. Every time you run your app, a new logcat_XXX.txt file will be created.

import android.app.Application
import java.io.File
import java.io.IOException

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        getExternalFilesDir(null)?.let { publicAppDirectory -> // getExternalFilesDir don't need storage permission
            val logDirectory = File("${publicAppDirectory.absolutePath}/logs")
            if (!logDirectory.exists()) {
                logDirectory.mkdir()
            }

            val logFile = File(logDirectory, "logcat_" + System.currentTimeMillis() + ".txt")
            // clear the previous logcat and then write the new one to the file
            try {
                Runtime.getRuntime().exec("logcat -c")
                Runtime.getRuntime().exec("logcat -f $logFile")
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }
}

Set Application in AndroidManifest.xml:

<application
    android:name=".MyApplication"
    ... >

Solution 8:[8]

Drunken Daddy's answer is perfect. I would like to add though,

Environment.getExternalStorageDirectory()

is deprecated in API level 29 and Android Studio does not give you any warnings. Instead, you need to use

context.getExternalFilesDir(null);

which returns

/storage/emulated/0/Android/data/com.domain.myapp/files

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 smophos
Solution 2 Green goblin
Solution 3 Carlos Eduardo Millani
Solution 4 Pablo Valdes
Solution 5 d3n13d1
Solution 6 Florian Z.
Solution 7 ChrisWolfDev
Solution 8 David Buck