Skip to content

Commit 159065a

Browse files
authored
feat: Migrate to SLF4J for logging and update dependencies (#150)
* feat: Migrate to SLF4J for logging and update dependencies Replaced `java.util.logging` with SLF4J. Updated Gradle configurations to include `slf4j-api` and `slf4j-simple`. Adjusted tests and container logging to reflect new logging framework. Fixes #128 Signed-off-by: Eric Deandrea <[email protected]> * feat: Migrate to SLF4J for logging and update dependencies Replaced `java.util.logging` with SLF4J. Updated Gradle configurations to include `slf4j-api` and `slf4j-simple`. Adjusted tests and container logging to reflect new logging framework. Fixes #128 Signed-off-by: Eric Deandrea <[email protected]> --------- Signed-off-by: Eric Deandrea <[email protected]>
1 parent fd9738d commit 159065a

File tree

7 files changed

+61
-25
lines changed

7 files changed

+61
-25
lines changed

docling-serve/docling-serve-client/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ description = "Docling Serve Client"
77

88
dependencies {
99
api(project(":docling-serve-api"))
10+
api(libs.slf4j.api)
1011
implementation(platform(libs.jackson.bom))
1112
implementation(libs.jackson.databind)
1213
implementation(libs.jackson2.databind)
1314

1415
testImplementation(platform(libs.testcontainers.bom))
1516
testImplementation(libs.testcontainers.junit.jupiter)
1617
testImplementation(project(":docling-testcontainers"))
18+
testImplementation(libs.slf4j.simple)
1719
}

docling-serve/docling-serve-client/src/main/java/ai/docling/serve/client/DoclingServeClient.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
import java.util.Objects;
1515
import java.util.Optional;
1616
import java.util.concurrent.Flow.Subscriber;
17-
import java.util.logging.Level;
18-
import java.util.logging.Logger;
17+
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
1920

2021
import ai.docling.serve.api.DoclingServeApi;
2122
import ai.docling.serve.api.chunk.request.HierarchicalChunkDocumentRequest;
@@ -37,7 +38,7 @@
3738
* {@link #writeValueAsString(Object)} for serialization and deserialization behavior.
3839
*/
3940
public abstract class DoclingServeClient implements DoclingServeApi {
40-
private static final Logger LOG = Logger.getLogger(DoclingServeClient.class.getName());
41+
private static final Logger LOG = LoggerFactory.getLogger(DoclingServeClient.class);
4142
protected static final URI DEFAULT_BASE_URL = URI.create("http://localhost:5001");
4243

4344
private final URI baseUrl;
@@ -82,7 +83,7 @@ protected DoclingServeClient(DoclingServeClientBuilder builder) {
8283
protected abstract <T> String writeValueAsString(T value);
8384

8485
protected void logRequest(HttpRequest request) {
85-
if (LOG.isLoggable(Level.INFO)) {
86+
if (LOG.isInfoEnabled()) {
8687
var stringBuilder = new StringBuilder();
8788
stringBuilder.append("\n→ REQUEST: %s %s\n".formatted(request.method(), request.uri()));
8889
stringBuilder.append(" HEADERS:\n");
@@ -96,7 +97,7 @@ protected void logRequest(HttpRequest request) {
9697
}
9798

9899
protected void logResponse(HttpResponse<String> response, Optional<String> responseBody) {
99-
if (LOG.isLoggable(Level.INFO)) {
100+
if (LOG.isInfoEnabled()) {
100101
var stringBuilder = new StringBuilder();
101102
stringBuilder.append("\n← RESPONSE: %s\n".formatted(response.statusCode()));
102103
stringBuilder.append(" HEADERS:\n");
@@ -124,7 +125,7 @@ protected <T> T execute(HttpRequest request, Class<T> expectedValueType) {
124125
}
125126
finally {
126127
long duration = System.currentTimeMillis() - startTime;
127-
LOG.info(() -> "Request [%s %s] took %d ms".formatted(request.method(), request.uri(), duration));
128+
LOG.info("Request [{} {}] took {}ms", request.method(), request.uri(), duration);
128129
}
129130
}
130131

@@ -198,7 +199,7 @@ public long contentLength() {
198199
@Override
199200
public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
200201
if (logRequests) {
201-
LOG.info(() -> "→ REQUEST BODY: %s".formatted(this.stringContent));
202+
LOG.info("→ REQUEST BODY: {}", this.stringContent);
202203
}
203204

204205
this.delegate.subscribe(subscriber);

docling-testcontainers/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ description = "Testcontainers for Docling services"
88
dependencies {
99
api(platform(libs.testcontainers.bom))
1010
api("org.testcontainers:testcontainers")
11+
api(libs.slf4j.api)
1112
testImplementation(platform(libs.jackson.bom))
1213
testImplementation(libs.jackson.databind)
1314
testImplementation(libs.testcontainers.junit.jupiter)
15+
testImplementation(libs.slf4j.simple)
1416
}

docling-testcontainers/src/main/java/ai/docling/testcontainers/package-info.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

docling-testcontainers/src/main/java/ai/docling/testcontainers/serve/DoclingServeContainer.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package ai.docling.testcontainers.serve;
22

3-
import java.util.logging.Logger;
3+
import java.util.Optional;
44

5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
57
import org.testcontainers.containers.GenericContainer;
68
import org.testcontainers.containers.wait.strategy.Wait;
79
import org.testcontainers.utility.DockerImageName;
@@ -18,7 +20,7 @@
1820
* health checks for the container.
1921
*/
2022
public class DoclingServeContainer extends GenericContainer<DoclingServeContainer> {
21-
private static final Logger LOG = Logger.getLogger(DoclingServeContainer.class.getName());
23+
private static final Logger LOG = LoggerFactory.getLogger(DoclingServeContainer.class);
2224

2325
/**
2426
* The default container port that docling runs on
@@ -66,21 +68,20 @@ public int getPort() {
6668
* The URL where to access the Docling Serve API.
6769
*/
6870
public String getApiUrl() {
69-
return "http://" + getHost() + ":" + getPort();
71+
return "http://%s:%d".formatted(getHost(), getPort());
7072
}
7173

7274
/**
7375
* The URL where to access the Docling Serve UI, if enabled.
7476
*/
75-
public String getUiUrl() {
76-
return getApiUrl() + "/ui";
77+
public Optional<String> getUiUrl() {
78+
return this.config.enableUi() ?
79+
Optional.of("%s/ui".formatted(getApiUrl())) :
80+
Optional.empty();
7781
}
7882

7983
@Override
8084
protected void containerIsStarted(InspectContainerResponse containerInfo) {
81-
if (config.enableUi()) {
82-
LOG.info(() -> "Docling Serve UI: %s".formatted(getUiUrl()));
83-
}
85+
getUiUrl().ifPresent(url -> LOG.info("Docling Serve UI: {}", url));
8486
}
85-
8687
}

docling-testcontainers/src/test/java/ai/docling/testcontainers/serve/DoclingServeContainerAvailableTests.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ private record HealthResponse(String status) {}
3030
.build()
3131
);
3232

33+
@Container
34+
private final DoclingServeContainer noUiDoclingContainer = new DoclingServeContainer(
35+
DoclingServeContainerConfig.builder()
36+
.image(DoclingServeContainerConfig.DOCLING_IMAGE)
37+
.enableUi(false)
38+
.build()
39+
);
40+
3341
@Test
34-
void containerAvailable() throws IOException, InterruptedException {
35-
var healthRequest = HttpRequest.newBuilder(
36-
URI.create("%s/health".formatted(this.doclingContainer.getApiUrl()))
37-
)
42+
void containerNoUI() throws IOException, InterruptedException {
43+
var healthRequest = HttpRequest.newBuilder(URI.create("%s/health".formatted(this.noUiDoclingContainer.getApiUrl())))
3844
.header("Accept", "application/json")
3945
.timeout(Duration.ofSeconds(10))
4046
.GET()
@@ -49,8 +55,31 @@ void containerAvailable() throws IOException, InterruptedException {
4955
.usingRecursiveComparison()
5056
.isEqualTo(new HealthResponse("ok"));
5157

58+
assertThat(this.noUiDoclingContainer.getUiUrl())
59+
.isNotNull()
60+
.isEmpty();
61+
}
62+
63+
@Test
64+
void containerAvailable() throws IOException, InterruptedException {
65+
var healthRequest = HttpRequest.newBuilder(URI.create("%s/health".formatted(this.doclingContainer.getApiUrl())))
66+
.header("Accept", "application/json")
67+
.timeout(Duration.ofSeconds(10))
68+
.GET()
69+
.build();
70+
71+
var response = HttpClient.newHttpClient()
72+
.send(healthRequest, jsonBodyHandler(DoclingServeContainerAvailableTests.HealthResponse.class))
73+
.body();
74+
75+
assertThat(response)
76+
.isNotNull()
77+
.usingRecursiveComparison()
78+
.isEqualTo(new HealthResponse("ok"));
79+
5280
assertThat(this.doclingContainer.getUiUrl())
53-
.isEqualTo(this.doclingContainer.getApiUrl() + "/ui");
81+
.get()
82+
.isEqualTo("%s/ui".formatted(this.doclingContainer.getApiUrl()));
5483
}
5584

5685
private static <T> HttpResponse.BodyHandler<T> jsonBodyHandler(Class<T> type) {

gradle/libs.versions.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ junit = "6.0.1"
88
lombok = "1.18.42"
99
lombok-gradle = "9.1.0"
1010
semver4j = "6.0.0"
11+
slf4j = "2.0.17"
1112
testcontainers = "2.0.2"
1213
quarkus = "3.30.2"
1314
quarkus-github-api = "1.330.0"
@@ -35,6 +36,10 @@ junit-bom = { group = "org.junit", name = "junit-bom", version.ref = "junit" }
3536
junit-jupiter = { group = "org.junit.jupiter", name = "junit-jupiter" }
3637
junit-platform = { group = "org.junit.platform", name = "junit-platform-launcher" }
3738

39+
# SLF4j
40+
slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j" }
41+
slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j" }
42+
3843
# Testcontainers
3944
testcontainers-bom = { group = "org.testcontainers", name = "testcontainers-bom", version.ref = "testcontainers"}
4045
testcontainers-junit-jupiter = { group = "org.testcontainers", name = "testcontainers-junit-jupiter" }

0 commit comments

Comments
 (0)