当前位置: 首页 > 工具软件 > acra > 使用案例 >

Android acra 监控报错日志上报(acra版本4.9)

安承教
2023-12-01

使用acra监控app运行时的崩溃日志,并上报服务器,方便排错。

acra是一款优秀的开源日志上报项目。它可以在你软件运行发生anr,carsh,oom等崩溃状况时拦截并收集报错信息上报服务端,与友盟等三方报错收集sdk功能相似,胜在稳定开元,可以根据需求方便自己定制。

 使用方便简单可以编译成jar包(PS:jar包资源已经上传),可以将源码直接放入项目,也可以在build中直接添加依赖。使用详情可以参照 https://github.com/ACRA/acra/wiki/AdvancedUsage 官方的wiki。

这里讲一下acra 4.9 的使用:

一.acra的初始化

1.第一种基本使用方式:

@ReportsCrashes(formUri = "" , // 上报服务器的url
        mode = ReportingInteractionMode.SILENT, //静默上报,没有任何提示
        reportType = HttpSender.Type.JSON,  //数据格式json
        httpMethod = HttpSender.Method.POST,  //上报方式post
        customReportContent = {DROPBOX},  // 可选保留长文本或数据块
        includeDropBoxSystemTags = true, //是否检索系统标记事件
        dropboxCollectionMinutes = 30  // 设置上报的数据截取时间,这里截取前30分钟的)
public class MyApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        ACRA.DEV_LOGGING = true;  // 是否进行log输出
        ACRA.init(this);  //acra初始化
    }
}

其中使用customReportContent ,includeDropBoxSystemTags 需要权限

<uses-permission android:name="android.permission.READ_LOGS" />,当app拥有系统级权限时可以监测整机的报错日志,做整机源码开发时使用也很方便的可以监测整个系统的报错信息,方便调试抓错。

返回系统标记事件:

  • system_app_anr
  • system_app_wtf
  • system_app_crash
  • system_server_anr
  • system_server_wtf
  • system_server_crash
  • BATTERY_DISCHARGE_INFO
  • SYSTEM_RECOVERY_LOG
  • SYSTEM_BOOT
  • SYSTEM_LAST_KMSG
  • APANIC_CONSOLE
  • APANIC_THREADS
  • SYSTEM_RESTART
  • SYSTEM_TOMBSTONE
  • data_app_strictmode

2.第二种基本使用方式:

  第一种是使用注解的方式,简单方便,但是注解中的参数只能使用常量,当你有些特殊需求要求动态地址变动等就无法适用,但我们还可以使用configuration。

public class MyApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        ConfigurationBuilder c = new ConfigurationBuilder(this);
        
        c.setFormUri("") // 服务器地址
                            
        .setMode(ReportingInteractionMode.SILENT) //静默上报
        .setReportType(HttpSender.Type.JSON)
        .setHttpMethod(HttpSender.Method.POST)
        .setCustomReportContent(new org.acra.ReportField[]{DROPBOX})
        .setIncludeDropboxSystemTags(true)
        .setDropboxCollectionMinutes(30)
        .setSocketTimeout(60000); //设置超时
        ACRA.DEV_LOGGING = true;
        ACRA.init(this, c); //acra初始化
    }
}

使用此方式可以使用变量,可以根据需求添加变动配置。

接入时记得在AndroidManifest.xml中添加:

<service android:name="org.acra.sender.SenderService"></service>

到此完成以上这两种方式接入后,已经可以使用acra了。

二.acra主动上报:

对于不同的开发需求,我们有时候会有需求要求acra主动上报多少时间段内的报错信息。

这个时候我们可以写个定时器主动触发acra进行上报:

private void setAlarm() {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        int anhour = 30*60*1000;
        Intent alarmIntent = new Intent(this, ReportService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, java.lang.System.currentTimeMillis(), anhour, pendingIntent);
    }

使用AlarmManager可以长时间精准计时。

使用pendingIntent开启一个service,该service继承IntentService执行完后会自动销毁。

public class ReportService extends IntentService {


    public ReportService() {
        super("ReportService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //ACRA主动触发上报,handleSilentException可以自己定义报错,不需要的穿null
        ACRA.getErrorReporter().handleSilentException(null);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

如此联合上面的初始化,就做到了每隔30分钟就会上报一次30分钟内的所有标记报错信息。

三.上报格式(基于我上面初始化的json格式):

ACRA: {"REPORT_ID":"e7395f2a-12eb-4b4d-a4ef-d48f9348381d","STACK_TRACE":"Report requested by developer\n","USER_APP_START_DATE":"2011-12-01T00:00:20.602+08:00","USER_CRASH_DATE":"2021-07-20T10:25:53.167+08:00","DROPBOX":"Tag: system_app_anr\nNothing.\nTag: system_app_wtf\nNothing.\nTag: system_app_crash\nNothing.\nTag: system_server_anr\nNothing.\nTag: system_server_wtf\nNothing.\nTag: system_server_crash\nNothing.\nTag: BATTERY_DISCHARGE_INFO\nNothing.\nTag: SYSTEM_RECOVERY_LOG\nNothing.\nTag: SYSTEM_BOOT\nNothing.\nTag: SYSTEM_LAST_KMSG\nNothing.\nTag: APANIC_CONSOLE\nNothing.\nTag: APANIC_THREADS\nNothing.\nTag: SYSTEM_RESTART\nNothing.\nTag: SYSTEM_TOMBSTONE\nNothing.\nTag: data_app_strictmode\nNothing.\n","IS_SILENT":true}

系统标记事件以tag形式上报,没有则nNothing。

到此就结束了,大部分接口说明官方文档都有,此部分是自己整理分享一下。

 类似资料: