Skip to content

Commit 14190d7

Browse files
authored
feat(java): enable custom level debug APIC-535 (#705)
1 parent 750523b commit 14190d7

File tree

6 files changed

+84
-23
lines changed

6 files changed

+84
-23
lines changed

clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/ApiClient.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,11 @@ public boolean isDebugging() {
105105
}
106106

107107
/**
108-
* Enable/disable debugging for this API client.
109-
*
110-
* @param debugging To enable (true) or disable (false) debugging
108+
* Set the log level of the requester
111109
* @return ApiClient
112110
*/
113-
public ApiClient setDebugging(boolean debugging) {
114-
requester.setDebugging(debugging);
111+
public ApiClient setLogLevel(LogLevel level) {
112+
requester.setLogLevel(level);
115113
return this;
116114
}
117115

clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/HttpRequester.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@
1313
import okhttp3.Request;
1414
import okhttp3.Response;
1515
import okhttp3.logging.HttpLoggingInterceptor;
16-
import okhttp3.logging.HttpLoggingInterceptor.Level;
1716

1817
public class HttpRequester implements Requester {
1918

2019
private RetryStrategy retryStrategy;
2120
private OkHttpClient httpClient;
2221
private HttpLoggingInterceptor loggingInterceptor;
23-
private boolean debugging;
22+
private LogLevel level;
2423

2524
public HttpRequester() {
2625
this.retryStrategy = new RetryStrategy();
2726

2827
OkHttpClient.Builder builder = new OkHttpClient.Builder();
2928
builder.addInterceptor(retryStrategy.getRetryInterceptor());
29+
30+
this.loggingInterceptor = new HttpLoggingInterceptor();
31+
loggingInterceptor.setLevel(LogLevel.NONE.value());
32+
builder.addInterceptor(this.loggingInterceptor);
33+
3034
builder.retryOnConnectionFailure(false);
3135

3236
httpClient = builder.build();
@@ -99,20 +103,11 @@ private <T> T deserialize(Response response, Type returnType) throws AlgoliaRunt
99103
}
100104

101105
@Override
102-
public void setDebugging(boolean debugging) {
103-
if (debugging != this.debugging) {
104-
if (debugging) {
105-
loggingInterceptor = new HttpLoggingInterceptor();
106-
loggingInterceptor.setLevel(Level.BODY);
107-
httpClient = httpClient.newBuilder().addInterceptor(loggingInterceptor).build();
108-
} else {
109-
final OkHttpClient.Builder builder = httpClient.newBuilder();
110-
builder.interceptors().remove(loggingInterceptor);
111-
httpClient = builder.build();
112-
loggingInterceptor = null;
113-
}
106+
public void setLogLevel(LogLevel level) {
107+
if (level != this.level) {
108+
this.loggingInterceptor.setLevel(level.value());
114109
}
115-
this.debugging = debugging;
110+
this.level = level;
116111
}
117112

118113
@Override
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.algolia.utils;
2+
3+
import okhttp3.logging.HttpLoggingInterceptor.Level;
4+
5+
public enum LogLevel {
6+
/**
7+
* No logs.
8+
*/
9+
NONE(Level.NONE),
10+
11+
/**
12+
* Logs request and response lines and their respective headers.
13+
*/
14+
HEADERS(Level.HEADERS),
15+
16+
/**
17+
* Logs request and response lines and their respective headers and bodies (if present).
18+
*/
19+
BODY(Level.BODY),
20+
21+
/**
22+
* Logs request and response lines.
23+
*/
24+
BASIC(Level.BASIC);
25+
26+
private Level value;
27+
28+
private LogLevel(Level value) {
29+
this.value = value;
30+
}
31+
32+
public Level value() {
33+
return this.value;
34+
}
35+
}

clients/algoliasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/Requester.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public interface Requester {
1616
/**
1717
* Enable/disable debugging for this API client.
1818
*
19-
* @param debugging To enable (true) or disable (false) debugging
19+
* @param level LogLevel the level of log to output
2020
*/
21-
public void setDebugging(boolean debugging);
21+
public void setLogLevel(LogLevel level);
2222

2323
/**
2424
* Get connection timeout (in milliseconds).

playground/java/src/main/java/com/algolia/playground/Search.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.algolia.api.SearchClient;
44
import com.algolia.exceptions.*;
55
import com.algolia.model.search.*;
6-
import com.algolia.utils.ClientOptions;
6+
import com.algolia.utils.*;
77
import io.github.cdimascio.dotenv.Dotenv;
88
import java.util.*;
99
import java.util.concurrent.CompletableFuture;
@@ -33,6 +33,8 @@ public static void main(String[] args) {
3333
.addAlgoliaAgentSegment("no version")
3434
);
3535

36+
client.setLogLevel(LogLevel.NONE);
37+
3638
String indexName = dotenv.get("SEARCH_INDEX");
3739
String query = dotenv.get("SEARCH_QUERY");
3840

@@ -49,6 +51,8 @@ public static void main(String[] args) {
4951

5052
client.waitForTask(indexName, response.getTaskID());
5153

54+
client.setLogLevel(LogLevel.BASIC);
55+
5256
SearchMethodParams searchMethodParams = new SearchMethodParams();
5357
List<SearchQuery> requests = new ArrayList<>();
5458
SearchForHits request = new SearchForHits();

website/docs/clients/guides/customized-client-usage.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@ import TabItem from '@theme/TabItem';
77

88
You might want to customize how the API client works, by providing additional information to your request, adding user-agents or use your own HTTP requester.
99

10+
## Setting a logger
11+
12+
You can set a custom logger on the client to enable more or less debug output depending on your need.
13+
14+
<TabsLanguage>
15+
<TabItem value="java">
16+
17+
```java
18+
import com.algolia.api.SearchClient;
19+
import com.algolia.utils.LogLevel;
20+
21+
SearchClient client = new SearchClient("<YOUR_INDEX_NAME>", "<YOUR_API_KEY>");
22+
23+
// No logs.
24+
client.setLogLevel(LogLevel.NONE);
25+
26+
// Logs request and response lines.
27+
client.setLogLevel(LogLevel.BASIC);
28+
29+
// Logs request and response lines and their respective headers.
30+
client.setLogLevel(LogLevel.HEADERS);
31+
32+
// Logs request and response lines and their respective headers and bodies (if present).
33+
client.setLogLevel(LogLevel.BODY);
34+
```
35+
36+
</TabItem>
37+
</TabsLanguage>
38+
1039
## Send additional information in your request with `requestOptions`
1140

1241
The `requestOptions` parameter allows you to merge additional information with the client transporter, such as `headers` or `query parameters`.

0 commit comments

Comments
 (0)