Skip to content

Commit 0a0fada

Browse files
authored
🔊 add default logging (#356)
1 parent 8edfd57 commit 0a0fada

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎src/main/java/com/mindee/v2/MindeeClient.java‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* Entry point for the Mindee **V2** API features.
2424
*/
2525
public class MindeeClient {
26+
private static final System.Logger logger = System.getLogger(MindeeClient.class.getName());
27+
2628
private final MindeeApiV2 mindeeApi;
2729

2830
/** Uses an API key read from the environment variables. */
@@ -50,6 +52,7 @@ public JobResponse enqueue(
5052
LocalInputSource inputSource,
5153
BaseProductParameters params
5254
) throws IOException {
55+
logger.log(System.Logger.Level.INFO, "Enqueuing: local source");
5356
return mindeeApi.reqPostEnqueue(inputSource, params);
5457
}
5558

@@ -63,6 +66,7 @@ public JobResponse enqueue(
6366
URLInputSource inputSource,
6467
BaseProductParameters params
6568
) throws IOException {
69+
logger.log(System.Logger.Level.INFO, "Enqueuing: URL source");
6670
inputSource.validateSecure();
6771
return mindeeApi.reqPostEnqueue(inputSource, params);
6872
}
@@ -72,6 +76,7 @@ public JobResponse enqueue(
7276
* Can be used for polling.
7377
*/
7478
public JobResponse getJob(String jobId) {
79+
logger.log(System.Logger.Level.INFO, "Getting job ID: {0}", jobId);
7580
if (jobId == null || jobId.trim().isEmpty()) {
7681
throw new IllegalArgumentException("jobId must not be null or blank.");
7782
}
@@ -86,6 +91,7 @@ public <TResponse extends CommonResponse> TResponse getResult(
8691
Class<TResponse> responseClass,
8792
String inferenceId
8893
) {
94+
logger.log(System.Logger.Level.INFO, "Getting result with ID: {0}", inferenceId);
8995
if (inferenceId == null || inferenceId.trim().isEmpty()) {
9096
throw new IllegalArgumentException("inferenceId must not be null or blank.");
9197
}
@@ -100,6 +106,7 @@ public <TResponse extends CommonResponse> TResponse getResultFromUrl(
100106
Class<TResponse> responseClass,
101107
String inferenceUrl
102108
) {
109+
logger.log(System.Logger.Level.INFO, "Getting result at: {0}", inferenceUrl);
103110
if (inferenceUrl == null || inferenceUrl.trim().isEmpty()) {
104111
throw new IllegalArgumentException("inferenceUrl must not be null or blank.");
105112
}
@@ -147,7 +154,13 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
147154
PollingOptions pollingOptions
148155
) throws IOException, InterruptedException {
149156
JobResponse job = enqueue(inputSource, params);
150-
return pollAndFetch(responseClass, job, pollingOptions);
157+
logger
158+
.log(
159+
System.Logger.Level.INFO,
160+
"Successfully enqueued document with job ID {0}",
161+
job.getJob().getId()
162+
);
163+
return pollForResult(responseClass, job, pollingOptions);
151164
}
152165

153166
/**
@@ -192,7 +205,13 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
192205
) throws IOException, InterruptedException {
193206
inputSource.validateSecure();
194207
JobResponse job = enqueue(inputSource, params);
195-
return pollAndFetch(responseClass, job, pollingOptions);
208+
logger
209+
.log(
210+
System.Logger.Level.INFO,
211+
"Successfully enqueued document with job ID {0}",
212+
job.getJob().getId()
213+
);
214+
return pollForResult(responseClass, job, pollingOptions);
196215
}
197216

198217
/**
@@ -251,11 +270,17 @@ public SearchResponse searchModels(String modelName, String modelType) {
251270
* @return an instance of {@link ExtractionResponse}.
252271
* @throws InterruptedException Throws if interrupted.
253272
*/
254-
private <TResponse extends CommonResponse> TResponse pollAndFetch(
273+
private <TResponse extends CommonResponse> TResponse pollForResult(
255274
Class<TResponse> responseClass,
256275
JobResponse initialJob,
257276
PollingOptions pollingOptions
258277
) throws InterruptedException {
278+
logger
279+
.log(
280+
System.Logger.Level.DEBUG,
281+
"Waiting {0} seconds before attempting to retrieve the result...",
282+
pollingOptions.getInitialDelaySec()
283+
);
259284
interruptibleSleep((long) (pollingOptions.getInitialDelaySec() * 1000), pollingOptions);
260285

261286
JobResponse resp = initialJob;
@@ -265,12 +290,20 @@ private <TResponse extends CommonResponse> TResponse pollAndFetch(
265290

266291
while (attempts < max) {
267292
interruptibleSleep(intervalMillis, pollingOptions);
293+
logger.log(System.Logger.Level.DEBUG, "Poll attempt {0} of {1}", attempts + 1, max);
268294
resp = getJob(initialJob.getJob().getId());
269295

270296
if (resp.getJob().getStatus().equals("Failed")) {
271297
attempts = max;
272298
}
273299
if (resp.getJob().getStatus().equals("Processed")) {
300+
logger
301+
.log(
302+
System.Logger.Level.DEBUG,
303+
"Job ID {0} completed processing at: {1}",
304+
resp.getJob().getId(),
305+
resp.getJob().getCompletedAt()
306+
);
274307
return getResult(responseClass, resp.getJob().getId());
275308
}
276309
attempts++;

‎src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*/
3737
public final class MindeeHttpApiV2 extends MindeeApiV2 {
3838

39+
private static final System.Logger logger = System.getLogger(MindeeHttpApiV2.class.getName());
3940
private static final ObjectMapper mapper = JsonMapper.builder().findAndAddModules().build();
4041

4142
/**
@@ -91,6 +92,8 @@ public JobResponse reqPostEnqueue(
9192
);
9293
parameters.getRequestParameters().forEach(builder::addTextBody);
9394
post.setEntity(builder.build());
95+
96+
logger.log(System.Logger.Level.DEBUG, "HTTP POST to {0} ...", url);
9497
return executeAPIRequest(post, JobResponse.class);
9598
}
9699

@@ -113,6 +116,8 @@ public JobResponse reqPostEnqueue(URLInputSource inputSource, BaseProductParamet
113116
builder.addTextBody("url", inputSource.getUrl().toString());
114117
options.getRequestParameters().forEach(builder::addTextBody);
115118
post.setEntity(builder.build());
119+
120+
logger.log(System.Logger.Level.DEBUG, "HTTP POST to {0} ...", url);
116121
return executeAPIRequest(post, JobResponse.class);
117122
}
118123

@@ -125,6 +130,7 @@ public JobResponse reqGetJobById(String jobId) {
125130
var noRedirect = RequestConfig.custom().setRedirectsEnabled(false).build();
126131
get.setConfig(noRedirect);
127132

133+
logger.log(System.Logger.Level.DEBUG, "HTTP GET to {0}...", url);
128134
return this.executeAPIRequest(get, JobResponse.class);
129135
}
130136

@@ -157,6 +163,8 @@ public <TResponse extends CommonResponse> TResponse reqGetResultByUrl(
157163
}
158164
validateInferenceUrl(inferenceUrl);
159165
var get = new HttpGet(inferenceUrl);
166+
167+
logger.log(System.Logger.Level.DEBUG, "HTTP GET to {0}...", inferenceUrl);
160168
return executeAPIRequest(get, responseClass);
161169
}
162170

@@ -173,6 +181,8 @@ public <TSearchResponse extends BaseSearchResponse> TSearchResponse reqGetSearch
173181
}
174182
parameters.getRequestParameters().forEach(url::addParameter);
175183
var get = new HttpGet(url.toString());
184+
185+
logger.log(System.Logger.Level.INFO, "Searching {0} ...", productInfo.slug());
176186
return this.executeAPIRequest(get, parameters.getResponseClass());
177187
}
178188

@@ -187,6 +197,8 @@ public SearchResponse reqGetSearch(ModelSearchParameters parameters) {
187197
}
188198
parameters.getRequestParameters().forEach(url::addParameter);
189199
var get = new HttpGet(url.toString());
200+
201+
logger.log(System.Logger.Level.INFO, "Model search...");
190202
return this.executeAPIRequest(get, SearchResponse.class);
191203
}
192204

@@ -286,6 +298,7 @@ private <TResponse extends CommonResponse> TResponse executeAPIRequest(
286298
}
287299
try {
288300
var raw = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
301+
logger.log(System.Logger.Level.DEBUG, "HTTP response: {0}", raw);
289302
return deserializeOrThrow(raw, responseClass, response.getCode());
290303
} finally {
291304
EntityUtils.consumeQuietly(responseEntity);
@@ -303,6 +316,8 @@ private MindeeHttpExceptionV2 getHttpError(ClassicHttpResponse response) {
303316
? ""
304317
: EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
305318

319+
logger.log(System.Logger.Level.DEBUG, "HTTP response: {0}", rawBody);
320+
306321
var errorResponse = mapper.readValue(rawBody, ErrorResponse.class);
307322

308323
if (errorResponse.getDetail() == null) {

0 commit comments

Comments
 (0)