2010-05-17 133 views
17

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

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

回答

13

看看 android-log-collector,因为它做你正在做的事情。

从Android 4.1开始,不可能收集任意的LogCat数据。从来没有一个记录和支持的方式来做到这一点,以及无证/无支持的方式was locked down in Jelly Bean。对于你自己的崩溃,你最好使用崩溃日志库,如ACRA

+0

嗯,谢谢。所以我必须将源代码添加到我的项目中或在手机上安装应用程序。对? – RoflcoptrException 2010-05-17 20:21:03

+0

你不得不问他们,因为我没有用过他们的东西。至少,你可以看看他们的源代码,看看他们如何阅读日志。 – CommonsWare 2010-05-17 20:59:39

+0

嗯是的,我在开发人员可以使用它的页面上阅读,但是没有文档,我不知道如何启动它。但感谢 – RoflcoptrException 2010-05-17 21:06:41

7

我还会研究Flurry(flurry.com),它不仅可以给你一般的分析,还可以记录任意信息并记录未捕获的异常。我在5分钟内设置了它,但有一点需要记住的是它不是实时的电子邮件警报。您必须等待几个小时才能登录您的应用,并在其仪表板上显示。如果你有一个非常轻量级的应用程序,它也可能是矫枉过正的,但我注意到,由于使用该服务,我的应用程序没有性能损失。

0

感谢您2,但通过浏览Android的日志收集论坛上,我发现了一个solution这是更容易处理:

在那里,你可以在服务器上存储一些PHP文件(或者,如果你不想要使用您自己的服务器,他们的服务器上也有一个脚本)。在这个php文件中,你可以定义邮件到达服务器时应该做什么。我只是定义了一个将数据转发给我的邮件地址的非常简单的代码。

这只适用于未抛出异常的情况。但是可以扩展默认的未捕获异常处理程序,这样不仅可以获取堆栈跟踪,还可以获取logcat。

+1

此链接已中断,您可以升级它或者发布源代码? – gobernador 2012-03-07 02:32:12

+0

是的,它坏了,我找不到一个新的。但正如你可能已经看到答案已经快2年了。 – 2012-03-07 09:11:24

+0

这是一个旧的链接。我正在研究并尝试做这件事,我只是想知道你是否知道它在哪里。无论如何,我会尝试另一个答案。 – gobernador 2012-03-07 16:38:35

3

我肯定会推荐也来看看这个项目here

6

我发现LogCollector非常有用的确(从CommonsWare尖端):

而且不要忘记在自己的应用程序设置:

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

LogCollector似乎记录一些垃圾,而不是我的应用程序的日志。没有一条线。 – djdance 2014-08-28 19:31:34

+1

LogCollector似乎已经死亡。自2012年以来没有任何活动。 – 2014-10-01 07:44:45

1

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

https://github.com/Chemik/logcatudp

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

+0

Logcat到UDP源已经转移到https://github.com/Chemik/logcatudp。 BTW应用程序可以发送日志到电子邮件或使用任何其他安装的应用程序共享。 – Chemik 2015-03-13 22:11:11

1

在您的清单文件提供以下权限:

<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到所需的电子邮件添加RESS:用以下方法

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