将应用程序的 Logcat 输出发送到电子邮件地址

发布于 2024-09-02 02:00:10 字数 174 浏览 3 评论 0原文

我们现在正在与几个朋友一起测试我们的应用程序。有时,有些错误不会引发异常。所以我真的不知道问题是什么。所以我认为实现一个菜单项是一个好主意,该菜单项允许将 logcat 输出发送到电子邮件地址,以便我们可以检查日志。

不幸的是,我没有在互联网上找到如何从手机中提取 logcat 的提示。如何发送电子邮件应该不是问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

雨轻弹 2024-09-09 02:00:10

查看 android-log-collector,它的作用 从 Android 4.1 开始,

无法收集任意 LogCat 数据。从来没有记录和支持的方法来做到这一点,并且未记录/不支持的方法 被 Jelly Bean 锁定。对于您自己的崩溃,最好使用崩溃日志库,例如 ACRA

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.

不甘平庸 2024-09-09 02:00:10

我还会研究 Flurry (flurry.com),它不仅为您提供一般分析,还允许您记录任意信息,并且还可以为您记录未捕获的异常。我只用了 5 分钟就完成了设置,但要记住的一件事是,它不像电子邮件提醒那样实时。您必须等待几个小时才能在应用程序中登录的内容显示在他们的仪表板上。如果您有一个非常轻量级的应用程序,它也可能有点过大,但我注意到使用该服务后我的应用程序没有性能损失。

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.

感性 2024-09-09 02:00:10

我发现 LogCollector 确实非常有用(CommonsWare 的提示)

:不要忘记在您自己的应用程序中设置:

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

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" />
盛夏已如深秋| 2024-09-09 02:00:10

我绝对建议您在此处查看此项目

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

原谅过去的我 2024-09-09 02:00:10

此解决方案不发送电子邮件,而是通过 UDP 将其发送到服务器。

https://github.com/Chemik/logcatudp

源代码已提供。它可以轻松嵌入到我们自己的应用程序中。我还没有尝试这样做。

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.

难如初 2024-09-09 02:00:10

在您的清单文件中,授予以下权限:

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

在您的第一个/启动器活动中,在写入以下行之后

super.onCreate(savedInstanceState);

:这会将您的应用程序的 logcat 写入设备的外部存储

        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
    }

用于将 logcat 发送到所需的电子邮件地址:使用以下方法

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[] = {"[email protected]"};
            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..."));
        }
    }
}

检查是否存在的方法是否授予写入外部存储的权限:

/* 检查外部存储是否可用于读写 */

public static boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
        return true;
    }
    return false;
}

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[] = {"[email protected]"};
            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;
}
玉环 2024-09-09 02:00:10

谢谢你 2,但通过浏览 android-log-collector 论坛,我找到了一个 解决方案,它甚至更容易句柄:

您可以在服务器上的某个位置存储 php 文件(或者如果您不想使用自己的服务器,他们的服务器上也有一个脚本)。在此 php 文件中,您可以定义当发布消息到达服务器时应执行的操作。我刚刚定义了一个非常简单的代码,它将数据转发到我的邮件地址。

仅当抛出未捕获的异常时,此方法才有效。但是可以扩展默认的未捕获异常处理程序,这样不仅可以获取堆栈跟踪,还可以获取 logcat。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文