Skip to content

Commit 0c7c686

Browse files
authored
Migrate to Spring JCL logging.
Original Pull Request #2001 Closes #2000
1 parent 45b4c99 commit 0c7c686

24 files changed

+197
-175
lines changed

Diff for: pom.xml

-22
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,6 @@
147147
</exclusions>
148148
</dependency>
149149

150-
<!-- Logging -->
151-
<dependency>
152-
<groupId>org.slf4j</groupId>
153-
<artifactId>log4j-over-slf4j</artifactId>
154-
<version>${slf4j}</version>
155-
<scope>test</scope>
156-
</dependency>
157-
158-
<dependency>
159-
<groupId>org.apache.logging.log4j</groupId>
160-
<artifactId>log4j-core</artifactId>
161-
<version>${log4j}</version>
162-
<scope>test</scope>
163-
</dependency>
164-
165150
<!-- Jackson JSON Mapper -->
166151
<dependency>
167152
<groupId>com.fasterxml.jackson.core</groupId>
@@ -223,13 +208,6 @@
223208
</exclusions>
224209
</dependency>
225210

226-
<dependency>
227-
<groupId>org.apache.logging.log4j</groupId>
228-
<artifactId>log4j-to-slf4j</artifactId>
229-
<version>${log4j}</version>
230-
<scope>test</scope>
231-
</dependency>
232-
233211
<dependency>
234212
<groupId>io.projectreactor.tools</groupId>
235213
<artifactId>blockhound-junit-platform</artifactId>

Diff for: src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/ElasticsearchRestTemplate.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.stream.Collectors;
2525
import java.util.stream.Stream;
2626

27+
import org.apache.commons.logging.Log;
28+
import org.apache.commons.logging.LogFactory;
2729
import org.elasticsearch.Version;
2830
import org.elasticsearch.action.DocWriteResponse;
2931
import org.elasticsearch.action.bulk.BulkItemResponse;
@@ -54,8 +56,6 @@
5456
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
5557
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
5658
import org.elasticsearch.search.suggest.SuggestBuilder;
57-
import org.slf4j.Logger;
58-
import org.slf4j.LoggerFactory;
5959
import org.springframework.data.elasticsearch.BulkFailureException;
6060
import org.springframework.data.elasticsearch.backend.elasticsearch7.cluster.ElasticsearchClusterOperations;
6161
import org.springframework.data.elasticsearch.backend.elasticsearch7.document.DocumentAdapters;
@@ -116,7 +116,7 @@
116116
*/
117117
public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate {
118118

119-
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchRestTemplate.class);
119+
private static final Log LOGGER = LogFactory.getLog(ElasticsearchRestTemplate.class);
120120

121121
private final RestHighLevelClient client;
122122
private final ElasticsearchExceptionTranslator exceptionTranslator = new ElasticsearchExceptionTranslator();
@@ -419,7 +419,7 @@ public void searchScrollClear(List<String> scrollIds) {
419419
request.scrollIds(scrollIds);
420420
execute(client -> client.clearScroll(request, RequestOptions.DEFAULT));
421421
} catch (Exception e) {
422-
LOGGER.warn("Could not clear scroll: {}", e.getMessage());
422+
LOGGER.warn(String.format("Could not clear scroll: %s", e.getMessage()));
423423
}
424424
}
425425

Diff for: src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/ReactiveElasticsearchTemplate.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.function.Function;
2727
import java.util.stream.Collectors;
2828

