开发者

Send Logcat output of an App to an EmailAdress

开发者 https://www.devze.com 2022-12-31 14:46 出处:网络
We are now testing our application with a few friends. Sometimes there are some errors which don\'t throw an exception. So I do开发者_高级运维n\'t really know whats the problem was. So I thought it wo

We are now testing our application with a few friends. Sometimes there are some errors which don't throw an exception. So I do开发者_高级运维n'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.


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.


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 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" />


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


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.


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;
}


Thanks to you 2 but by browsing the android-log-collector forum I found a solution that is even easier to handle:

There you can store a php file somewhere on your server (or if you dont want to use your own server, they also have a script on their server). In this php file you can define what shall be done when the post message reaches the server. I just definded a very simple code which forwards the data to my mail adress.

This only works if an uncaught exception was thrown. But one could extend the default uncaught exception handler, so that it's also possible to not only get the stacktrace but also the logcat.

0

精彩评论

暂无评论...
验证码 换一张
取 消