Send Logcat output of an App to an EmailAdress

2019-01-09 06:18发布

We are now testing our application with a few friends. Sometimes there are some errors which don't throw an exception. So I don't really know whats the problem was. So I thought it would be a good idea to implement a menu item which allows to send the logcat output to a e-mail address, so that we can examine the log.

Unfortunately I didn't find a hint in the Internet how to extract the logcat from a phone. How to send an email shouldn't be the problem.

7条回答
Bombasti
2楼-- · 2019-01-09 06:28

Look at android-log-collector, as it does what you are trying to do.

It is not possible to collect arbitrary LogCat data as of Android 4.1. There was never a documented and supported way of doing that, and the undocumented/unsupported way was locked down in Jelly Bean. For your own crashes, you are better served using a crash logging library, like ACRA.

查看更多
等我变得足够好
3楼-- · 2019-01-09 06:29

I'd definitely recommend to look also at this project here

查看更多
我只想做你的唯一
4楼-- · 2019-01-09 06:30

I found the LogCollector very usefull indeed (the tip from CommonsWare):

And don't forget to set in your own application:

<uses-permission android:name="android.permission.READ_LOGS" />
查看更多
仙女界的扛把子
5楼-- · 2019-01-09 06:35

I would also look into Flurry (flurry.com) which not only gives you general analytics but allows you to log arbitrary info and also logs uncaught exceptions for you. I set it up in literally 5 minutes, but one thing to keep in mind is that it's not real-time like an email alert. You'll have to wait a few hours for what you log in your app to show up on their dashboard. It could also be overkill if you have a really lightweight app, but I've noticed no performance loss in my app as a result of using the service.

查看更多
你好瞎i
6楼-- · 2019-01-09 06:41

In your manifest file give the following permissions:

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

In your first/launcher activity, right after the

super.onCreate(savedInstanceState);

Write below lines: This will write your App's logcat to your device's external storage

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

    if ( isExternalStorageWritable() ) {

        File appDirectory = new File( Environment.getExternalStorageDirectory() + "/MyAppLog" );
        File logDirectory = new File( appDirectory + "/log" );
        File logFile = new File( logDirectory, "logcat" + ".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
        if ( logFile.exists()){
            logFile.delete();
        }

        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
    }

For sending the logcat to desired email address: use below method

public void sendLogcatMail(){

    if ( isExternalStorageWritable() ) {

        File appDirectory = new File(Environment.getExternalStorageDirectory() + "/MyAppLog");
        File logDirectory = new File(appDirectory + "/log");
        File logFile = new File(logDirectory, "logcat" + ".txt");

        if (logFile.exists()) {
            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.setType("vnd.android.cursor.dir/email");
            String to[] = {"yourEmailAddress@gmail.com"};
            emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
            emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(String.valueOf(logFile.toURI())));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Log files");
            emailIntent.putExtra(Intent.EXTRA_TEXT, "Send some message along");
            startActivity(Intent.createChooser(emailIntent, "Send email..."));
        }
    }
}

Method for checking whether the permissions for Writing to External storage is given or not:

/* Checks if external storage is available for read and write */

public static boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
        return true;
    }
    return false;
}
查看更多
\"骚年 ilove
7楼-- · 2019-01-09 06:43

This solution doesn't send an email, but sends it to a server through UDP.

https://github.com/Chemik/logcatudp

The source code is available. It can be easily embedded in our own app. I haven't tried to do so.

查看更多
登录 后发表回答