29+
import org.apache.commons.logging.Log;
30+
import org.apache.commons.logging.LogFactory;
2931
import org.elasticsearch.Version;
3032
import org.elasticsearch.action.DocWriteResponse;
3133
import org.elasticsearch.action.bulk.BulkItemResponse;
@@ -47,8 +49,6 @@
4749
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
4850
import org.elasticsearch.search.suggest.SuggestBuilder;
4951
import org.reactivestreams.Publisher;
50-
import org.slf4j.Logger;
51-
import org.slf4j.LoggerFactory;
5252
import org.springframework.beans.BeansException;
5353
import org.springframework.context.ApplicationContext;
5454
import org.springframework.context.ApplicationContextAware;
@@ -109,8 +109,7 @@
109109
*/
110110
public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOperations, ApplicationContextAware {
111111

112-
private static final Logger QUERY_LOGGER = LoggerFactory
113-
.getLogger("org.springframework.data.elasticsearch.core.QUERY");
112+
private static final Log QUERY_LOGGER = LogFactory.getLog("org.springframework.data.elasticsearch.core.QUERY");
114113

115114
private final ReactiveElasticsearchClient client;
116115
private final ElasticsearchConverter converter;
@@ -811,7 +810,7 @@ public Flux<AggregationContainer<?>> aggregate(Query query, Class<?> entityType,
811810
protected Flux<AggregationContainer<?>> doAggregate(SearchRequest request) {
812811

813812
if (QUERY_LOGGER.isDebugEnabled()) {
814-
QUERY_LOGGER.debug("Executing doCount: {}", request);
813+
QUERY_LOGGER.debug(String.format("Executing doCount: %s", request));
815814
}
816815

817816
return Flux.from(execute(client -> client.aggregate(request))) //
@@ -885,7 +884,7 @@ private Mono<Long> doCount(Query query, Class<?> entityType, IndexCoordinates in
885884
protected Flux<SearchDocument> doFind(SearchRequest request) {
886885

887886
if (QUERY_LOGGER.isDebugEnabled()) {
888-
QUERY_LOGGER.debug("Executing doFind: {}", request);
887+
QUERY_LOGGER.debug(String.format("Executing doFind: %s", request));
889888
}
890889

891890
return Flux.from(execute(client -> client.search(request))).map(DocumentAdapters::from) //
@@ -903,7 +902,7 @@ protected Mono<SearchDocumentResponse> doFindForResponse(SearchRequest request,
903902
Function<SearchDocument, ? extends Object> suggestEntityCreator) {
904903

905904
if (QUERY_LOGGER.isDebugEnabled()) {
906-
QUERY_LOGGER.debug("Executing doFindForResponse: {}", request);
905+
QUERY_LOGGER.debug(String.format("Executing doFindForResponse: %s", request));
907906
}
908907

909908
return Mono.from(execute(client1 -> client1.searchForResponse(request))).map(searchResponse -> {
@@ -920,7 +919,7 @@ protected Mono<SearchDocumentResponse> doFindForResponse(SearchRequest request,
920919
protected Mono<Long> doCount(SearchRequest request) {
921920

922921
if (QUERY_LOGGER.isDebugEnabled()) {
923-
QUERY_LOGGER.debug("Executing doCount: {}", request);
922+
QUERY_LOGGER.debug(String.format("Executing doCount: %s", request));
924923
}
925924

926925
return Mono.from(execute(client -> client.count(request))) //
@@ -936,7 +935,7 @@ protected Mono<Long> doCount(SearchRequest request) {
936935
protected Flux<SearchDocument> doScroll(SearchRequest request) {
937936

938937
if (QUERY_LOGGER.isDebugEnabled()) {
939-
QUERY_LOGGER.debug("Executing doScroll: {}", request);
938+
QUERY_LOGGER.debug(String.format("Executing doScroll: %s", request));
940939
}
941940

942941
return Flux.from(execute(client -> client.scroll(request))) //

Diff for: src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/ReactiveIndexTemplate.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Map;
2525
import java.util.Set;
2626

27+
import org.apache.commons.logging.Log;
28+
import org.apache.commons.logging.LogFactory;
2729
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
2830
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
2931
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
@@ -36,8 +38,6 @@
3638
import org.elasticsearch.client.indices.GetMappingsRequest;
3739
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
3840
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
39-
import org.slf4j.Logger;
40-
import org.slf4j.LoggerFactory;
4141
import org.springframework.core.annotation.AnnotatedElementUtils;
4242
import org.springframework.dao.InvalidDataAccessApiUsageException;
4343
import org.springframework.data.elasticsearch.NoSuchIndexException;
@@ -69,7 +69,7 @@
6969
*/
7070
class ReactiveIndexTemplate implements ReactiveIndexOperations {
7171

72-
private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveIndexTemplate.class);
72+
private static final Log LOGGER = LogFactory.getLog(ReactiveIndexTemplate.class);
7373

7474
@Nullable private final Class<?> boundClass;
7575
private final IndexCoordinates boundIndex;
@@ -366,7 +366,7 @@ private Mono<Document> loadDocument(String path, String annotation) {
366366
}
367367
});
368368
} else {
369-
LOGGER.info("path in {} has to be defined. Using default instead.", annotation);
369+
LOGGER.info(String.format("path in %s has to be defined. Using default empty Document instead.", annotation));
370370
}
371371

372372
return Mono.just(Document.create());

Diff for: src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/RestIndexTemplate.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Map;
2121
import java.util.Set;
2222

23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
2325
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
2426
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
2527
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
@@ -38,8 +40,6 @@
3840
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
3941
import org.elasticsearch.client.indices.PutMappingRequest;
4042
import org.elasticsearch.cluster.metadata.MappingMetadata;
41-
import org.slf4j.Logger;
42-
import org.slf4j.LoggerFactory;
4343
import org.springframework.data.elasticsearch.core.AbstractIndexTemplate;
4444
import org.springframework.data.elasticsearch.core.IndexInformation;
4545
import org.springframework.data.elasticsearch.core.IndexOperations;
@@ -66,7 +66,7 @@
6666
*/
6767
class RestIndexTemplate extends AbstractIndexTemplate implements IndexOperations {
6868

69-
private static final Logger LOGGER = LoggerFactory.getLogger(RestIndexTemplate.class);
69+
private static final Log LOGGER = LogFactory.getLog(RestIndexTemplate.class);
7070

7171
private final ElasticsearchRestTemplate restTemplate;
7272

Diff for: src/main/java/org/springframework/data/elasticsearch/backend/elasticsearch7/client/RestClientFactoryBean.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
import java.net.URL;
1919
import java.util.ArrayList;
2020

21+
import org.apache.commons.logging.Log;
22+
import org.apache.commons.logging.LogFactory;
2123
import org.apache.http.HttpHost;
2224
import org.elasticsearch.client.RestClient;
2325
import org.elasticsearch.client.RestHighLevelClient;
24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
2626
import org.springframework.beans.factory.DisposableBean;
2727
import org.springframework.beans.factory.FactoryBean;
2828
import org.springframework.beans.factory.FactoryBeanNotInitializedException;
@@ -38,7 +38,7 @@
3838
*/
3939
public class RestClientFactoryBean implements FactoryBean<RestHighLevelClient>, InitializingBean, DisposableBean {
4040

41-
private static final Logger LOGGER = LoggerFactory.getLogger(RestClientFactoryBean.class);
41+
private static final Log LOGGER = LogFactory.getLog(RestClientFactoryBean.class);
4242

4343
private @Nullable RestHighLevelClient client;
4444
private String hosts = "http://localhost:9200";

Diff for: src/main/java/org/springframework/data/elasticsearch/client/ClientLogger.java

+17-20
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717

1818
import java.util.function.Supplier;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
2222
import org.springframework.http.HttpStatus;
2323
import org.springframework.lang.Nullable;
2424
import org.springframework.util.ObjectUtils;
2525

2626
/**
2727
* Logging Utility to log client requests and responses. Logs client requests and responses to Elasticsearch to a
28-
* dedicated logger: {@code org.springframework.data.elasticsearch.client.WIRE} on {@link org.slf4j.event.Level#TRACE}
29-
* level.
28+
* dedicated logger: {@code org.springframework.data.elasticsearch.client.WIRE} on trace level.
3029
*
3130
* @author Mark Paluch
3231
* @author Christoph Strobl
@@ -35,8 +34,7 @@
3534
public abstract class ClientLogger {
3635

3736
private static final String lineSeparator = System.getProperty("line.separator");
38-
private static final Logger WIRE_LOGGER = LoggerFactory
39-
.getLogger("org.springframework.data.elasticsearch.client.WIRE");
37+
private static final Log WIRE_LOGGER = LogFactory.getLog("org.springframework.data.elasticsearch.client.WIRE");
4038

4139
private ClientLogger() {}
4240

@@ -52,24 +50,23 @@ public static boolean isEnabled() {
5250
/**
5351
* Log an outgoing HTTP request.
5452
*
55-
* @param logId the correlation Id, see {@link #newLogId()}.
53+
* @param logId the correlation id, see {@link #newLogId()}.
5654
* @param method HTTP method
5755
* @param endpoint URI
5856
* @param parameters optional parameters.
5957
*/
6058
public static void logRequest(String logId, String method, String endpoint, Object parameters) {
6159

6260
if (isEnabled()) {
63-
64-
WIRE_LOGGER.trace("[{}] Sending request {} {} with parameters: {}", logId, method.toUpperCase(), endpoint,
65-
parameters);
61+
WIRE_LOGGER.trace(String.format("[%s] Sending request %s %s with parameters: %s", logId, method.toUpperCase(),
62+
endpoint, parameters));
6663
}
6764
}
6865

6966
/**
7067
* Log an outgoing HTTP request with a request body.
7168
*
72-
* @param logId the correlation Id, see {@link #newLogId()}.
69+
* @param logId the correlation id, see {@link #newLogId()}.
7370
* @param method HTTP method
7471
* @param endpoint URI
7572
* @param parameters optional parameters.
@@ -79,43 +76,43 @@ public static void logRequest(String logId, String method, String endpoint, Obje
7976
Supplier<Object> body) {
8077

8178
if (isEnabled()) {
82-
83-
WIRE_LOGGER.trace("[{}] Sending request {} {} with parameters: {}{}Request body: {}", logId, method.toUpperCase(),
84-
endpoint, parameters, lineSeparator, body.get());
79+
WIRE_LOGGER.trace(String.format("[%s] Sending request %s %s with parameters: %s%sRequest body: %s", logId,
80+
method.toUpperCase(), endpoint, parameters, lineSeparator, body.get()));
8581
}
8682
}
8783

8884
/**
8985
* Log a raw HTTP response without logging the body.
9086
*
91-
* @param logId the correlation Id, see {@link #newLogId()}.
87+
* @param logId the correlation id, see {@link #newLogId()}.
9288
* @param statusCode the HTTP status code.
9389
*/
9490
public static void logRawResponse(String logId, @Nullable HttpStatus statusCode) {
9591

9692
if (isEnabled()) {
97-
WIRE_LOGGER.trace("[{}] Received raw response: {}", logId, statusCode);
93+
WIRE_LOGGER.trace(String.format("[%s] Received raw response: %s", logId, statusCode));
9894
}
9995
}
10096

10197
/**
10298
* Log a raw HTTP response along with the body.
10399
*
104-
* @param logId the correlation Id, see {@link #newLogId()}.
100+
* @param logId the correlation id, see {@link #newLogId()}.
105101
* @param statusCode the HTTP status code.
106102
* @param body body content.
107103
*/
108104
public static void logResponse(String logId, HttpStatus statusCode, String body) {
109105

110106
if (isEnabled()) {
111-
WIRE_LOGGER.trace("[{}] Received response: {}{}Response body: {}", logId, statusCode, lineSeparator, body);
107+
WIRE_LOGGER.trace(
108+
String.format("[%s] Received response: %s%sResponse body: %s", logId, statusCode, lineSeparator, body));
112109
}
113110
}
114111

115112
/**
116-
* Creates a new, unique correlation Id to improve tracing across log events.
113+
* Creates a new, unique correlation id to improve tracing across log events.
117114
*
118-
* @return a new, unique correlation Id.
115+
* @return a new, unique correlation id.
119116
*/
120117
public static String newLogId() {
121118

0 commit comments

Comments
 (0)