11

I'm currently developing an android app. I noticed a very rare error which leeds to a crash of my app. Unfortunately, I had my smartphone never connected to my pc when it occured. So, is there a way to automatically save all logs (and especially the thrown runtimeexceptions) to a file when my app starts, so that I can copy this file to my pc and analyse the error? The file should be overwritten on every start of my app, so that it contains only the logs of the last run... How can I achieve that?

regards

user2224350
  • 2,204
  • 3
  • 26
  • 50

4 Answers4

19

You can find help by following this link Writing crash reports into device sd card

You don't need to add external library.

import com.wordpress.doandroid.Training.R;

import android.app.Activity;
import android.os.Bundle;

public class CaptureExceptionActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Sets the default uncaught exception handler. This handler is invoked
        // in case any Thread dies due to an unhandled exception.
        Thread.setDefaultUncaughtExceptionHandler(new CustomizedExceptionHandler(
            "/mnt/sdcard/"));

        String nullString = null;
        System.out.println(nullString.toString());
        setContentView(R.layout.main);
    }
}

And the Handler implementation

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;
import android.util.Log;

public class CustomizedExceptionHandler implements UncaughtExceptionHandler {

    private UncaughtExceptionHandler defaultUEH;
    private String localPath;
    public CustomizedExceptionHandler(String localPath) {
        this.localPath = localPath;
        //Getting the the default exception handler
        //that's executed when uncaught exception terminates a thread
        this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    }

    public void uncaughtException(Thread t, Throwable e) {

        //Write a printable representation of this Throwable
        //The StringWriter gives the lock used to synchronize access to this writer.
        final Writer stringBuffSync = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(stringBuffSync);
        e.printStackTrace(printWriter);
        String stacktrace = stringBuffSync.toString();
        printWriter.close();

        if (localPath != null) {
            writeToFile(stacktrace);
        }

        //Used only to prevent from any code getting executed.
        // Not needed in this example
        defaultUEH.uncaughtException(t, e);
    }

    private void writeToFile(String currentStacktrace) {
        try {

            //Gets the Android external storage directory & Create new folder Crash_Reports
            File dir = new File(Environment.getExternalStorageDirectory(),
                "Crash_Reports");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy_MM_dd_HH_mm_ss");
            Date date = new Date();
            String filename = dateFormat.format(date) + ".STACKTRACE";

            // Write the file into the folder
            File reportFile = new File(dir, filename);
            FileWriter fileWriter = new FileWriter(reportFile);
            fileWriter.append(currentStacktrace);
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            Log.e("ExceptionHandler", e.getMessage());
        }
    }

}

Don't forget to add this permission in the manifest WRITE_EXTERNAL_STORAGE

Cililing
  • 3,455
  • 1
  • 13
  • 32
Hasina
  • 335
  • 3
  • 13
  • Awesome! But what is the `defaultUEH`, which is *Used only to prevent from any code getting executed*, and *Not needed in this example*? So where is it needed? – Acuna Dec 11 '17 at 16:00
  • You can put this statement as comment to know the effect. Normally, without this statement, your app will not crash. With this statement, it will crash, however you get the stacktrace before. – Hasina Dec 12 '17 at 18:38
1

Try to follow this question. Creating and Storing Log File on device in Android

The last answer is pretty useful.

Community
  • 1
  • 1
thestar
  • 4,625
  • 2
  • 26
  • 22
1

I'm going with the solution of the second answer in this post here ( by rrainn): How do I obtain crash-data from my Android application?

Community
  • 1
  • 1
user2224350
  • 2,204
  • 3
  • 26
  • 50
0

You might try the ACRA (Application Crash Report for Android) library:

ACRA is a library enabling Android Application to automatically post their crash reports to a GoogleDoc form. It is targetted to android applications developers to help them get data from their applications when they crash or behave erroneously.

It's easy to install in your app, highly configurable and don't require you to host a server script anywhere... reports are sent to a Google Doc spreadsheet !

Thanks

Md Abdul Gafur
  • 6,169
  • 2
  • 26
  • 37
  • "Since the recent update of Google Forms by Google, the usage of Google Docs as a storage engine for ACRA reports is now deprecated." They launched a tool called Acralyzer but - if I understood it right- it needs a server. Unfortunately, I'm totally unfamiliar with this server-stuff. I think that answer won't work for me personally. tanks anyway :) – user2224350 Dec 25 '13 at 13:30