Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.launchdarkly.sdk.android;

import static org.junit.Assert.assertEquals;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;

@RunWith(AndroidJUnit4.class)
public class TimberLoggingTest {

@Rule
public TimberLoggingRule timberLoggingRule = new TimberLoggingRule();

private TestTree testTree;

@Before
public void setUp() throws Exception {
testTree = new TestTree();
Timber.plant(testTree);
}

@After
public void tearDown() throws Exception {
testTree = null;
}

@Test
public void timberTagIsLaunchDarklySdkForAllEvents() {
LDConfig.log().d("event");
LDConfig.log().d("event");

assertEquals(List.of("LaunchDarklySdk", "LaunchDarklySdk"), testTree.loggedTags);
}

private static class TestTree extends Timber.Tree {

final List<String> loggedTags = new ArrayList<String>();

@Override
protected void log(int priority, @Nullable String tag, @NonNull String message, @Nullable Throwable t) {
loggedTags.add(tag);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void onError(Throwable e) {
LDClient ldClient = LDClient.getForMobileKey(environmentName);
ldClient.updateListenersOnFailure(connectionInformation.getLastFailure());
} catch (LaunchDarklyException ex) {
LDConfig.LOG.e(e, "Error getting LDClient for ConnectivityManager");
LDConfig.log().e(e, "Error getting LDClient for ConnectivityManager");
}
callInitCallback();
}
Expand Down Expand Up @@ -374,13 +374,13 @@ private synchronized void updateConnectionMode(ConnectionMode connectionMode) {
try {
saveConnectionInformation();
} catch (Exception ex) {
LDConfig.LOG.w(ex, "Error saving connection information");
LDConfig.log().w(ex, "Error saving connection information");
}
try {
LDClient ldClient = LDClient.getForMobileKey(environmentName);
ldClient.updateListenersConnectionModeChanged(connectionInformation);
} catch (LaunchDarklyException e) {
LDConfig.LOG.e(e, "Error getting LDClient for ConnectivityManager");
LDConfig.log().e(e, "Error getting LDClient for ConnectivityManager");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ private void postEvents(List<Event> events) {
baseHeadersForRequest.put("X-LaunchDarkly-Payload-ID", eventPayloadId);
baseHeadersForRequest.putAll(baseEventHeaders);

LDConfig.LOG.d("Posting %s event(s) to %s", events.size(), url);
LDConfig.LOG.d("Events body: %s", content);
LDConfig.log().d("Posting %s event(s) to %s", events.size(), url);
LDConfig.log().d("Events body: %s", content);

for (int attempt = 0; attempt < 2; attempt++) {
if (attempt > 0) {
LDConfig.LOG.w("Will retry posting events after 1 second");
LDConfig.log().w("Will retry posting events after 1 second");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
Expand All @@ -166,11 +166,11 @@ private void postEvents(List<Event> events) {
.build();

try (Response response = client.newCall(request).execute()) {
LDConfig.LOG.d("Events Response: %s", response.code());
LDConfig.LOG.d("Events Response Date: %s", response.header("Date"));
LDConfig.log().d("Events Response: %s", response.code());
LDConfig.log().d("Events Response Date: %s", response.header("Date"));

if (!response.isSuccessful()) {
LDConfig.LOG.w("Unexpected response status when posting events: %d", response.code());
LDConfig.log().w("Unexpected response status when posting events: %d", response.code());
if (isHttpErrorRecoverable(response.code())) {
continue;
}
Expand All @@ -179,7 +179,7 @@ private void postEvents(List<Event> events) {
tryUpdateDate(response);
break;
} catch (IOException e) {
LDConfig.LOG.e(e, "Unhandled exception in LaunchDarkly client attempting to connect to URI: %s", request.url());
LDConfig.log().e(e, "Unhandled exception in LaunchDarkly client attempting to connect to URI: %s", request.url());
}
}
}
Expand All @@ -192,7 +192,7 @@ private void tryUpdateDate(Response response) {
Date date = sdf.parse(dateString);
currentTimeMs = date.getTime();
} catch (ParseException pe) {
LDConfig.LOG.e(pe, "Failed to parse date header");
LDConfig.log().e(pe, "Failed to parse date header");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static String sharedPrefs(final LDUser user) {
*/
void setCurrentUser(final LDUser user) {
String userBase64 = base64Url(user);
LDConfig.LOG.d("Setting current user to: [%s] [%s]", userBase64, userBase64ToJson(userBase64));
LDConfig.log().d("Setting current user to: [%s] [%s]", userBase64, userBase64ToJson(userBase64));
currentUser = user;
flagStoreManager.switchToUser(DefaultUserManager.sharedPrefs(user));
}
Expand All @@ -98,7 +98,7 @@ public void onSuccess(JsonObject result) {
@Override
public void onError(Throwable e) {
if (LDUtil.isClientConnected(application, environmentName)) {
LDConfig.LOG.e(e, "Error when attempting to set user: [%s] [%s]",
LDConfig.log().e(e, "Error when attempting to set user: [%s] [%s]",
base64Url(currentUser),
userBase64ToJson(base64Url(currentUser)));
}
Expand Down Expand Up @@ -133,14 +133,14 @@ void unregisterAllFlagsListener(@NonNull final LDAllFlagsListener listener) {
*/
@SuppressWarnings("JavaDoc")
private void saveFlagSettings(JsonObject flagsJson, LDUtil.ResultCallback<Void> onCompleteListener) {
LDConfig.LOG.d("saveFlagSettings for user key: %s", currentUser.getKey());
LDConfig.log().d("saveFlagSettings for user key: %s", currentUser.getKey());

try {
final List<Flag> flags = GsonCache.getGson().fromJson(flagsJson, FlagsResponse.class).getFlags();
flagStoreManager.getCurrentUserStore().clearAndApplyFlagUpdates(flags);
onCompleteListener.onSuccess(null);
} catch (Exception e) {
LDConfig.LOG.d("Invalid JsonObject for flagSettings: %s", flagsJson);
LDConfig.log().d("Invalid JsonObject for flagSettings: %s", flagsJson);
onCompleteListener.onError(new LDFailure("Invalid Json received from flags endpoint", e, LDFailure.FailureType.INVALID_RESPONSE_BODY));
}
}
Expand All @@ -157,13 +157,13 @@ public void deleteCurrentUserFlag(@NonNull final String json, final LDUtil.Resul
flagStoreManager.getCurrentUserStore().applyFlagUpdate(deleteFlagResponse);
onCompleteListener.onSuccess(null);
} else {
LDConfig.LOG.d("Invalid DELETE payload: %s", json);
LDConfig.log().d("Invalid DELETE payload: %s", json);
onCompleteListener.onError(new LDFailure("Invalid DELETE payload",
LDFailure.FailureType.INVALID_RESPONSE_BODY));
}
});
} catch (Exception ex) {
LDConfig.LOG.d(ex, "Invalid DELETE payload: %s", json);
LDConfig.log().d(ex, "Invalid DELETE payload: %s", json);
onCompleteListener.onError(new LDFailure("Invalid DELETE payload", ex,
LDFailure.FailureType.INVALID_RESPONSE_BODY));
}
Expand All @@ -173,12 +173,12 @@ public void putCurrentUserFlags(final String json, final LDUtil.ResultCallback<V
try {
final List<Flag> flags = GsonCache.getGson().fromJson(json, FlagsResponse.class).getFlags();
executor.submit(() -> {
LDConfig.LOG.d("PUT for user key: %s", currentUser.getKey());
LDConfig.log().d("PUT for user key: %s", currentUser.getKey());
flagStoreManager.getCurrentUserStore().clearAndApplyFlagUpdates(flags);
onCompleteListener.onSuccess(null);
});
} catch (Exception ex) {
LDConfig.LOG.d(ex, "Invalid PUT payload: %s", json);
LDConfig.log().d(ex, "Invalid PUT payload: %s", json);
onCompleteListener.onError(new LDFailure("Invalid PUT payload", ex,
LDFailure.FailureType.INVALID_RESPONSE_BODY));
}
Expand All @@ -192,13 +192,13 @@ public void patchCurrentUserFlags(@NonNull final String json, final LDUtil.Resul
flagStoreManager.getCurrentUserStore().applyFlagUpdate(flag);
onCompleteListener.onSuccess(null);
} else {
LDConfig.LOG.d("Invalid PATCH payload: %s", json);
LDConfig.log().d("Invalid PATCH payload: %s", json);
onCompleteListener.onError(new LDFailure("Invalid PATCH payload",
LDFailure.FailureType.INVALID_RESPONSE_BODY));
}
});
} catch (Exception ex) {
LDConfig.LOG.d(ex, "Invalid PATCH payload: %s", json);
LDConfig.log().d(ex, "Invalid PATCH payload: %s", json);
onCompleteListener.onError(new LDFailure("Invalid PATCH payload", ex,
LDFailure.FailureType.INVALID_RESPONSE_BODY));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ void sendDiagnosticEventSync(DiagnosticEvent diagnosticEvent) {
.headers(config.headersForEnvironment(environment, baseDiagnosticHeaders))
.post(RequestBody.create(content, JSON)).build();

LDConfig.LOG.d("Posting diagnostic event to %s with body %s", request.url(), content);
LDConfig.log().d("Posting diagnostic event to %s with body %s", request.url(), content);

try (Response response = client.newCall(request).execute()) {
LDConfig.LOG.d("Diagnostic Event Response: %s", response.code());
LDConfig.LOG.d("Diagnostic Event Response Date: %s", response.header("Date"));
LDConfig.log().d("Diagnostic Event Response: %s", response.code());
LDConfig.log().d("Diagnostic Event Response Date: %s", response.header("Date"));
} catch (IOException e) {
LDConfig.LOG.w(e, "Unhandled exception in LaunchDarkly client attempting to connect to URI: %s", request.url());
LDConfig.log().w(e, "Unhandled exception in LaunchDarkly client attempting to connect to URI: %s", request.url());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private List<DiagnosticEvent.StreamInit> getStreamInits() {
DiagnosticEvent.StreamInit[] streamInitsArr = GsonCache.getGson().fromJson(streamInitsString, DiagnosticEvent.StreamInit[].class);
streamInits = Arrays.asList(streamInitsArr);
} catch (Exception ex) {
LDConfig.LOG.w(ex, "Invalid stream inits array in diagnostic data store");
LDConfig.log().w(ex, "Invalid stream inits array in diagnostic data store");
streamInits = null;
}
return streamInits;
Expand Down Expand Up @@ -161,4 +161,4 @@ void recordEventsInLastBatch(long eventsInLastBatch) {
.putLong(EVENT_BATCH_KEY, eventsInLastBatch)
.apply();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ public void onActivityResumed(Activity activity) {

if (wasBackground) {
handler.post(() -> {
LDConfig.LOG.d("went foreground");
LDConfig.log().d("went foreground");
for (Listener l : listeners) {
try {
l.onBecameForeground();
} catch (Exception exc) {
LDConfig.LOG.e(exc, "Listener threw exception!");
LDConfig.log().e(exc, "Listener threw exception!");
}
}
});
} else {
LDConfig.LOG.d("still foreground");
LDConfig.log().d("still foreground");
}
}

Expand All @@ -174,16 +174,16 @@ public void onActivityPaused(Activity activity) {
handler.postDelayed(check = () -> {
if (foreground && paused) {
foreground = false;
LDConfig.LOG.d("went background");
LDConfig.log().d("went background");
for (Listener l : listeners) {
try {
l.onBecameBackground();
} catch (Exception exc) {
LDConfig.LOG.e(exc, "Listener threw exception!");
LDConfig.log().e(exc, "Listener threw exception!");
}
}
} else {
LDConfig.LOG.d("still background");
LDConfig.log().d("still background");
}
}, CHECK_DELAY);
}
Expand All @@ -207,4 +207,4 @@ public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
@Override
public void onActivityDestroyed(Activity activity) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private HttpFeatureFlagFetcher(Context context, LDConfig config, String environm
this.context = context;

File cacheDir = new File(context.getCacheDir(), "com.launchdarkly.http-cache");
LDConfig.LOG.d("Using cache at: %s", cacheDir.getAbsolutePath());
LDConfig.log().d("Using cache at: %s", cacheDir.getAbsolutePath());

client = new OkHttpClient.Builder()
.cache(new Cache(cacheDir, MAX_CACHE_SIZE_BYTES))
Expand All @@ -62,12 +62,12 @@ public synchronized void fetch(LDUser user, final LDUtil.ResultCallback<JsonObje
? getReportRequest(user)
: getDefaultRequest(user);

LDConfig.LOG.d(request.toString());
LDConfig.log().d(request.toString());
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
LDConfig.LOG.e(e, "Exception when fetching flags.");
LDConfig.log().e(e, "Exception when fetching flags.");
callback.onError(new LDFailure("Exception while fetching flags", e, LDFailure.FailureType.NETWORK_FAILURE));
}

Expand All @@ -81,20 +81,20 @@ public void onResponse(@NonNull Call call, @NonNull final Response response) {
}
if (!response.isSuccessful()) {
if (response.code() == 400) {
LDConfig.LOG.e("Received 400 response when fetching flag values. Please check recommended ProGuard settings");
LDConfig.log().e("Received 400 response when fetching flag values. Please check recommended ProGuard settings");
}
callback.onError(new LDInvalidResponseCodeFailure("Unexpected response when retrieving Feature Flags: " + response + " using url: "
+ request.url() + " with body: " + body, response.code(), true));
}
LDConfig.LOG.d(body);
LDConfig.LOG.d("Cache hit count: %s Cache network Count: %s", client.cache().hitCount(), client.cache().networkCount());
LDConfig.LOG.d("Cache response: %s", response.cacheResponse());
LDConfig.LOG.d("Network response: %s", response.networkResponse());
LDConfig.log().d(body);
LDConfig.log().d("Cache hit count: %s Cache network Count: %s", client.cache().hitCount(), client.cache().networkCount());
LDConfig.log().d("Cache response: %s", response.cacheResponse());
LDConfig.log().d("Network response: %s", response.networkResponse());

JsonObject jsonObject = JsonParser.parseString(body).getAsJsonObject();
callback.onSuccess(jsonObject);
} catch (Exception e) {
LDConfig.LOG.e(e, "Exception when handling response for url: %s with body: %s", request.url(), body);
LDConfig.log().e(e, "Exception when handling response for url: %s with body: %s", request.url(), body);
callback.onError(new LDFailure("Exception while handling flag fetch response", e, LDFailure.FailureType.INVALID_RESPONSE_BODY));
} finally {
if (response != null) {
Expand All @@ -111,7 +111,7 @@ private Request getDefaultRequest(LDUser user) {
if (config.isEvaluationReasons()) {
uri += "?withReasons=true";
}
LDConfig.LOG.d("Attempting to fetch Feature flags using uri: %s", uri);
LDConfig.log().d("Attempting to fetch Feature flags using uri: %s", uri);
return new Request.Builder().url(uri)
.headers(config.headersForEnvironment(environmentName, null))
.build();
Expand All @@ -122,7 +122,7 @@ private Request getReportRequest(LDUser user) {
if (config.isEvaluationReasons()) {
reportUri += "?withReasons=true";
}
LDConfig.LOG.d("Attempting to report user using uri: %s", reportUri);
LDConfig.log().d("Attempting to report user using uri: %s", reportUri);
String userJson = GSON.toJson(user);
RequestBody reportBody = RequestBody.create(userJson, JSON);

Expand Down
Loading