Skip to content

Commit

Permalink
feat(java): enable custom level debug APIC-535 (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jun 16, 2022
1 parent 750523b commit 14190d7
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,11 @@ public boolean isDebugging() {
}

/**
* Enable/disable debugging for this API client.
*
* @param debugging To enable (true) or disable (false) debugging
* Set the log level of the requester
* @return ApiClient
*/
public ApiClient setDebugging(boolean debugging) {
requester.setDebugging(debugging);
public ApiClient setLogLevel(LogLevel level) {
requester.setLogLevel(level);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level;

public class HttpRequester implements Requester {

private RetryStrategy retryStrategy;
private OkHttpClient httpClient;
private HttpLoggingInterceptor loggingInterceptor;
private boolean debugging;
private LogLevel level;

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

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(retryStrategy.getRetryInterceptor());

this.loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(LogLevel.NONE.value());
builder.addInterceptor(this.loggingInterceptor);

builder.retryOnConnectionFailure(false);

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

@Override
public void setDebugging(boolean debugging) {
if (debugging != this.debugging) {
if (debugging) {
loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(Level.BODY);
httpClient = httpClient.newBuilder().addInterceptor(loggingInterceptor).build();
} else {
final OkHttpClient.Builder builder = httpClient.newBuilder();
builder.interceptors().remove(loggingInterceptor);
httpClient = builder.build();
loggingInterceptor = null;
}
public void setLogLevel(LogLevel level) {
if (level != this.level) {
this.loggingInterceptor.setLevel(level.value());
}
this.debugging = debugging;
this.level = level;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.algolia.utils;

import okhttp3.logging.HttpLoggingInterceptor.Level;

public enum LogLevel {
/**
* No logs.
*/
NONE(Level.NONE),

/**
* Logs request and response lines and their respective headers.
*/
HEADERS(Level.HEADERS),

/**
* Logs request and response lines and their respective headers and bodies (if present).
*/
BODY(Level.BODY),

/**
* Logs request and response lines.
*/
BASIC(Level.BASIC);

private Level value;

private LogLevel(Level value) {
this.value = value;
}

public Level value() {
return this.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public interface Requester {
/**
* Enable/disable debugging for this API client.
*
* @param debugging To enable (true) or disable (false) debugging
* @param level LogLevel the level of log to output
*/
public void setDebugging(boolean debugging);
public void setLogLevel(LogLevel level);

/**
* Get connection timeout (in milliseconds).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.algolia.api.SearchClient;
import com.algolia.exceptions.*;
import com.algolia.model.search.*;
import com.algolia.utils.ClientOptions;
import com.algolia.utils.*;
import io.github.cdimascio.dotenv.Dotenv;
import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -33,6 +33,8 @@ public static void main(String[] args) {
.addAlgoliaAgentSegment("no version")
);

client.setLogLevel(LogLevel.NONE);

String indexName = dotenv.get("SEARCH_INDEX");
String query = dotenv.get("SEARCH_QUERY");

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

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

client.setLogLevel(LogLevel.BASIC);

SearchMethodParams searchMethodParams = new SearchMethodParams();
List<SearchQuery> requests = new ArrayList<>();
SearchForHits request = new SearchForHits();
Expand Down
29 changes: 29 additions & 0 deletions website/docs/clients/guides/customized-client-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ import TabItem from '@theme/TabItem';

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.

## Setting a logger

You can set a custom logger on the client to enable more or less debug output depending on your need.

<TabsLanguage>
<TabItem value="java">

```java
import com.algolia.api.SearchClient;
import com.algolia.utils.LogLevel;

SearchClient client = new SearchClient("<YOUR_INDEX_NAME>", "<YOUR_API_KEY>");

// No logs.
client.setLogLevel(LogLevel.NONE);

// Logs request and response lines.
client.setLogLevel(LogLevel.BASIC);

// Logs request and response lines and their respective headers.
client.setLogLevel(LogLevel.HEADERS);

// Logs request and response lines and their respective headers and bodies (if present).
client.setLogLevel(LogLevel.BODY);
```

</TabItem>
</TabsLanguage>

## Send additional information in your request with `requestOptions`

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

0 comments on commit 14190d7

Please sign in to comment.