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
37 changes: 37 additions & 0 deletions posthog/src/main/java/com/posthog/java/DefaultPostHogLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.posthog.java;

import java.util.logging.Level;
import java.util.logging.Logger;

public class DefaultPostHogLogger implements PostHogLogger {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could probably use https://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html but having PostHog logger is also fine

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I'm far enough away from java ecosystem that I thought if we have an interface then folk can use just about whatever they want and avoid coupling ourselves to something only for people to say "but why not OmniLogger.io"

private final Logger logger;

public DefaultPostHogLogger() {
this.logger = Logger.getLogger(PostHog.class.getName());
}

@Override
public void debug(String message) {
logger.fine(message);
}

@Override
public void info(String message) {
logger.info(message);
}

@Override
public void warn(String message) {
logger.warning(message);
}

@Override
public void error(String message) {
logger.severe(message);
}

@Override
public void error(String message, Throwable throwable) {
logger.log(Level.SEVERE, message, throwable);
}
}
37 changes: 22 additions & 15 deletions posthog/src/main/java/com/posthog/java/HttpSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import okhttp3.Response;

public class HttpSender implements Sender {
private String apiKey;
private String host;
private OkHttpClient client;
private int maxRetries;
private Duration initialRetryInterval;
private final String apiKey;
private final String host;
private final OkHttpClient client;
private final int maxRetries;
private final Duration initialRetryInterval;
private final PostHogLogger logger;

public static class Builder {
// required
Expand All @@ -33,6 +34,7 @@ public static class Builder {

// optional
private Duration initialRetryInterval = Duration.ofMillis(500);
private PostHogLogger logger = new DefaultPostHogLogger();

public Builder(String apiKey) {
this.apiKey = apiKey;
Expand All @@ -53,6 +55,12 @@ public Builder initialRetryInterval(Duration initialRetryInterval) {
return this;
}


public Builder logger(PostHogLogger logger) {
this.logger = logger;
return this;
}

public HttpSender build() {
return new HttpSender(this);
}
Expand All @@ -63,6 +71,7 @@ private HttpSender(Builder builder) {
this.host = builder.host;
this.maxRetries = builder.maxRetries;
this.initialRetryInterval = builder.initialRetryInterval;
this.logger = builder.logger;
this.client = new OkHttpClient();
}

Expand Down Expand Up @@ -102,7 +111,7 @@ public Boolean send(List<JSONObject> events) {
// TODO: verify if we need to retry on IOException, this may
// already be handled by OkHTTP. See
// https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/retry-on-connection-failure/
e.printStackTrace();
logger.error("Error sending events to PostHog", e);
} finally {
// must always close an OkHTTP response
// https://square.github.io/okhttp/4.x/okhttp/okhttp3/-call/execute/
Expand All @@ -125,7 +134,7 @@ public Boolean send(List<JSONObject> events) {
// On retries, make sure we log the response code or exception such
// that people will know if something is up, ensuring we include the
// retry count and how long we will wait before retrying.
System.out.println("Retrying sending events to PostHog after " + retries + " retries. Waiting for "
logger.debug("Retrying sending events to PostHog after " + retries + " retries. Waiting for "
+ retryInterval + "ms before retrying.");

try {
Expand All @@ -134,7 +143,7 @@ public Boolean send(List<JSONObject> events) {
// TODO this is a blocking sleep, we should use `Future`s here instead
Thread.sleep(retryInterval);
} catch (Exception e) {
e.printStackTrace();
logger.error("Error sending events to PostHog", e);
}
}
}
Expand All @@ -145,7 +154,7 @@ private String getRequestBody(List<JSONObject> events) {
jsonObject.put("api_key", apiKey);
jsonObject.put("batch", events);
} catch (JSONException e) {
e.printStackTrace();
logger.error("Error creating event JSON", e);
}
return jsonObject.toString();
}
Expand All @@ -156,7 +165,7 @@ public JSONObject post(String route, String distinctId) {
bodyJSON.put("api_key", apiKey);
bodyJSON.put("distinct_id", distinctId);
} catch (JSONException e) {
e.printStackTrace();
logger.error("Error creating event JSON", e);
}

Response response = null;
Expand All @@ -170,17 +179,15 @@ public JSONObject post(String route, String distinctId) {
response = call.execute();

if (response.isSuccessful()) {
JSONObject responseJSON = new JSONObject(response.body().string());

return responseJSON;
return new JSONObject(response.body().string());
}

if (response.code() >= 400 && response.code() < 500) {
System.err.println("Error calling API: " + response.body().string());
logger.error("Error calling API: " + response.body().string());
return null;
}
} catch (IOException e) {
e.printStackTrace();
logger.error("Error calling API", e);
} finally {
if (response != null) {
response.close();
Expand Down
19 changes: 13 additions & 6 deletions posthog/src/main/java/com/posthog/java/PostHog.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import org.json.JSONObject;

public class PostHog {
private QueueManager queueManager;
private final QueueManager queueManager;
private Thread queueManagerThread;
private Sender sender;
private final Sender sender;
private final PostHogLogger logger;

private static abstract class BuilderBase {
protected QueueManager queueManager;
protected Sender sender;
protected PostHogLogger logger = new DefaultPostHogLogger();
}

public static class Builder extends BuilderBase {
Expand All @@ -34,8 +36,13 @@ public Builder host(String host) {
return this;
}

public Builder logger(PostHogLogger logger) {
this.logger = logger;
return this;
}

public PostHog build() {
this.sender = new HttpSender.Builder(apiKey).host(host).build();
this.sender = new HttpSender.Builder(apiKey).host(host).logger(logger).build();
this.queueManager = new QueueManager.Builder(this.sender).build();
return new PostHog(this);
}
Expand All @@ -57,6 +64,7 @@ public PostHog build() {
private PostHog(BuilderBase builder) {
this.queueManager = builder.queueManager;
this.sender = builder.sender;
this.logger = builder.logger;
startQueueManager();
}

Expand All @@ -65,8 +73,7 @@ public void shutdown() {
try {
queueManagerThread.join(); // wait for the current items in queue to be sent
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("Error shutting down PostHog", e);
}
}

Expand Down Expand Up @@ -196,7 +203,7 @@ private JSONObject getEventJson(String event, String distinctId, Map<String, Obj
eventJson.put("properties", properties);
}
} catch (JSONException e) {
e.printStackTrace();
logger.error("Error creating event JSON", e);
}
return eventJson;
}
Expand Down
13 changes: 13 additions & 0 deletions posthog/src/main/java/com/posthog/java/PostHogLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.posthog.java;

/**
* Allows you to inject a logger to the PostHog library
* We configure the DefaultPostHogLogger if one is not provided
*/
public interface PostHogLogger {
void debug(String message);
void info(String message);
void warn(String message);
void error(String message);
void error(String message, Throwable throwable);
}