Skip to content

Commit 8ee457f

Browse files
committed
Added Logger in ClientOptions
1 parent b9a36bb commit 8ee457f

3 files changed

Lines changed: 105 additions & 4 deletions

File tree

EXAMPLES.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [Management API usage](#management-api-usage)
66
- [Verifying an ID token](#verifying-an-id-token)
77
- [Organizations](#organizations)
8+
- [Logging](#logging)
89
- [Asynchronous operations](#asynchronous-operations)
910

1011
## Error handling
@@ -303,6 +304,71 @@ String url = auth.authorizeUrl("https://me.auth0.com/callback")
303304
.build();
304305
```
305306

307+
## Logging
308+
309+
The SDK is silent by default. You can enable logging to see HTTP requests and responses, which is useful for debugging.
310+
311+
### Enable logging with default settings
312+
313+
```java
314+
import com.auth0.client.mgmt.ManagementApi;
315+
import com.auth0.client.mgmt.core.LogConfig;
316+
317+
ManagementApi client = ManagementApi.builder()
318+
.domain("{YOUR_DOMAIN}")
319+
.token("{YOUR_API_TOKEN}")
320+
.logging(LogConfig.builder()
321+
.silent(false)
322+
.build())
323+
.build();
324+
```
325+
326+
### Enable debug-level logging
327+
328+
```java
329+
import com.auth0.client.mgmt.core.LogConfig;
330+
import com.auth0.client.mgmt.core.LogLevel;
331+
332+
ManagementApi client = ManagementApi.builder()
333+
.domain("{YOUR_DOMAIN}")
334+
.token("{YOUR_API_TOKEN}")
335+
.logging(LogConfig.builder()
336+
.level(LogLevel.DEBUG)
337+
.silent(false)
338+
.build())
339+
.build();
340+
```
341+
342+
### Using a custom logger
343+
344+
```java
345+
import com.auth0.client.mgmt.core.ILogger;
346+
import com.auth0.client.mgmt.core.LogConfig;
347+
import com.auth0.client.mgmt.core.LogLevel;
348+
349+
ManagementApi client = ManagementApi.builder()
350+
.domain("{YOUR_DOMAIN}")
351+
.token("{YOUR_API_TOKEN}")
352+
.logging(LogConfig.builder()
353+
.level(LogLevel.DEBUG)
354+
.logger(new MyCustomLogger()) // implements ILogger
355+
.silent(false)
356+
.build())
357+
.build();
358+
```
359+
360+
Logging is also available on the async client:
361+
362+
```java
363+
AsyncManagementApi asyncClient = AsyncManagementApi.builder()
364+
.token("{YOUR_API_TOKEN}")
365+
.tenantDomain("{YOUR_DOMAIN}")
366+
.logging(LogConfig.builder()
367+
.silent(false)
368+
.build())
369+
.build();
370+
```
371+
306372
## Asynchronous operations
307373

308374
### Management API

src/main/java/com/auth0/client/mgmt/ManagementApiBuilder.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
*/
44
package com.auth0.client.mgmt;
55

6-
import com.auth0.client.mgmt.core.ClientOptions;
7-
import com.auth0.client.mgmt.core.CustomDomainInterceptor;
8-
import com.auth0.client.mgmt.core.Environment;
9-
import com.auth0.client.mgmt.core.OAuthTokenSupplier;
6+
import com.auth0.client.mgmt.core.*;
107
import java.util.HashMap;
118
import java.util.Map;
129
import java.util.Optional;
@@ -26,6 +23,8 @@ public class ManagementApiBuilder {
2623

2724
private OkHttpClient httpClient;
2825

26+
private Optional<LogConfig> logging = Optional.empty();
27+
2928
private String customDomain = null;
3029

3130
// Domain-based initialization fields
@@ -168,6 +167,14 @@ public ManagementApiBuilder httpClient(OkHttpClient httpClient) {
168167
return this;
169168
}
170169

170+
/**
171+
* Configure logging for the SDK. Silent by default — no log output unless explicitly configured.
172+
*/
173+
public ManagementApiBuilder logging(LogConfig logging) {
174+
this.logging = Optional.of(logging);
175+
return this;
176+
}
177+
171178
/**
172179
* Add a custom header to be sent with all requests.
173180
* For headers that need to be computed dynamically or conditionally, use the setAdditional() method override instead.
@@ -188,6 +195,7 @@ protected ClientOptions buildClientOptions() {
188195
setHttpClient(builder);
189196
setTimeouts(builder);
190197
setRetries(builder);
198+
setLogging(builder);
191199
for (Map.Entry<String, String> header : this.customHeaders.entrySet()) {
192200
builder.addHeader(header.getKey(), header.getValue());
193201
}
@@ -293,6 +301,18 @@ protected void setHttpClient(ClientOptions.Builder builder) {
293301
}
294302
}
295303

304+
/**
305+
* Sets the logging configuration for the SDK.
306+
* Override this method to customize logging behavior.
307+
*
308+
* @param builder The ClientOptions.Builder to configure
309+
*/
310+
protected void setLogging(ClientOptions.Builder builder) {
311+
if (this.logging.isPresent()) {
312+
builder.logging(this.logging.get());
313+
}
314+
}
315+
296316
/**
297317
* Override this method to add any additional configuration to the client.
298318
* This method is called at the end of the configuration chain, allowing you to add

src/main/java/com/auth0/client/mgmt/core/ClientOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public static class Builder {
111111

112112
private OkHttpClient httpClient = null;
113113

114+
private LogConfig logging = null;
115+
114116
public Builder environment(Environment environment) {
115117
this.environment = environment;
116118
return this;
@@ -155,6 +157,14 @@ public Builder httpClient(OkHttpClient httpClient) {
155157
return this;
156158
}
157159

160+
/**
161+
* Configure logging for the SDK. Silent by default.
162+
*/
163+
public Builder logging(LogConfig logging) {
164+
this.logging = logging;
165+
return this;
166+
}
167+
158168
/**
159169
* Add an OkHttp interceptor to the client.
160170
*/
@@ -186,6 +196,11 @@ public ClientOptions build() {
186196
httpClientBuilder.addInterceptor(interceptor);
187197
}
188198

199+
if (this.logging != null && !this.logging.silent()) {
200+
httpClientBuilder.addInterceptor(new LoggingInterceptor(
201+
new Logger(this.logging.level(), this.logging.logger(), this.logging.silent())));
202+
}
203+
189204
this.httpClient = httpClientBuilder.build();
190205
this.timeout = Optional.of(httpClient.callTimeoutMillis() / 1000);
191206

0 commit comments

Comments
 (0)