30

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.
Adam Matan
  • 107,447
  • 124
  • 346
  • 512
  • 1
    have you read this https://github.com/ACRA/acra? – deniz Sep 10 '14 at 12:37
  • Possible duplicate of [Send Logcat output of an App to an EmailAdress](http://stackoverflow.com/questions/2852181/send-logcat-output-of-an-app-to-an-emailadress) – avalancha Nov 04 '15 at 08:42

2 Answers2

34

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[] = {"yourmail@gmail.com"};
 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..."));
 }
dfrankow
  • 16,533
  • 35
  • 121
  • 177
  • 1
    Nice. I can even attach it to a button, so the user can just send me the logs when I ask for them. – Adam Matan Sep 10 '14 at 12:48
  • Sure, I will do it as soon as it works. Do you have any idea if it requires root or other special permissions? – Adam Matan Sep 10 '14 at 12:53
  • No, you don't need to root the device. – Aniruddh Rathore Sep 10 '14 at 17:31
  • 1
    Is this solution outdated? I am getting a fragment that pops up and says "No apps can perform this action." – Shadoninja Feb 07 '16 at 03:15
  • I added `emailIntent.setType("vnd.android.cursor.dir/email");` and it fixed my issue with "No apps can perform this action" – dequec64 Apr 13 '16 at 19:44
  • Can anybody comment on how safe/robust the `logcat -f` call is? It doesn't look very safe to me... – Vicky Chijwani Feb 02 '17 at 13:24
  • The chooser lets select between several Activities, even not related to email. To let select only email client it's possible to use setSelector with an Intent.ACTION_SENDTO Intent. Look here for the code: [link](http://stackoverflow.com/a/42856167/3257025) – ARLabs Mar 17 '17 at 11:24
  • 2
    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.) – vishvAs vAsuki Apr 13 '17 at 04:19
  • I needed to also add (besides manifest file): ` if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_LOGS) == PackageManager.PERMISSION_GRANTED) { Log.d(getLocalClassName(), "Got READ_LOGS permissions"); } else { Log.e(getLocalClassName(), "Don't have READ_LOGS permissions"); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_LOGS}, 103); Log.i(getLocalClassName(), "new READ_LOGS permission: " + ContextCompat.checkSelfPermission(this, Manifest.permission.READ_LOGS)); } ` – vishvAs vAsuki Apr 13 '17 at 15:07
  • @vishvAsvAsuki Hi, I'm not getting an attachment either. Where exactly does this code go? Can you maybe post another answer with the code all set out as need? It would be greatly appreciated. – AndyCr15 May 31 '17 at 16:25
  • 1
    @AndyCr15 Please see SendLoagcatMail in https://github.com/sanskrit-coders/stardict-dictionary-updater/blob/master/app/src/main/java/sanskritcode/sanskritdictionaryupdater/GetDictionariesActivity.java – vishvAs vAsuki May 31 '17 at 20:57
  • @vishvAsvAsuki `android.permission.READ_LOGS` is now granted only to system apps (see https://commonsware.com/blog/2012/07/12/read-logs-regression.html) – Vadim Kotov Jul 03 '19 at 15:20
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.

Community
  • 1
  • 1
Someone Somewhere
  • 22,369
  • 11
  • 111
  • 155