15
15
import okhttp3 .Response ;
16
16
17
17
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 ;
23
24
24
25
public static class Builder {
25
26
// required
@@ -33,6 +34,7 @@ public static class Builder {
33
34
34
35
// optional
35
36
private Duration initialRetryInterval = Duration .ofMillis (500 );
37
+ private PostHogLogger logger = new DefaultPostHogLogger ();
36
38
37
39
public Builder (String apiKey ) {
38
40
this .apiKey = apiKey ;
@@ -53,6 +55,12 @@ public Builder initialRetryInterval(Duration initialRetryInterval) {
53
55
return this ;
54
56
}
55
57
58
+
59
+ public Builder logger (PostHogLogger logger ) {
60
+ this .logger = logger ;
61
+ return this ;
62
+ }
63
+
56
64
public HttpSender build () {
57
65
return new HttpSender (this );
58
66
}
@@ -63,6 +71,7 @@ private HttpSender(Builder builder) {
63
71
this .host = builder .host ;
64
72
this .maxRetries = builder .maxRetries ;
65
73
this .initialRetryInterval = builder .initialRetryInterval ;
74
+ this .logger = builder .logger ;
66
75
this .client = new OkHttpClient ();
67
76
}
68
77
@@ -102,7 +111,7 @@ public Boolean send(List<JSONObject> events) {
102
111
// TODO: verify if we need to retry on IOException, this may
103
112
// already be handled by OkHTTP. See
104
113
// 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 );
106
115
} finally {
107
116
// must always close an OkHTTP response
108
117
// https://square.github.io/okhttp/4.x/okhttp/okhttp3/-call/execute/
@@ -125,7 +134,7 @@ public Boolean send(List<JSONObject> events) {
125
134
// On retries, make sure we log the response code or exception such
126
135
// that people will know if something is up, ensuring we include the
127
136
// 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 "
129
138
+ retryInterval + "ms before retrying." );
130
139
131
140
try {
@@ -134,7 +143,7 @@ public Boolean send(List<JSONObject> events) {
134
143
// TODO this is a blocking sleep, we should use `Future`s here instead
135
144
Thread .sleep (retryInterval );
136
145
} catch (Exception e ) {
137
- e . printStackTrace ( );
146
+ logger . error ( "Error sending events to PostHog" , e );
138
147
}
139
148
}
140
149
}
@@ -145,7 +154,7 @@ private String getRequestBody(List<JSONObject> events) {
145
154
jsonObject .put ("api_key" , apiKey );
146
155
jsonObject .put ("batch" , events );
147
156
} catch (JSONException e ) {
148
- e . printStackTrace ( );
157
+ logger . error ( "Error creating event JSON" , e );
149
158
}
150
159
return jsonObject .toString ();
151
160
}
@@ -156,7 +165,7 @@ public JSONObject post(String route, String distinctId) {
156
165
bodyJSON .put ("api_key" , apiKey );
157
166
bodyJSON .put ("distinct_id" , distinctId );
158
167
} catch (JSONException e ) {
159
- e . printStackTrace ( );
168
+ logger . error ( "Error creating event JSON" , e );
160
169
}
161
170
162
171
Response response = null ;
@@ -170,17 +179,15 @@ public JSONObject post(String route, String distinctId) {
170
179
response = call .execute ();
171
180
172
181
if (response .isSuccessful ()) {
173
- JSONObject responseJSON = new JSONObject (response .body ().string ());
174
-
175
- return responseJSON ;
182
+ return new JSONObject (response .body ().string ());
176
183
}
177
184
178
185
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 ());
180
187
return null ;
181
188
}
182
189
} catch (IOException e ) {
183
- e . printStackTrace ( );
190
+ logger . error ( "Error calling API" , e );
184
191
} finally {
185
192
if (response != null ) {
186
193
response .close ();
0 commit comments