Logging

This section covers how Instabug automatically attaches the console logs, the verbose logs, all the steps done by the user until a bug report is sent.

You can send a variety of logs types with each crash or bug report. They will appear within each report on your Instabug dashboard as shown below. The cut-off point of the log collection is when Instabug is invoked.

Logs - Dashboard

Logs - Dashboard

Console Logs

Instabug automatically captures all console logs and display them on your dashboard with each report. Note that the maximum number of console logs sent with each report is 500 for the number of the statements and 5K characters for each statement.

Instabug Logs

You can log messages throughout your application's lifecycle. Those logs are going to be sent with each report. InstabugLog works just like the regular Log class you use to show colorful logs in your logcat. Note that the maximum number of Instabug logs sent with each report is 1k.

InstabugLog.d("Message to log");
InstabugLog.v("Message to log");
InstabugLog.i("Message to log");
InstabugLog.e("Message to log");
InstabugLog.w("Message to log");
InstabugLog.wtf("Message to log");

Visual Repro Steps

The set of steps that the user commits while they are in a specific view are grouped together. When the user navigates to a new view, this action is reflected in the repro steps and all the interactions happening within the new view are grouped together until the user navigates away from this view. The limit for each report is 100 log statements and 20 screenshots.

To Allow Instabug to record user steps, you need to call the following method from your Activity.

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
	InstabugTrackingDelegate.notifyActivityGotTouchEvent(ev, this);
	return super.dispatchTouchEvent(ev);
}

This feature is available starting from the Silver Plan. You can control it through setReproStepsState Instabug builder method.

new Instabug.Builder(this, “APP_TOKEN”)
	.setInvocationEvents(InstabugInvocationEvent.SHAKE, InstabugInvocationEvent.SCREENSHOT)
  .setReproStepsState(State.ENABLED)
	.build(); 

Here are the possible arguments.

State.ENABLED;
State.ENABLED_WITH_NO_SCREENSHOTS;
State.DISABLED;

User Steps

Instabug can detect user steps that can help you reproduce a bug in your app. To allow Instabug to record user steps, you need to call the following method from your Activity. Note that the maximum number of user steps sent with each report is 100.

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
	InstabugTrackingDelegate.notifyActivityGotTouchEvent(ev, this);
	return super.dispatchTouchEvent(ev);
}

User steps will be formatted as follows:
Event in text/viewID of type class in parentView.

The type of events captured are tap, double tap, long press, swipe, scroll and pinch.
The text/viewID would be the text or the ID of the object that contains the event.
Class would be the class of the object that contains the event.
ParentView would be the view that contained the event.

Tracking Fragments

If you tend to use fragments in your application, you will need to track in manually. In order to do so, add the following method to the Activity holding the fragment.

@Override public boolean dispatchTouchEvent(MotionEvent ev){  
           InstabugTrackingDelegate.notifyActivityGotTouchEvent(ev, this);  
           return super.dispatchTouchEvent(ev); 
}

Afterwards, create a base fragment class. You'll need to call InstabugTrakingDelegate's fragment life-cycle methods from your fragment's life-cycle callbacks. These methods are as follows:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
           super.onViewCreated(view, savedInstanceState);
           InstabugTrackingDelegate.notifyFragmentViewCreated(view, this, getActivity());
}

@Override
public void onStart() {
           InstabugTrackingDelegate.notifyFragmentStarted(this, getActivity());
           super.onStart();
}

@Override
public void onResume() {
           InstabugTrackingDelegate.notifyFragmentResumed(this, getActivity());
           super.onResume();
}

@Override
public void onPause() {
           InstabugTrackingDelegate.notifyFragmentPaused(this, getActivity());
           super.onPause();
}

@Override
public void onStop() {
           InstabugTrackingDelegate.notifyFragmentStopped(this, getActivity());
           super.onStop();
}

Why in a base fragment?

A base fragment can use this code, which you can then inherit in other fragments you already have without the need to write redundant code.

User Events

You can log custom user events throughout your application. Events are automatically going to be included with each report. Note that the maximum number of user events sent with each report is 1k.

Instabug.logUserEvent("Test", new UserEventParam().setKey("Key").setValue("Value"));

Network Logs

Instabug offers logging for all network requests performed by your app. Requests details, along with their responses, are going to be sent with each report. Instabug will also show an alert in the bug report when it finds that some network requests have timed-out or have taken too long to complete. Note that the maximum number of network logs sent with each report is 1k.

Logging HttpUrlConnection requests

To log network request, use InstabugNetworkLog then use the following method at the HttpUrlConnection, Request body and response body

 InstabugNetworkLog networkLog = new InstabugNetworkLog();
 networkLog.Log(urlConnection, requestBody, responseBody);

For a more detailed example see the following network request

@Override
protected String doInBackground(Void... params) {
	
  HttpURLConnection urlConnection = null;
  BufferedReader reader = null;
  String moviesJsonStr = null;
	
  try {
    URL url = new URL("<YOUR_URL>");
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoOutput(true);
    urlConnection.setRequestMethod("POST");        
    urlConnection.setUseCaches(false);           
    urlConnection.setConnectTimeout(10000);            
    urlConnection.setReadTimeout(10000);           
    urlConnection.setRequestProperty("Content-Type", "application/json");         
    urlConnection.connect();
    
    JSONObject jsonParam = new JSONObject();         
    try {        
      jsonParam.put("PARAM_1", "one");           
      jsonParam.put("PARAM_2", "two");       
    } catch (JSONException e) {            
      e.printStackTrace();        
    }
		
    OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
    out.write(jsonParam.toString());   
    out.close();

    InputStream inputStream = urlConnection.getInputStream();
    StringBuffer buffer = new StringBuffer();
    if (inputStream == null) {
    	return null;
    }
                
    reader = new BufferedReader(new InputStreamReader(inputStream));              
    String line;        
    while ((line = reader.readLine()) != null) {
      buffer.append(line + "\n");   
    }
		
    if (buffer.length() == 0) {                 
      // Stream was empty.  No point in parsing.             
      return null;      
    }
    moviesJsonStr = buffer.toString();

    //logging network request to instabug
    InstabugNetworkLog networkLog = new InstabugNetworkLog();
    networkLog.Log(urlConnection, jsonParam.toString(), moviesJsonStr);

    return moviesJsonStr;
}

Logging Okhttp requests

First, you will need to compile Instabug with network interceptor instead.

compile 'com.instabug.library:instabug-with-okhttp-interceptor:8+'

To log Oktthp requests use the InstabugOkhttpInterceptor as shown in the following example.

InstabugOkhttpInterceptor instabugOkhttpInterceptor = new InstabugOkhttpInterceptor();

OkHttpClient client = new OkHttpClient.Builder()
	.addInterceptor(interceptor)
	.addInterceptor(instabugOkhttpInterceptor)
	.build();