Saving Logcat to a text file on an Android device

I found some crashes when starting the application on an Android device that does not appear in the emulator. Therefore, I need to save Logcat in a text file in the device memory or on an SD card. Could you suggest me a good way to do this?

+15
java android eclipse android-sdcard logcat
source share
7 answers

use the application class at the beginning of your application. This allows you to process files and logs. Here is an example. This piece of code adds a new folder named "MyPersonalAppFolder" with another folder named "log" to it in the public external storage. then the logcat output is cleared, and the new logcat output is written to a new file called logcatXXX.txt, where XXX is the millisecond time at the moment.

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

you need the correct permissions in your .manifest file:

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

Now run the application and go to "/ your external storage / MyPersonalAppFolder / logs /"

You will find the log file here.

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

Edit:

if you want to keep a log of only certain actions.

replace:

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

from:

 process = Runtime.getRuntime().exec( "logcat -f " + logFile + " *:S MyActivity:D MyActivity2:D"); 
+51
source share
 adb shell logcat -t 500 > D:\logcat_output.txt 

Go to your terminal / command line command and navigate to the folder using adb in it if it is not already added to your environment variables and inserts this command.

t - numeric lines to be viewed

D: \ logcat_output.txt where your logarithm will be saved.

+12
source share

Use the -f option with logcat in your class:

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

This will upload the logs to the saved file.

Please note that the path "/ sdcard /" may not be available on all devices. You must use standard APIs to access external storage .

+10
source share

While I can’t comment, I will post this as an answer

I did as @HeisenBerg said, everything worked fine for me, but starting with Android 6.0 and above, we need to request permission at runtime, 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 onRequestPermissionsResult

+6
source share

Add 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); } 
+2
source share

If you need to save logcat (without encoding), you can use aLogrec or aLogcat from Google Play.

Google Play Store: aLogcat and aLogrec

+1
source share

Obviously, android.permission.READ_LOGS is only provided to system applications in the latest versions of Android.

0
source share

All Articles