Write Android Logcat Data to a File

Android Writing Logs to text File

Hope this can help...

public void appendLog(String text)
{
File logFile = new File("sdcard/log.file");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Saving Logcat to a text file in Android Device

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:

/ExternalStorage/MyPersonalAppFolder/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.

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 + "/logs" );
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 and name of your application class 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" />

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

Edit:

if you want to save log of only some particular activities..

replace:

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

with:

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

Write android logcat data to a file

Here is an example of reading the logs.

You could change this to write to a file instead of to a TextView.

Need permission in AndroidManifest:

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

Code:

public class LogTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
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);
}
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) {
}
}
}

How to save LogCat contents to file?

Two ways to do what you want:

  1. Right-click in the logcat messages window, choose select all. Then your save will be all logcat messages in that window (including the ones scrolled out of view).
  2. When focus is in logcat messages window, type control-A, this will select all messages. Then save etc. etc.

Save Logcat from Android app to txt file

This worked for me it saves it in the main storage with this code:

Log.w("before","Logcat save");
try {
Process process = Runtime.getRuntime().exec("logcat -d");
process = Runtime.getRuntime().exec( "logcat -f " + "/storage/emulated/0/"+"Logging.txt");
}catch(Exception e)
{
e.printStackTrace();
}

enter image description here

enter image description here

Save android logcat in a file on Android device

if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

}
else{
Process process = Runtime.getRuntime().exec("logcat -f "+logFile);
}

Your code is working perfectly may be, by mistake, You had written ask for permission & make log file both together

Trying to save logcat to file in Android 11

private static final String APP_DIRECTORY = Environment.getExternalStorageDirectory() + "/MYAPPFOLDER";

You cannot create folders in root of external storage with sdk 30 devices.

Change to:

 private static final String APP_DIRECTORY = 
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/MYAPPFOLDER";


Related Topics



Leave a reply



Submit