Android acra monitoring error log reporting (acra version 4.9)

Posted by ehask on Tue, 18 Jan 2022 17:56:12 +0100

Use acra to monitor the crash log of app running and report it to the server for troubleshooting.

acra is an excellent open source log reporting project. It can intercept and collect error reporting information and report it to the server when your software crashes such as anr, cash and oom. It is similar to the error reporting collection sdk function of Youmeng and other three parties. It is better than stable Kaiyuan. It can be customized according to needs.

It is easy to use and can be compiled into a jar package (PS: jar package resources have been uploaded). You can put the source code directly into the project or add dependencies directly in build. For details, please refer to https://github.com/ACRA/acra/wiki/AdvancedUsage Official wiki.

Let's talk about the use of acra 4.9:

I Initialization of acra

1. The first basic use mode:

@ReportsCrashes(formUri = "" , // url of escalation server
        mode = ReportingInteractionMode.SILENT, //Report silently without any prompt
        reportType = HttpSender.Type.JSON,  //Data format json
        httpMethod = HttpSender.Method.POST,  //Reporting method: post
        customReportContent = {DROPBOX},  // Optionally, keep long text or data blocks
        includeDropBoxSystemTags = true, //Retrieve system tag events
        dropboxCollectionMinutes = 30  // Set the interception time of reported data (30 minutes before interception)
public class MyApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        ACRA.DEV_LOGGING = true;  // log output
        ACRA.init(this);  //acra initialization
    }
}

Permission is required to use customreportcontent and includedropboxsystemtags

< uses permission Android: name = "Android. Permission. Read_logs" / >, when the app has system level permissions, it can monitor the error log of the whole machine. It is also convenient to use when developing the source code of the whole machine. It can monitor the error information of the whole system to facilitate debugging and catching errors.

Return system tag events:

  • 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. The second basic use mode:

The first way is to use annotation, which is simple and convenient, but the parameters in the annotation can only use constants. When you have some special requirements for dynamic address change, it cannot be applied, but we can also use configuration.

public class MyApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        ConfigurationBuilder c = new ConfigurationBuilder(this);
        
        c.setFormUri("") // server address
                            
        .setMode(ReportingInteractionMode.SILENT) //Silent reporting
        .setReportType(HttpSender.Type.JSON)
        .setHttpMethod(HttpSender.Method.POST)
        .setCustomReportContent(new org.acra.ReportField[]{DROPBOX})
        .setIncludeDropboxSystemTags(true)
        .setDropboxCollectionMinutes(30)
        .setSocketTimeout(60000); //Set timeout
        ACRA.DEV_LOGGING = true;
        ACRA.init(this, c); //acra initialization
    }
}

Variables can be used in this way, and change configurations can be added according to requirements.

Remember to connect to Android manifest Add to XML:

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

After the above two access methods are completed, acra can be used.

II acra actively reports:

For different development requirements, we sometimes need to ask acra to actively report error information in how many time periods.

At this time, we can write a timer to actively trigger acra for reporting:

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

Use the AlarmManager to time accurately for a long time.

Use pendingIntent to open a service that inherits IntentService and will be automatically destroyed after execution.

public class ReportService extends IntentService {


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

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

    @Override
    protected void onHandleIntent(Intent intent) {
        //ACRA actively triggers reporting, and handleSilentException can define its own error reporting, which is unnecessary
        ACRA.getErrorReporter().handleSilentException(null);
    }


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

By combining the above initialization, all tag error messages within 30 minutes will be reported every 30 minutes.

III Submission format (based on the json format I initialized above):

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}

The system flag event is reported in the form of tag. If there is no tag, then nothing.

That's it. Most of the official documents of the interface description are available. This part is for you to sort out and share.

Topics: Android