Skip to main content

Logging

TestFairy gives you the ability to log all your network requests. It gives you an effortless way to monitor your app's network access.

example issues
TestFairy.addNetworkEvent(URI uri, String method, int code, long startTimeMillis, long endTimeMillis, long requestSize, long responseSize, String errorMessage);
ExampleIf you are using OkHttp or Retrofit all you need to do is add CustomHttpInterceptor to your client:
// Be sure to import TestFairy
import com.testfairy.TestFairy;

public class CustomHttpInterceptor implements Interceptor {
@Override
public Response intercept(@NonNull Chain chain) throws IOException {

Request request = chain.request();
long startTimeMillis = System.currentTimeMillis();
Long requestSize = request.body() != null ? request.body().contentLength() : 0;
Response response;
try {
response = chain.proceed(request);
} catch (IOException e) {
long endTimeMillis = System.currentTimeMillis();
TestFairy.addNetworkEvent(request.url().uri(), request.method(), -1, startTimeMillis, endTimeMillis, requestSize, -1, e.getMessage());
throw e;
}

long endTimeMillis = System.currentTimeMillis();
long responseSize = response.body() != null ? response.body().contentLength() : 0;
TestFairy.addNetworkEvent(request.url().uri(), request.method(), response.code(), startTimeMillis, endTimeMillis, requestSize, responseSize, null);
return response;
}
}


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

Exception Logging

TestFairy allows developers to log up to five exceptions or errors for a session.

note

It does not mark the sessions as crashed; it will only log the exception or error to the session.

TestFairy.logThrowable(<throwable exception>);
Example
// Be sure to import TestFairy
import com.testfairy.TestFairy;

TestFairy.logThrowable(new Throwable("Some Message"));