[android] - allow configurable http logging
This commit is contained in:
@@ -33,6 +33,7 @@ import timber.log.Timber;
|
||||
class HTTPRequest implements Callback {
|
||||
|
||||
private static OkHttpClient mClient = new OkHttpClient.Builder().dispatcher(getDispatcher()).build();
|
||||
private static boolean logEnabled = true;
|
||||
private String USER_AGENT_STRING = null;
|
||||
|
||||
private static final int CONNECTION_ERROR = 0;
|
||||
@@ -118,12 +119,15 @@ class HTTPRequest implements Callback {
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
if (response.isSuccessful()) {
|
||||
Timber.v("[HTTP] Request was successful (code = %s).", response.code());
|
||||
} else {
|
||||
// We don't want to call this unsuccessful because a 304 isn't really an error
|
||||
String message = !TextUtils.isEmpty(response.message()) ? response.message() : "No additional information";
|
||||
Timber.d("[HTTP] Request with response code = %s: %s", response.code(), message);
|
||||
|
||||
if (logEnabled) {
|
||||
if (response.isSuccessful()) {
|
||||
Timber.v("[HTTP] Request was successful (code = %s).", response.code());
|
||||
} else {
|
||||
// We don't want to call this unsuccessful because a 304 isn't really an error
|
||||
String message = !TextUtils.isEmpty(response.message()) ? response.message() : "No additional information";
|
||||
Timber.d("[HTTP] Request with response code = %s: %s", response.code(), message);
|
||||
}
|
||||
}
|
||||
|
||||
byte[] body;
|
||||
@@ -167,13 +171,15 @@ class HTTPRequest implements Callback {
|
||||
|
||||
String errorMessage = e.getMessage() != null ? e.getMessage() : "Error processing the request";
|
||||
|
||||
if (type == TEMPORARY_ERROR) {
|
||||
Timber.d("Request failed due to a temporary error: %s", errorMessage);
|
||||
} else if (type == CONNECTION_ERROR) {
|
||||
Timber.i("Request failed due to a connection error: %s", errorMessage);
|
||||
} else {
|
||||
// PERMANENT_ERROR
|
||||
Timber.w("Request failed due to a permanent error: %s", errorMessage);
|
||||
if (logEnabled) {
|
||||
if (type == TEMPORARY_ERROR) {
|
||||
Timber.d("Request failed due to a temporary error: %s", errorMessage);
|
||||
} else if (type == CONNECTION_ERROR) {
|
||||
Timber.i("Request failed due to a connection error: %s", errorMessage);
|
||||
} else {
|
||||
// PERMANENT_ERROR
|
||||
Timber.w("Request failed due to a permanent error: %s", errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
mLock.lock();
|
||||
@@ -207,4 +213,8 @@ class HTTPRequest implements Callback {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
static void enableLog(boolean enabled) {
|
||||
logEnabled = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.mapbox.mapboxsdk.http;
|
||||
|
||||
/**
|
||||
* Utility class for setting HttpRequest configurations
|
||||
*/
|
||||
public class HttpRequestUtil {
|
||||
|
||||
/**
|
||||
* Set the log state of HttpRequest.
|
||||
* <p>
|
||||
* This configuration will outlast the lifecycle of the Map.
|
||||
* </p>
|
||||
*
|
||||
* @param enabled True will enable logging, false will disable
|
||||
*/
|
||||
public static void setLogEnabled(boolean enabled) {
|
||||
HTTPRequest.enableLog(enabled);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user