Skip to content

Commit

Permalink
Migrate to Spring JCL logging.
Browse files Browse the repository at this point in the history
Original Pull Request #2001
Closes #2000
  • Loading branch information
sothawo authored Nov 17, 2021
1 parent 45b4c99 commit 0c7c686
Show file tree
Hide file tree
Showing 24 changed files with 197 additions and 175 deletions.
22 changes: 0 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,6 @@
</exclusions>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j}</version>
<scope>test</scope>
</dependency>

<!-- Jackson JSON Mapper -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down Expand Up @@ -223,13 +208,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>${log4j}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.projectreactor.tools</groupId>
<artifactId>blockhound-junit-platform</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.Version;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
Expand Down Expand Up @@ -54,8 +56,6 @@
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.BulkFailureException;
import org.springframework.data.elasticsearch.backend.elasticsearch7.cluster.ElasticsearchClusterOperations;
import org.springframework.data.elasticsearch.backend.elasticsearch7.document.DocumentAdapters;
Expand Down Expand Up @@ -116,7 +116,7 @@
*/
public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.Version;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
Expand All @@ -47,8 +49,6 @@
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand Down Expand Up @@ -109,8 +109,7 @@
*/
public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOperations, ApplicationContextAware {

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

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

if (QUERY_LOGGER.isDebugEnabled()) {
QUERY_LOGGER.debug("Executing doCount: {}", request);
QUERY_LOGGER.debug(String.format("Executing doCount: %s", request));
}

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

if (QUERY_LOGGER.isDebugEnabled()) {
QUERY_LOGGER.debug("Executing doFind: {}", request);
QUERY_LOGGER.debug(String.format("Executing doFind: %s", request));
}

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

if (QUERY_LOGGER.isDebugEnabled()) {
QUERY_LOGGER.debug("Executing doFindForResponse: {}", request);
QUERY_LOGGER.debug(String.format("Executing doFindForResponse: %s", request));
}

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

if (QUERY_LOGGER.isDebugEnabled()) {
QUERY_LOGGER.debug("Executing doCount: {}", request);
QUERY_LOGGER.debug(String.format("Executing doCount: %s", request));
}

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

if (QUERY_LOGGER.isDebugEnabled()) {
QUERY_LOGGER.debug("Executing doScroll: {}", request);
QUERY_LOGGER.debug(String.format("Executing doScroll: %s", request));
}

return Flux.from(execute(client -> client.scroll(request))) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
Expand All @@ -36,8 +38,6 @@
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.elasticsearch.NoSuchIndexException;
Expand Down Expand Up @@ -69,7 +69,7 @@
*/
class ReactiveIndexTemplate implements ReactiveIndexOperations {

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

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

return Mono.just(Document.create());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
Expand All @@ -38,8 +40,6 @@
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.core.AbstractIndexTemplate;
import org.springframework.data.elasticsearch.core.IndexInformation;
import org.springframework.data.elasticsearch.core.IndexOperations;
Expand All @@ -66,7 +66,7 @@
*/
class RestIndexTemplate extends AbstractIndexTemplate implements IndexOperations {

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

private final ElasticsearchRestTemplate restTemplate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import java.net.URL;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.FactoryBeanNotInitializedException;
Expand All @@ -38,7 +38,7 @@
*/
public class RestClientFactoryBean implements FactoryBean<RestHighLevelClient>, InitializingBean, DisposableBean {

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

private @Nullable RestHighLevelClient client;
private String hosts = "http://localhost:9200";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@

import java.util.function.Supplier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

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

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

private ClientLogger() {}

Expand All @@ -52,24 +50,23 @@ public static boolean isEnabled() {
/**
* Log an outgoing HTTP request.
*
* @param logId the correlation Id, see {@link #newLogId()}.
* @param logId the correlation id, see {@link #newLogId()}.
* @param method HTTP method
* @param endpoint URI
* @param parameters optional parameters.
*/
public static void logRequest(String logId, String method, String endpoint, Object parameters) {

if (isEnabled()) {

WIRE_LOGGER.trace("[{}] Sending request {} {} with parameters: {}", logId, method.toUpperCase(), endpoint,
parameters);
WIRE_LOGGER.trace(String.format("[%s] Sending request %s %s with parameters: %s", logId, method.toUpperCase(),
endpoint, parameters));
}
}

/**
* Log an outgoing HTTP request with a request body.
*
* @param logId the correlation Id, see {@link #newLogId()}.
* @param logId the correlation id, see {@link #newLogId()}.
* @param method HTTP method
* @param endpoint URI
* @param parameters optional parameters.
Expand All @@ -79,43 +76,43 @@ public static void logRequest(String logId, String method, String endpoint, Obje
Supplier<Object> body) {

if (isEnabled()) {

WIRE_LOGGER.trace("[{}] Sending request {} {} with parameters: {}{}Request body: {}", logId, method.toUpperCase(),
endpoint, parameters, lineSeparator, body.get());
WIRE_LOGGER.trace(String.format("[%s] Sending request %s %s with parameters: %s%sRequest body: %s", logId,
method.toUpperCase(), endpoint, parameters, lineSeparator, body.get()));
}
}

/**
* Log a raw HTTP response without logging the body.
*
* @param logId the correlation Id, see {@link #newLogId()}.
* @param logId the correlation id, see {@link #newLogId()}.
* @param statusCode the HTTP status code.
*/
public static void logRawResponse(String logId, @Nullable HttpStatus statusCode) {

if (isEnabled()) {
WIRE_LOGGER.trace("[{}] Received raw response: {}", logId, statusCode);
WIRE_LOGGER.trace(String.format("[%s] Received raw response: %s", logId, statusCode));
}
}

/**
* Log a raw HTTP response along with the body.
*
* @param logId the correlation Id, see {@link #newLogId()}.
* @param logId the correlation id, see {@link #newLogId()}.
* @param statusCode the HTTP status code.
* @param body body content.
*/
public static void logResponse(String logId, HttpStatus statusCode, String body) {

if (isEnabled()) {
WIRE_LOGGER.trace("[{}] Received response: {}{}Response body: {}", logId, statusCode, lineSeparator, body);
WIRE_LOGGER.trace(
String.format("[%s] Received response: %s%sResponse body: %s", logId, statusCode, lineSeparator, body));
}
}

/**
* Creates a new, unique correlation Id to improve tracing across log events.
* Creates a new, unique correlation id to improve tracing across log events.
*
* @return a new, unique correlation Id.
* @return a new, unique correlation id.
*/
public static String newLogId() {

Expand Down
Loading

0 comments on commit 0c7c686

Please # to comment.