Skip to main content

🌏 Network logs

Gleap allows you to monitor all network requests for a better understanding of what's happening within your apps. It can be done manually by passing the data to Gleap or if you're using OkHttp with the Gleap OkHttpInterceptor.

Logging HttpUrlConnection requests

There are several ways to log the network with HttpUrlConnection. The recommended way is to use the following method. All information from the connection is automatically gathered for you. The requestBody and result can be passed on as JSON or String.

  Gleap.getInstance().logNetwork((HttpsURLConnection) conn, requestBody, result);

Let's have a more detailed example. This request posts data to an endpoint.

....
@Override
protected Object doInBackground(Object[] objects) {
HttpURLConnection conn = null;
JSONObject result = null;
try {
URL url = new URL("YOUR_URL");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");

JSONObject requestBody = new JSONObject();
try {
requestBody.put("Key", "Value");
requestBody.put("Key2", "Value");
} catch (JSONException e) {
e.printStackTrace();
}


try (OutputStream os = conn.getOutputStream()) {
byte[] input = requestBody.toString().getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}

try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "utf-8"))) {

String input;
while ((input = br.readLine()) != null) {
result = new JSONObject(input);
}
} catch (JSONException e) {
e.printStackTrace();
}
Gleap.getInstance().logNetwork((HttpsURLConnection) conn, requestBody, result);
} catch(Exception e) {
e.printStackTrace();
}

OkHttp

There is an easy way to intercept all requests from OkHttp. This will be logged automatically by Gleap.

First of all, import the OkHttp interceptor for Gleap in your build.gradle(root).

implementation group: 'io.gleap', name: 'gleap-okhttp-interceptor', version: '6.3.0'

The interceptor can be used like this:

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new GleapOkHttpInterceptor())
.build();

Request request = new Request.Builder()
.url("http://www.publicobject.com/helloworld.txt")
.header("User-Agent", "OkHttp Example")
.build();

Response response = client.newCall(request).execute();
response.body().close();

Other Libraries

If you are using any other library than HTTPUrlConnection or OkHttp you can manually log any requests with the following method.

void logNetwork("YOUR_URL", RequestType.GET, 200, 150, requestBody, response);

Filtering network logs

Gleap allows you to strip off specific key/value pairs from network logs. This empowers you to easily remove sensitive data like tokens, passwords or usernames.

How does it work?

Log in to the Gleap dashboard and open the visual widget configurator. Now enable the network log filters option (within the advanced options tab). Once the option is enabled, you can add multiple keys to the array bellow, which should be excluded.

All keys, which you add to the exclusion list, will be loaded together with the widget configuration. Before sending a feedback item to our backend, the client SDK will loop through all network requests and stripe off all matching key/value pairs from the request headers, header-payload (if it's a JSON) and response body (if it's a JSON).

This ensures that sensitive information will never even leave the client application.

Example:

If you want to remove the Authorization bearer token from your requests, simply add "Authorization" to the list of keys.

Network log filters