Skip to content

Commit 1e3a971

Browse files
authored
feat: injectable logging instead of printing (#62)
1 parent e230c64 commit 1e3a971

File tree

4 files changed

+85
-21
lines changed

4 files changed

+85
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.posthog.java;
2+
3+
import java.util.logging.Level;
4+
import java.util.logging.Logger;
5+
6+
public class DefaultPostHogLogger implements PostHogLogger {
7+
private final Logger logger;
8+
9+
public DefaultPostHogLogger() {
10+
this.logger = Logger.getLogger(PostHog.class.getName());
11+
}
12+
13+
@Override
14+
public void debug(String message) {
15+
logger.fine(message);
16+
}
17+
18+
@Override
19+
public void info(String message) {
20+
logger.info(message);
21+
}
22+
23+
@Override
24+
public void warn(String message) {
25+
logger.warning(message);
26+
}
27+
28+
@Override
29+
public void error(String message) {
30+
logger.severe(message);
31+
}
32+
33+
@Override
34+
public void error(String message, Throwable throwable) {
35+
logger.log(Level.SEVERE, message, throwable);
36+
}
37+
}

posthog/src/main/java/com/posthog/java/HttpSender.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
import okhttp3.Response;
1616

1717
public class HttpSender implements Sender {
18-
private String apiKey;
19-
private String host;
20-
private OkHttpClient client;
21-
private int maxRetries;
22-
private Duration initialRetryInterval;
18+
private final String apiKey;
19+
private final String host;
20+
private final OkHttpClient client;
21+
private final int maxRetries;
22+
private final Duration initialRetryInterval;
23+
private final PostHogLogger logger;
2324

2425
public static class Builder {
2526
// required
@@ -33,6 +34,7 @@ public static class Builder {
3334

3435
// optional
3536
private Duration initialRetryInterval = Duration.ofMillis(500);
37+
private PostHogLogger logger = new DefaultPostHogLogger();
3638

3739
public Builder(String apiKey) {
3840
this.apiKey = apiKey;
@@ -53,6 +55,12 @@ public Builder initialRetryInterval(Duration initialRetryInterval) {
5355
return this;
5456
}
5557

58+
59+
public Builder logger(PostHogLogger logger) {
60+
this.logger = logger;
61+
return this;
62+
}
63+
5664
public HttpSender build() {
5765
return new HttpSender(this);
5866
}
@@ -63,6 +71,7 @@ private HttpSender(Builder builder) {
6371
this.host = builder.host;
6472
this.maxRetries = builder.maxRetries;
6573
this.initialRetryInterval = builder.initialRetryInterval;
74+
this.logger = builder.logger;
6675
this.client = new OkHttpClient();
6776
}
6877

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

131140
try {
@@ -134,7 +143,7 @@ public Boolean send(List<JSONObject> events) {
134143
// TODO this is a blocking sleep, we should use `Future`s here instead
135144
Thread.sleep(retryInterval);
136145
} catch (Exception e) {
137-
e.printStackTrace();
146+
logger.error("Error sending events to PostHog", e);
138147
}
139148
}
140149
}
@@ -145,7 +154,7 @@ private String getRequestBody(List<JSONObject> events) {
145154
jsonObject.put("api_key", apiKey);
146155
jsonObject.put("batch", events);
147156
} catch (JSONException e) {
148-
e.printStackTrace();
157+
logger.error("Error creating event JSON", e);
149158
}
150159
return jsonObject.toString();
151160
}
@@ -156,7 +165,7 @@ public JSONObject post(String route, String distinctId) {
156165
bodyJSON.put("api_key", apiKey);
157166
bodyJSON.put("distinct_id", distinctId);
158167
} catch (JSONException e) {
159-
e.printStackTrace();
168+
logger.error("Error creating event JSON", e);
160169
}
161170

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

172181
if (response.isSuccessful()) {
173-
JSONObject responseJSON = new JSONObject(response.body().string());
174-
175-
return responseJSON;
182+
return new JSONObject(response.body().string());
176183
}
177184

178185
if (response.code() >= 400 && response.code() < 500) {
179-
System.err.println("Error calling API: " + response.body().string());
186+
logger.error("Error calling API: " + response.body().string());
180187
return null;
181188
}
182189
} catch (IOException e) {
183-
e.printStackTrace();
190+
logger.error("Error calling API", e);
184191
} finally {
185192
if (response != null) {
186193
response.close();

posthog/src/main/java/com/posthog/java/PostHog.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import org.json.JSONObject;
1010

1111
public class PostHog {
12-
private QueueManager queueManager;
12+
private final QueueManager queueManager;
1313
private Thread queueManagerThread;
14-
private Sender sender;
14+
private final Sender sender;
15+
private final PostHogLogger logger;
1516

1617
private static abstract class BuilderBase {
1718
protected QueueManager queueManager;
1819
protected Sender sender;
20+
protected PostHogLogger logger = new DefaultPostHogLogger();
1921
}
2022

2123
public static class Builder extends BuilderBase {
@@ -34,8 +36,13 @@ public Builder host(String host) {
3436
return this;
3537
}
3638

39+
public Builder logger(PostHogLogger logger) {
40+
this.logger = logger;
41+
return this;
42+
}
43+
3744
public PostHog build() {
38-
this.sender = new HttpSender.Builder(apiKey).host(host).build();
45+
this.sender = new HttpSender.Builder(apiKey).host(host).logger(logger).build();
3946
this.queueManager = new QueueManager.Builder(this.sender).build();
4047
return new PostHog(this);
4148
}
@@ -57,6 +64,7 @@ public PostHog build() {
5764
private PostHog(BuilderBase builder) {
5865
this.queueManager = builder.queueManager;
5966
this.sender = builder.sender;
67+
this.logger = builder.logger;
6068
startQueueManager();
6169
}
6270

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

@@ -196,7 +203,7 @@ private JSONObject getEventJson(String event, String distinctId, Map<String, Obj
196203
eventJson.put("properties", properties);
197204
}
198205
} catch (JSONException e) {
199-
e.printStackTrace();
206+
logger.error("Error creating event JSON", e);
200207
}
201208
return eventJson;
202209
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.posthog.java;
2+
3+
/**
4+
* Allows you to inject a logger to the PostHog library
5+
* We configure the DefaultPostHogLogger if one is not provided
6+
*/
7+
public interface PostHogLogger {
8+
void debug(String message);
9+
void info(String message);
10+
void warn(String message);
11+
void error(String message);
12+
void error(String message, Throwable throwable);
13+
}

0 commit comments

Comments
 (0)