Network logs are automatically collected by Instabug when possible. There are many way to configure and manipulate these logs from the code.
Logging HttpUrlConnection
Requests
HttpUrlConnection
RequestsTo log network requests, use InstabugNetworkLog
then use the following method at the HttpUrlConnection
, requestBody
and responseBody
. A more detailed example can be found here.
Logging HttpUrlConnection
Requests
InstabugNetworkLog networkLog = new InstabugNetworkLog();
networkLog.Log(urlConnection, requestBody, responseBody);
InstabugNetworkLog networkLog = new InstabugNetworkLog()
networkLog.Log(urlConnection, requestBody, responseBody)
Logging Okhttp
Requests
Okhttp
RequestsIn order to log Okhttp requests, first make sure that you compiled Instabug with a network interceptor. By adding the following to your Gradle:
implementation 'com.instabug.library:instabug-with-okhttp-interceptor:8+'
An example of the implementation can be found on the right hand side of this section.
First, start by compiling Instabug with a network interceptor using this API.
implementation 'com.instabug.library:instabug-with-okhttp-interceptor:11.5.4'
Then 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();
val instabugOkhttpInterceptor = InstabugOkhttpInterceptor()
val client = OkHttpClient.Builder()
.addInterceptor(interceptor)
.addInterceptor(instabugOkhttpInterceptor)
.build()
Modifying Requests
If you want to modify a network request before it gets sent to the dashboard, you may follow the steps below.
1- Create a NetworkLogListener
object and modify the captured network log as shown below.
NetworkLogListener networkLogListener = new NetworkLogListener() {
@Override
public NetworkLogSnapshot onNetworkLogCaptured(NetworkLogSnapshot networkLog) {
//Modify the received networkLog parameter
return networkLog;
}
};
val networkLogListener = NetworkLogListener { networkLog: NetworkLogSnapshot ->
//Modify the received networkLog parameter
return@NetworkLogListener networkLog
}
2- Register the created NetworkLogListener
to your InstabugOkHttpInterceptor
object. This can be done through two different methods:
a. Pass it in the constructor.
val instabugOkhttpInterceptor = InstabugOkhttpInterceptor(networkLogListener)
b. Call registerNetworkLogsListener method on InstabugOkhttpInterceptor object.
val instabugOkhttpInterceptor = InstabugOkhttpInterceptor()
instabugOkhttpInterceptor.registerNetworkLogsListener(networkLogListener)
If you want to remove the network listener, you can do so using this API.
instabugOkhttpInterceptor.removeNetworkLogsListener();
instabugOkhttpInterceptor.removeNetworkLogsListener()