Skip to content

Commit

Permalink
fix: Remove System.out.println statements (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchuangatmp authored Nov 16, 2022
1 parent 9d2e468 commit 41a65b3
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 12 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -223,6 +223,15 @@ Call<Void> singleResult = api.uploadEvents(batch);
Response<Void> 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)
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ apply from: '.scripts/maven.gradle'

buildscript {
repositories {
jcenter()
mavenCentral()
}
}

repositories {
jcenter()
mavenCentral()
}

sourceCompatibility = JavaVersion.VERSION_1_7
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
}
17 changes: 8 additions & 9 deletions src/main/java/com/mparticle/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.RequestBody;
Expand Down Expand Up @@ -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())
Expand All @@ -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.
Expand All @@ -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;
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/mparticle/Logger.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

24 changes: 24 additions & 0 deletions src/test/java/com/mparticle/client/DefaultLogHandler.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/mparticle/client/EventsApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 41a65b3

Please sign in to comment.