Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pure java Logger #167

Closed
wants to merge 3 commits into from
Closed
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
Expand Up @@ -48,7 +48,11 @@ public CoronerResponse rxIdFilter(final String rxId, final List<String> customAt
public CoronerResponse errorTypeTimestampFilter(final String errorType, final String timestampLeast, final String timestampMost, final List<String> customAttributes) throws CoronerResponseException, IOException, CoronerHttpException {
final List<String> attributes = concatAttributes(customAttributes);

backtraceio.coroner.common.Logger.d("CoronerHttpClient errorTypeTimestampFilter started attributes", String.valueOf(attributes));


final JsonObject coronerQuery = this.coronerQueries.filterByErrorTypeAndTimestamp(errorType, timestampLeast, timestampMost, attributes);
backtraceio.coroner.common.Logger.d("CoronerHttpClient errorTypeTimestampFilter coronerQuery JsonObject", String.valueOf(coronerQuery));

return makeRequest(coronerQuery);
}
Expand All @@ -60,8 +64,12 @@ private List<String> concatAttributes(final List<String> customAttributes) {
}

private CoronerResponse makeRequest(final JsonObject coronerQuery) throws CoronerResponseException, IOException, CoronerHttpException {
backtraceio.coroner.common.Logger.d("CoronerHttpClient makeRequest started", "started");


final CoronerApiResponse response = this.coronerHttpClient.get(coronerQuery.toString());

backtraceio.coroner.common.Logger.d("CoronerHttpClient CoronerResponse makeRequest", response.toString());

if (response.error != null) {
throw new CoronerResponseException(response.getError());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,25 @@ public CoronerHttpClient(final String apiUrl, final String coronerToken) {
public CoronerApiResponse get(final String requestJson) throws CoronerHttpException, IOException {
final HttpURLConnection urlConnection = prepareHttpRequest(requestJson);
final int statusCode = urlConnection.getResponseCode();

backtraceio.coroner.common.Logger.d("CoronerHttpClient", "invoked");
backtraceio.coroner.common.Logger.d("CoronerHttpClient api url", apiUrl);
backtraceio.coroner.common.Logger.d("CoronerHttpClient request json", requestJson);
backtraceio.coroner.common.Logger.d("CoronerHttpClient statusCode", String.valueOf(statusCode));
if (statusCode != HttpURLConnection.HTTP_OK) {
String message = getResponseMessage(urlConnection);

backtraceio.coroner.common.Logger.d("CoronerHttpClient getResponseMessage message", message);

message = (Common.isNullOrEmpty(message)) ?
urlConnection.getResponseMessage() : message;
throw new CoronerHttpException(statusCode, String.format("%s: %s", statusCode, message));
}

final String resultJson = getResponseMessage(urlConnection);

backtraceio.coroner.common.Logger.d("CoronerHttpClient resultJson", resultJson);


return GsonWrapper.fromJson(
resultJson,
CoronerApiResponse.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package backtraceio.coroner.common;

// Interface that the instrumentation test can implement to call android.util.Log
public interface AndroidLogDelegate {
void d(String tag, String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package backtraceio.coroner.common;

public final class Logger {
private static AndroidLogDelegate delegate;

private Logger() {
// no instances
}

/**
* Called from the Android test code to register a delegate that can log to Logcat.
*/
public static synchronized void setDelegate(AndroidLogDelegate logDelegate) {
delegate = logDelegate;
}

/**
* Pure Java code can call this method instead of System.out.println().
*/
public static synchronized void d(String tag, String message) {
if (delegate != null) {
delegate.d(tag, message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@
public class GsonWrapper {

public static <T> T fromJson(final String json, final Class<T> type) {
backtraceio.coroner.common.Logger.d("CoronerHttpClient GsonWrapper fromJson", "started");


Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
.registerTypeAdapter(CoronerResponseGroup.class, new CoronerResponseGroupDeserializer())
.create();
backtraceio.coroner.common.Logger.d("CoronerHttpClient GsonWrapper", gson.toString());


backtraceio.coroner.common.Logger.d("CoronerHttpClient GsonWrapper fromJson", "json");
backtraceio.coroner.common.Logger.d("CoronerHttpClient GsonWrapper fromJson", json);

backtraceio.coroner.common.Logger.d("CoronerHttpClient GsonWrapper fromJson", "started");
T result = gson.fromJson(json, type);
backtraceio.coroner.common.Logger.d("CoronerHttpClient GsonWrapper fromJson", "result");
backtraceio.coroner.common.Logger.d("CoronerHttpClient GsonWrapper fromJson", result.toString());

return gson.fromJson(json, type);
return result;
}

}
2 changes: 1 addition & 1 deletion example-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test:rules:1.6.1'
androidTestImplementation 'net.jodah:concurrentunit:0.4.4'
androidTestImplementation 'net.jodah:concurrentunit:0.4.6'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
implementation project(':backtrace-library')
androidTestImplementation project(path: ':coroner-client')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package backtraceio.backtraceio;

import backtraceio.coroner.common.AndroidLogDelegate;
import android.util.Log;

// Implementation that calls the real android.util.Log
public class CoronerLogDelegate implements AndroidLogDelegate {
@Override
public void d(String tag, String message) {
Log.d(tag, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import org.junit.runner.RunWith;

import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import backtraceio.coroner.common.Logger;
import backtraceio.coroner.response.CoronerResponse;
import backtraceio.coroner.response.CoronerResponseProcessingException;

Expand All @@ -44,6 +46,11 @@ public void enableMetricsAndBreadcrumbs() {
onView(withId(R.id.enableBreadcrumbs)).perform(click());
}

@Before
public void setUp() {
Logger.setDelegate(new CoronerLogDelegate());
}

@Test
public void useAppContext() {
// Context of the app under test.
Expand All @@ -70,8 +77,13 @@ public void handledException() throws TimeoutException, CoronerResponseProcessin
// THEN
CoronerResponse response = null;

// Test
backtraceio.coroner.common.Logger.d("CoronerHttpClient", "handledException");

try {
response = this.getCoronerClient().rxIdFilter(rxId[0], Arrays.asList("error.message"));
backtraceio.coroner.common.Logger.d("CoronerHttpClient handledException response", response.toString());

} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
Expand All @@ -96,13 +108,21 @@ public void dumpWithoutCrash() throws CoronerResponseProcessingException, Interr
onView(withId(R.id.dumpWithoutCrash)).perform(click()); // UI action
Thread.sleep(THREAD_SLEEP_TIME_MS * 10);

// Test
Logger.d("CoronerHttpClient", "Hello from pure Java code!");

// THEN
try {
response = this.getCoronerClient().errorTypeTimestampFilter("Crash",
Long.toString(timestampStart),
Long.toString(this.getSecondsTimestampNowGMT()),
Arrays.asList("error.message"));
Collections.singletonList("error.message"));

backtraceio.coroner.common.Logger.d("CoronerHttpClient dumpWithoutCrash response", response.toString());

} catch (Exception ex) {
backtraceio.coroner.common.Logger.d("CoronerHttpClient dumpWithoutCrash Exception", ex.getMessage());

Assert.fail(ex.getMessage());
}

Expand Down Expand Up @@ -132,7 +152,7 @@ public void unhandledException() throws CoronerResponseProcessingException, Inte
response = this.getCoronerClient().errorTypeTimestampFilter("Crash",
Long.toString(timestampStart),
Long.toString(this.getSecondsTimestampNowGMT()),
Arrays.asList("error.message"));
Collections.singletonList("error.message"));
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
Expand Down
Loading