From 41a65b3a7811f794494d2ed7a399d72f815c7dac Mon Sep 17 00:00:00 2001 From: mchuangatmp <85254535+mchuangatmp@users.noreply.github.com> Date: Wed, 16 Nov 2022 07:59:37 -0800 Subject: [PATCH] fix: Remove System.out.println statements (#16) --- README.md | 11 +++- build.gradle | 10 +++- src/main/java/com/mparticle/ApiClient.java | 17 +++--- src/main/java/com/mparticle/Logger.java | 55 +++++++++++++++++++ .../mparticle/client/DefaultLogHandler.java | 24 ++++++++ .../com/mparticle/client/EventsApiTest.java | 13 +++++ 6 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/mparticle/Logger.java create mode 100644 src/test/java/com/mparticle/client/DefaultLogHandler.java diff --git a/README.md b/README.md index f7e611f..9c53bf6 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Java 1.7 and later. ### Add the Dependency -The SDK is available via Maven Central and jCenter. +The SDK is available via Maven Central. #### Gradle @@ -223,6 +223,15 @@ Call singleResult = api.uploadEvents(batch); Response singleResponse = singleResult.execute(); ``` +### Logging + +By default, logging is ignored. Please implement your own LogHandler to handle log statements. + +```java +Logger.setLogHandler(new DefaultLogHandler()); +Logger.info("Statement"); +``` + ### License [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0) diff --git a/build.gradle b/build.gradle index 941cf7f..1bc3c28 100644 --- a/build.gradle +++ b/build.gradle @@ -7,12 +7,12 @@ apply from: '.scripts/maven.gradle' buildscript { repositories { - jcenter() + mavenCentral() } } repositories { - jcenter() + mavenCentral() } sourceCompatibility = JavaVersion.VERSION_1_7 @@ -47,6 +47,9 @@ ext { junit_version = "4.12" threetenbp_version = "1.3.5" json_fire_version = "1.8.0" + slf4j_core_version = "1.2.3" + slf4j_classic_version = "1.2.3" + slf4j_version = "1.7.30" } dependencies { @@ -58,4 +61,7 @@ dependencies { compile "io.gsonfire:gson-fire:$json_fire_version" compile "org.threeten:threetenbp:$threetenbp_version" testCompile "junit:junit:$junit_version" + testCompile "ch.qos.logback:logback-core:$slf4j_core_version" + testCompile "ch.qos.logback:logback-classic:$slf4j_classic_version" + testCompile "org.slf4j:slf4j-api:$slf4j_version" } diff --git a/src/main/java/com/mparticle/ApiClient.java b/src/main/java/com/mparticle/ApiClient.java index a7a77b3..a0fce01 100644 --- a/src/main/java/com/mparticle/ApiClient.java +++ b/src/main/java/com/mparticle/ApiClient.java @@ -12,7 +12,6 @@ import java.util.Map; import okhttp3.Interceptor; -import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Protocol; import okhttp3.RequestBody; @@ -145,9 +144,9 @@ static class RateLimitInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { - System.out.println("Starting request: " + chain.request().toString()); + Logger.info("Starting request: " + chain.request().toString()); if (retryAfter != null && System.currentTimeMillis() < retryAfter) { - System.out.println("This endpoint is currently rate-limited, please retry after" + retryAfter + "ms, returning a local 429 response"); + Logger.info("This endpoint is currently rate-limited, please retry after" + retryAfter + "ms, returning a local 429 response"); return new Response.Builder() .request(chain.request()) .addHeader("RETRY_AFTER", retryAfter.toString()) @@ -159,8 +158,8 @@ public Response intercept(Chain chain) throws IOException { } Response response = chain.proceed(chain.request()); - System.out.println("Response " + response.code()); - System.out.println(response); + Logger.info("Response " + response.code()); + Logger.info(String.valueOf(response)); if (response.code() == 429) { //Most HttpUrlConnectionImpl's are case insensitive, but the interface //doesn't actually restrict it so let's be safe and check. @@ -170,17 +169,17 @@ public Response intercept(Chain chain) throws IOException { } try { if (retryAfterString == null) { - System.out.println("No Retry-After value found"); + Logger.info("No Retry-After value found"); } else { long parsedThrottle = Long.parseLong(retryAfterString) * 1000; if (parsedThrottle > 0) { retryAfter = System.currentTimeMillis() + parsedThrottle; - System.out.println("Retry-After value: " + parsedThrottle); - System.out.println("Next request may not be attempted for " + retryAfter + "ms"); + Logger.info("Retry-After value: " + parsedThrottle); + Logger.info("Next request may not be attempted for " + retryAfter + "ms"); } } } catch (NumberFormatException nfe) { - System.out.println("Unable to parse retry-after header, next request will not be rate-limited."); + Logger.info("Unable to parse retry-after header, next request will not be rate-limited."); } } return response; diff --git a/src/main/java/com/mparticle/Logger.java b/src/main/java/com/mparticle/Logger.java new file mode 100644 index 0000000..e66a101 --- /dev/null +++ b/src/main/java/com/mparticle/Logger.java @@ -0,0 +1,55 @@ +package com.mparticle; + +public class Logger { + private static AbstractLogHandler logHandler; + public enum LogLevel { + INFO, ERROR + } + + public static void info(String message) { + if(getLogHandler() != null) + getLogHandler().log(LogLevel.INFO, null, message); + } + + public static void error(String message) { + if(getLogHandler() != null) + getLogHandler().log(LogLevel.ERROR, null, message); + } + + public static void error(Throwable error) { + if(getLogHandler() != null) + getLogHandler().log(LogLevel.ERROR, error, error.toString()); + } + + /** + * Testing method. Use this method to intercept Logs, or customize what happens when something is logged. + * For example, you can use this method to throw an exception every time an "error" log is called. + * @param logListener + */ + public static void setLogHandler(AbstractLogHandler logListener) { + Logger.logHandler = logListener; + } + + public static AbstractLogHandler getLogHandler() { + return logHandler; + } + + public abstract static class AbstractLogHandler { + + public void log(LogLevel priority, Throwable error, String message) { + if (message != null) { + switch (priority) { + case ERROR: + error(error, message); + break; + case INFO: + info(message); + } + } + } + + public abstract void info(String message); + public abstract void error(Throwable error, String message); + } +} + diff --git a/src/test/java/com/mparticle/client/DefaultLogHandler.java b/src/test/java/com/mparticle/client/DefaultLogHandler.java new file mode 100644 index 0000000..e34beec --- /dev/null +++ b/src/test/java/com/mparticle/client/DefaultLogHandler.java @@ -0,0 +1,24 @@ +package com.mparticle.client; + +import com.mparticle.Logger.AbstractLogHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DefaultLogHandler extends AbstractLogHandler { + + private static final Logger Log = LoggerFactory.getLogger(DefaultLogHandler.class); + @Override + public void info(String message) { + Log.info(message); + + } + + @Override + public void error(Throwable error, String message) { + if (error != null) { + Log.error(error.toString()); + } else { + Log.error(message); + } + } +} diff --git a/src/test/java/com/mparticle/client/EventsApiTest.java b/src/test/java/com/mparticle/client/EventsApiTest.java index fb2e552..3c78538 100644 --- a/src/test/java/com/mparticle/client/EventsApiTest.java +++ b/src/test/java/com/mparticle/client/EventsApiTest.java @@ -2,6 +2,7 @@ import com.google.gson.Gson; import com.mparticle.ApiClient; +import com.mparticle.Logger; import com.mparticle.model.*; import org.junit.Assert; import org.junit.Before; @@ -28,6 +29,18 @@ public void setup() { .createService(EventsApi.class); } + @Test + public void logStringWtihLogger() { + Logger.setLogHandler(new DefaultLogHandler()); + Logger.info("Test"); + Assert.assertNotNull(Logger.getLogHandler()); + } + + @Test + public void logStringNoLogger() { + Logger.info("Test"); + Assert.assertNull(Logger.getLogHandler()); + } @Test public void uploadUserIdentitiesAndAttributes() throws Exception {