33

Scenario

I have released a beta version of an Android app to a few friends. Now, I would like to fix some bugs that came up during the test period.

I have set a third-party crash reports utility, so I can easily handle app crashes. However, there are some erroneous behaviours which are not causing crashes. In these cases, I would like to inspect the app logs and see what went wrong.

Is there a way for the app to send its logcat entries via email?

Clarifications

  • There are many logging apps (android-log-collector, Log Viewer (logcat)) which can inspect and show logcat entries. However, these apps can't access other apps' logs since Android 4.1.
  • I don't mind taking a lot of space in the device - this feature is for beta testers only.
  • The solution should work without root or any other special permissions.
2

3 Answers 3

37

Call this method in onDestroy of your main activity.

 public void SendLogcatMail(){

    // save logcat in file
    File outputFile = new File(Environment.getExternalStorageDirectory(),
            "logcat.txt");
    try {
        Runtime.getRuntime().exec(
                "logcat -f " + outputFile.getAbsolutePath());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

 //send file using email
 Intent emailIntent = new Intent(Intent.ACTION_SEND);
 // Set type to "email"
 emailIntent.setType("vnd.android.cursor.dir/email");
 String to[] = {"[email protected]"};
 emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
 // the attachment
 emailIntent .putExtra(Intent.EXTRA_STREAM, outputFile.getAbsolutePath());
 // the mail subject
 emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
 startActivity(Intent.createChooser(emailIntent , "Send email..."));
 }
Sign up to request clarification or add additional context in comments.

12 Comments

Nice. I can even attach it to a button, so the user can just send me the logs when I ask for them.
Sure, I will do it as soon as it works. Do you have any idea if it requires root or other special permissions?
Is this solution outdated? I am getting a fragment that pops up and says "No apps can perform this action."
That can be fixed with emailIntent .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile)); . But then, the attachment turns out to be empty (or so the gmail app tells me.)
|
0

It sounds like RemoteLogger is exactly what you need

https://github.com/sschendel/RemoteLogger

Capture debug logging to a file that user can easily send to you via email. This was created out of necessity to troubleshoot a non-reproducible bug reported by a user. To be clear the logging was dropped into a version of app released to test users to troubleshoot. Removed in final production code.

Library provides hooks to start, stop, send, and delete log file.

1 Comment

Are you sure it can send logcat logs? Looks like you should add log entries explicitly
0

You can use logging libraries such as Timber, Android-Logback to log into file. Then send it with e-mail. Then you will have history of your logs which will help you to find bugs.

Use android-logback which is an android logging framework. Log into a file for a specific location with time and file size based rolling policies, which means every hour or when file size limit exceeded a new log file is created. Also you define expiration date and maximum total size for logs. If log date expired or maximum total file is exceeded logs will be deleted. Also bind logcat to android-logback logger that I created. Then show list of logs to user in a recyclerview then send it when user selects the specific log since logs are named with their dates.

Be careful about file storing policy, file sharing policy and file path to store, if you add this for your product release to store.

You can see how I implemented and logics in my following video. https://www.youtube.com/watch?v=0Evjd5c_x5E

You can just check implementation and copy the android library named filelogger from following github link https://github.com/b-basoglu/simpleproject/tree/video2-file-logging

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.