Skip to content

Commit

Permalink
Bugfix/timber cleanup (#92)
Browse files Browse the repository at this point in the history
Relates to #60

Cleaned up timber logging messages to use string formatting rather than concatenation. Log messages should remain the same as before.
Also replaced Log with Timber in the example app.
  • Loading branch information
jamesthacker authored and gavwhela committed Jan 12, 2019
1 parent 3dbab83 commit f2d1343
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 82 deletions.
2 changes: 2 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ dependencies {
// Comment the previous line and uncomment this one to depend on the published artifact:
// compile 'com.launchdarkly:launchdarkly-android-client:1.0.1'

implementation 'com.jakewharton.timber:timber:4.7.0'

testImplementation 'junit:junit:4.12'
}
26 changes: 13 additions & 13 deletions example/src/main/java/com/launchdarkly/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
Expand All @@ -25,9 +24,10 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import timber.log.Timber;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private LDClient ldClient;

@Override
Expand All @@ -54,7 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
try {
ldClient = initFuture.get(10, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
Log.e(TAG, "Exception when awaiting LaunchDarkly Client initialization", e);
Timber.e(e, "Exception when awaiting LaunchDarkly Client initialization");
}
}

Expand All @@ -67,7 +67,7 @@ public void call() {
try {
ldClient.close();
} catch (IOException e) {
Log.e(TAG, "Exception when closing LaunchDarkly Client", e);
Timber.e(e, "Exception when closing LaunchDarkly Client");
}
}
});
Expand All @@ -78,7 +78,7 @@ private void setupFlushButton() {
flushButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "flush onClick");
Timber.i("flush onClick");
MainActivity.this.doSafeClientAction(new LDClientFunction() {
@Override
public void call() {
Expand Down Expand Up @@ -116,7 +116,7 @@ private void setupTrackButton() {
trackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "track onClick");
Timber.i("track onClick");
MainActivity.this.doSafeClientAction(new LDClientFunction() {
@Override
public void call() {
Expand All @@ -132,7 +132,7 @@ private void setupIdentifyButton() {
identify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "identify onClick");
Timber.i("identify onClick");
String userKey = ((EditText) MainActivity.this.findViewById(R.id.userKey_editText)).getText().toString();
final LDUser updatedUser = new LDUser.Builder(userKey).build();
MainActivity.this.doSafeClientAction(new LDClientFunction() {
Expand Down Expand Up @@ -180,7 +180,7 @@ private void setupEval() {
evalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "eval onClick");
Timber.i("eval onClick");
final String flagKey = ((EditText) MainActivity.this.findViewById(R.id.feature_flag_key)).getText().toString();

String type = spinner.getSelectedItem().toString();
Expand All @@ -195,7 +195,7 @@ public String get() {
}
});
logResult = result == null ? "no result" : result;
Log.i(TAG, logResult);
Timber.i(logResult);
((TextView) MainActivity.this.findViewById(R.id.result_textView)).setText(result);
MainActivity.this.doSafeClientAction(new LDClientFunction() {
@Override
Expand All @@ -218,7 +218,7 @@ public String get() {
}
});
logResult = result == null ? "no result" : result;
Log.i(TAG, logResult);
Timber.i(logResult);
((TextView) MainActivity.this.findViewById(R.id.result_textView)).setText(result);
break;
case "Integer":
Expand All @@ -229,7 +229,7 @@ public String get() {
}
});
logResult = result == null ? "no result" : result;
Log.i(TAG, logResult);
Timber.i(logResult);
((TextView) MainActivity.this.findViewById(R.id.result_textView)).setText(result);
break;
case "Float":
Expand All @@ -240,7 +240,7 @@ public String get() {
}
});
logResult = result == null ? "no result" : result;
Log.i(TAG, logResult);
Timber.i(logResult);
((TextView) MainActivity.this.findViewById(R.id.result_textView)).setText(result);
break;
case "Json":
Expand All @@ -251,7 +251,7 @@ public String get() {
}
});
logResult = result == null ? "no result" : result;
Log.i(TAG, logResult);
Timber.i(logResult);
((TextView) MainActivity.this.findViewById(R.id.result_textView)).setText(result);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void postEvents(List<Event> events) {
.addHeader("X-LaunchDarkly-Event-Schema", "3")
.build();

Timber.d("Posting " + events.size() + " event(s) to " + request.url());
Timber.d("Posting %s event(s) to %s", events.size(), request.url());

Response response = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class HttpFeatureFlagFetcher implements FeatureFlagFetcher {
private final Context context;
private final OkHttpClient client;

private volatile boolean isOffline = false;
private volatile boolean isOffline;

static HttpFeatureFlagFetcher init(Context context, LDConfig config) {
instance = new HttpFeatureFlagFetcher(context, config);
Expand Down Expand Up @@ -109,15 +109,15 @@ public void onResponse(Call call, final Response response) throws IOException {
+ request.url() + " with body: " + body);
}
Timber.d(body);
Timber.d("Cache hit count: " + client.cache().hitCount() + " Cache network Count: " + client.cache().networkCount());
Timber.d("Cache hit count: %s Cache network Count: %s", client.cache().hitCount(), client.cache().networkCount());
Timber.d("Cache response: %s", response.cacheResponse());
Timber.d("Network response: %s", response.networkResponse());

JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(body).getAsJsonObject();
doneFuture.set(jsonObject);
} catch (Exception e) {
Timber.e(e, "Exception when handling response for url: " + request.url() + " with body: " + body);
Timber.e(e, "Exception when handling response for url: %s with body: %s", request.url(), body);
doneFuture.setException(e);
} finally {
if (response != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,14 @@ private static <T> boolean validateParameter(T parameter) {
* @return
*/
public static synchronized LDClient init(Application application, LDConfig config, LDUser user, int startWaitSeconds) {
Timber.i("Initializing Client and waiting up to " + startWaitSeconds + " for initialization to complete");
Timber.i("Initializing Client and waiting up to %s for initialization to complete", startWaitSeconds);
Future<LDClient> initFuture = init(application, config, user);
try {
return initFuture.get(startWaitSeconds, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException e) {
Timber.e(e, "Exception during Client initialization");
} catch (TimeoutException e) {
Timber.w("Client did not successfully initialize within " + startWaitSeconds + " seconds. " +
"It could be taking longer than expected to start up");
Timber.w("Client did not successfully initialize within %s seconds. It could be taking longer than expected to start up", startWaitSeconds);
}
return instance;
}
Expand Down Expand Up @@ -191,7 +190,7 @@ protected LDClient(final Application application, @NonNull final LDConfig config
}

instanceId = instanceIdSharedPrefs.getString(INSTANCE_ID_KEY, instanceId);
Timber.i("Using instance id: " + instanceId);
Timber.i("Using instance id: %s", instanceId);

this.fetcher = HttpFeatureFlagFetcher.init(application, config);
this.userManager = UserManager.init(application, fetcher);
Expand Down Expand Up @@ -326,11 +325,9 @@ public Boolean boolVariation(String flagKey, Boolean fallback) {
try {
result = userManager.getCurrentUserSharedPrefs().getBoolean(flagKey, fallback);
} catch (ClassCastException cce) {
Timber.e(cce, "Attempted to get boolean flag that exists as another type for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(cce, "Attempted to get boolean flag that exists as another type for key: %s Returning fallback: %s", flagKey, fallback);
} catch (NullPointerException npe) {
Timber.e(npe, "Attempted to get boolean flag with a default null value for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(npe, "Attempted to get boolean flag with a default null value for key: %s Returning fallback: %s", flagKey, fallback);
}
int version = userManager.getFlagResponseSharedPreferences().getVersionForEvents(flagKey);
int variation = userManager.getFlagResponseSharedPreferences().getStoredVariation(flagKey);
Expand All @@ -347,7 +344,7 @@ public Boolean boolVariation(String flagKey, Boolean fallback) {
updateSummaryEvents(flagKey, new JsonPrimitive(result), new JsonPrimitive(fallback));
sendFlagRequestEvent(flagKey, new JsonPrimitive(result), new JsonPrimitive(fallback), version, variation);
}
Timber.d("boolVariation: returning variation: " + result + " flagKey: " + flagKey + " user key: " + userManager.getCurrentUser().getKeyAsString());
Timber.d("boolVariation: returning variation: %s flagKey: %s user key: %s", result, flagKey, userManager.getCurrentUser().getKeyAsString());
return result;
}

Expand All @@ -369,11 +366,9 @@ public Integer intVariation(String flagKey, Integer fallback) {
try {
result = (int) userManager.getCurrentUserSharedPrefs().getFloat(flagKey, fallback);
} catch (ClassCastException cce) {
Timber.e(cce, "Attempted to get integer flag that exists as another type for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(cce, "Attempted to get integer flag that exists as another type for key: %s Returning fallback: %s", flagKey, fallback);
} catch (NullPointerException npe) {
Timber.e(npe, "Attempted to get integer flag with a default null value for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(npe, "Attempted to get integer flag with a default null value for key: %s Returning fallback: %s", flagKey, fallback);
}
int version = userManager.getFlagResponseSharedPreferences().getVersionForEvents(flagKey);
int variation = userManager.getFlagResponseSharedPreferences().getStoredVariation(flagKey);
Expand All @@ -390,7 +385,7 @@ public Integer intVariation(String flagKey, Integer fallback) {
updateSummaryEvents(flagKey, new JsonPrimitive(result), new JsonPrimitive(fallback));
sendFlagRequestEvent(flagKey, new JsonPrimitive(result), new JsonPrimitive(fallback), version, variation);
}
Timber.d("intVariation: returning variation: " + result + " flagKey: " + flagKey + " user key: " + userManager.getCurrentUser().getKeyAsString());
Timber.d("intVariation: returning variation: %s flagKey: %s user key: %s", result, flagKey, userManager.getCurrentUser().getKeyAsString());
return result;
}

Expand All @@ -412,11 +407,9 @@ public Float floatVariation(String flagKey, Float fallback) {
try {
result = userManager.getCurrentUserSharedPrefs().getFloat(flagKey, fallback);
} catch (ClassCastException cce) {
Timber.e(cce, "Attempted to get float flag that exists as another type for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(cce, "Attempted to get float flag that exists as another type for key: %s Returning fallback: %s", flagKey, fallback);
} catch (NullPointerException npe) {
Timber.e(npe, "Attempted to get float flag with a default null value for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(npe, "Attempted to get float flag with a default null value for key: %s Returning fallback: %s", flagKey, fallback);
}
int version = userManager.getFlagResponseSharedPreferences().getVersionForEvents(flagKey);
int variation = userManager.getFlagResponseSharedPreferences().getStoredVariation(flagKey);
Expand All @@ -433,7 +426,7 @@ public Float floatVariation(String flagKey, Float fallback) {
updateSummaryEvents(flagKey, new JsonPrimitive(result), new JsonPrimitive(fallback));
sendFlagRequestEvent(flagKey, new JsonPrimitive(result), new JsonPrimitive(fallback), version, variation);
}
Timber.d("floatVariation: returning variation: " + result + " flagKey: " + flagKey + " user key: " + userManager.getCurrentUser().getKeyAsString());
Timber.d("floatVariation: returning variation: %s flagKey: %s user key: %s", result, flagKey, userManager.getCurrentUser().getKeyAsString());
return result;
}

Expand All @@ -455,11 +448,9 @@ public String stringVariation(String flagKey, String fallback) {
try {
result = userManager.getCurrentUserSharedPrefs().getString(flagKey, fallback);
} catch (ClassCastException cce) {
Timber.e(cce, "Attempted to get string flag that exists as another type for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(cce, "Attempted to get string flag that exists as another type for key: %s Returning fallback: %s", flagKey, fallback);
} catch (NullPointerException npe) {
Timber.e(npe, "Attempted to get string flag with a default null value for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(npe, "Attempted to get string flag with a default null value for key: %s Returning fallback: %s", flagKey, fallback);
}
int version = userManager.getFlagResponseSharedPreferences().getVersionForEvents(flagKey);
int variation = userManager.getFlagResponseSharedPreferences().getStoredVariation(flagKey);
Expand All @@ -476,7 +467,7 @@ public String stringVariation(String flagKey, String fallback) {
updateSummaryEvents(flagKey, new JsonPrimitive(result), new JsonPrimitive(fallback));
sendFlagRequestEvent(flagKey, new JsonPrimitive(result), new JsonPrimitive(fallback), version, variation);
}
Timber.d("stringVariation: returning variation: " + result + " flagKey: " + flagKey + " user key: " + userManager.getCurrentUser().getKeyAsString());
Timber.d("stringVariation: returning variation: %s flagKey: %s user key: %s", result, flagKey, userManager.getCurrentUser().getKeyAsString());
return result;
}

Expand All @@ -501,20 +492,17 @@ public JsonElement jsonVariation(String flagKey, JsonElement fallback) {
result = new JsonParser().parse(stringResult);
}
} catch (ClassCastException cce) {
Timber.e(cce, "Attempted to get json (string) flag that exists as another type for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(cce, "Attempted to get json (string) flag that exists as another type for key: %s Returning fallback: %s", flagKey, fallback);
} catch (NullPointerException npe) {
Timber.e(npe, "Attempted to get json (string flag with a default null value for key: "
+ flagKey + " Returning fallback: " + fallback);
Timber.e(npe, "Attempted to get json (string flag with a default null value for key: %s Returning fallback: %s", flagKey, fallback);
} catch (JsonSyntaxException jse) {
Timber.e(jse, "Attempted to get json (string flag that exists as another type for key: " +
flagKey + " Returning fallback: " + fallback);
Timber.e(jse, "Attempted to get json (string flag that exists as another type for key: %s Returning fallback: %s", flagKey, fallback);
}
int version = userManager.getFlagResponseSharedPreferences().getVersionForEvents(flagKey);
int variation = userManager.getFlagResponseSharedPreferences().getStoredVariation(flagKey);
updateSummaryEvents(flagKey, result, fallback);
sendFlagRequestEvent(flagKey, result, fallback, version, variation);
Timber.d("jsonVariation: returning variation: " + result + " flagKey: " + flagKey + " user key: " + userManager.getCurrentUser().getKeyAsString());
Timber.d("jsonVariation: returning variation: %s flagKey: %s user key: %s", result, flagKey, userManager.getCurrentUser().getKeyAsString());
return result;
}

Expand Down
Loading

0 comments on commit f2d1343

Please sign in to comment.