diff --git a/src/main/java/com/studiomediatech/queryresponse/Query.java b/src/main/java/com/studiomediatech/queryresponse/Query.java index e2fbe27..26dbc9c 100644 --- a/src/main/java/com/studiomediatech/queryresponse/Query.java +++ b/src/main/java/com/studiomediatech/queryresponse/Query.java @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.type.TypeFactory; -import com.studiomediatech.queryresponse.util.Logging; +import com.studiomediatech.queryresponse.util.Loggable; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageBuilder; @@ -34,7 +34,7 @@ * @param * expected type of the coerced result elements. */ -class Query implements MessageListener, Logging { +class Query implements MessageListener, Loggable { private static final String HEADER_X_QR_PUBLISHED = RabbitFacade.HEADER_X_QR_PUBLISHED; @@ -84,7 +84,7 @@ protected Query(String queueName) { public void onMessage(Message message) { MessageProperties properties = message.getMessageProperties(); - log().info("|--> Received response message: {}", properties); + logger().info("|--> Received response message: {}", properties); measureLatency(properties.getHeader(HEADER_X_QR_PUBLISHED), System.currentTimeMillis()); handleResponseEnvelope(parseMessage(message)); } @@ -102,7 +102,7 @@ private ConsumedResponseEnvelope parseMessage(Message message) { JavaType type = TypeFactory.defaultInstance().constructParametricType(ConsumedResponseEnvelope.class, responseType); ConsumedResponseEnvelope response = reader.forType(type).readValue(message.getBody()); - log().debug("Received response: {}", response); + logger().debug("Received response: {}", response); return response; } catch (IOException ex) { @@ -111,7 +111,7 @@ private ConsumedResponseEnvelope parseMessage(Message message) { onError.accept(new IllegalArgumentException(errorMessage, ex)); } - log().error("Failed to parse received response.", ex); + logger().error("Failed to parse received response.", ex); } return ConsumedResponseEnvelope.empty(); @@ -120,7 +120,7 @@ private ConsumedResponseEnvelope parseMessage(Message message) { void handleResponseEnvelope(ConsumedResponseEnvelope envelope) { if (envelope.elements.isEmpty()) { - log().warn("Received empty response: {}", envelope); + logger().warn("Received empty response: {}", envelope); return; } @@ -211,7 +211,7 @@ public Collection accept(RabbitFacade facade, Statistics stats) throws Runtim } // Reset interrupted state, before moving on. - log().error("Sleep interrupted with still {} ms to go", wait, e); + logger().error("Sleep interrupted with still {} ms to go", wait, e); Thread.currentThread().interrupt(); /* @@ -263,7 +263,7 @@ private void publishQuery(RabbitFacade facade) { this.onError.accept(ex); } - log().error("Failed to publish query message", ex); + logger().error("Failed to publish query message", ex); } } diff --git a/src/main/java/com/studiomediatech/queryresponse/QueryRegistry.java b/src/main/java/com/studiomediatech/queryresponse/QueryRegistry.java index 621b5ed..45f0d9d 100644 --- a/src/main/java/com/studiomediatech/queryresponse/QueryRegistry.java +++ b/src/main/java/com/studiomediatech/queryresponse/QueryRegistry.java @@ -1,6 +1,6 @@ package com.studiomediatech.queryresponse; -import com.studiomediatech.queryresponse.util.Logging; +import com.studiomediatech.queryresponse.util.Loggable; import org.springframework.beans.BeansException; @@ -13,7 +13,7 @@ /** * Provides a way to register and execute queries. */ -class QueryRegistry implements ApplicationContextAware, Logging { +class QueryRegistry implements ApplicationContextAware, Loggable { // WARNING: Non-exemplary use of static supplier, for lazy access to bean instance. protected static Supplier instance = () -> null; diff --git a/src/main/java/com/studiomediatech/queryresponse/QueryResponseConfiguration.java b/src/main/java/com/studiomediatech/queryresponse/QueryResponseConfiguration.java index f516cf7..7966389 100644 --- a/src/main/java/com/studiomediatech/queryresponse/QueryResponseConfiguration.java +++ b/src/main/java/com/studiomediatech/queryresponse/QueryResponseConfiguration.java @@ -16,7 +16,7 @@ import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.env.Environment; -import com.studiomediatech.queryresponse.util.Logging; +import com.studiomediatech.queryresponse.util.Loggable; /** * Configures the required components for a Query/Response client, ensuring the availability of the necessary AMQP @@ -27,7 +27,7 @@ @AutoConfigureAfter(RabbitAutoConfiguration.class) @Import({ RabbitAutoConfiguration.class }) @EnableConfigurationProperties(QueryResponseConfigurationProperties.class) -class QueryResponseConfiguration implements Logging { +class QueryResponseConfiguration implements Loggable { private final QueryResponseConfigurationProperties props; @@ -69,7 +69,7 @@ RabbitFacade rabbitFacade(RabbitAdmin rabbitAdmin, ConnectionFactory connectionF @Bean QueryResponseTopicExchange queryResponseTopicExchange() { - return log(new QueryResponseTopicExchange(props.getExchange().getName())); + return info(new QueryResponseTopicExchange(props.getExchange().getName())); } @Bean diff --git a/src/main/java/com/studiomediatech/queryresponse/RabbitFacade.java b/src/main/java/com/studiomediatech/queryresponse/RabbitFacade.java index 481407d..89cd40c 100644 --- a/src/main/java/com/studiomediatech/queryresponse/RabbitFacade.java +++ b/src/main/java/com/studiomediatech/queryresponse/RabbitFacade.java @@ -20,13 +20,13 @@ import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer; import org.springframework.context.support.GenericApplicationContext; -import com.studiomediatech.queryresponse.util.Logging; +import com.studiomediatech.queryresponse.util.Loggable; /** * Provides an abstraction between the use of RabbitMQ and the capabilities in Spring Boot AMQP, and the structured * registry code in this library. */ -class RabbitFacade implements Logging { +class RabbitFacade implements Loggable { public static final String HEADER_X_QR_PUBLISHED = "x-qr-published"; @@ -61,7 +61,7 @@ public void declareQueue(Query query) { private void declareAndRegisterQueue(NamingStrategy name) { - AnonymousQueue queue = log(new AnonymousQueue(name)); + AnonymousQueue queue = info(new AnonymousQueue(name)); admin.declareQueue(queue); ctx.registerBean(queue.getActualName(), AnonymousQueue.class, () -> queue); } @@ -76,7 +76,7 @@ public void declareBinding(Response response) { private void declareAndRegisterBinding(String queueName, String routingKey) { - Binding binding = log( + Binding binding = info( new Binding(queueName, DestinationType.QUEUE, queriesExchange.getName(), routingKey, null)); admin.declareBinding(binding); ctx.registerBean(queueName + "-binding", Binding.class, () -> binding); @@ -193,7 +193,7 @@ public void publishResponse(String exchange, String routingKey, Message message) this.template.send(exchange, routingKey, m); logPublished("response", routingKey, m); } catch (RuntimeException e) { - log().error("Failed to publish response", e); + logger().error("Failed to publish response", e); } } diff --git a/src/main/java/com/studiomediatech/queryresponse/Response.java b/src/main/java/com/studiomediatech/queryresponse/Response.java index eb7bc45..22209c8 100644 --- a/src/main/java/com/studiomediatech/queryresponse/Response.java +++ b/src/main/java/com/studiomediatech/queryresponse/Response.java @@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; -import com.studiomediatech.queryresponse.util.Logging; +import com.studiomediatech.queryresponse.util.Loggable; import org.springframework.amqp.core.Address; import org.springframework.amqp.core.Message; @@ -28,7 +28,7 @@ * @param * type of the provided response elements. */ -class Response implements MessageListener, Logging { +class Response implements MessageListener, Loggable { private static final String HEADER_X_QR_PUBLISHED = RabbitFacade.HEADER_X_QR_PUBLISHED; @@ -69,7 +69,7 @@ public void onMessage(Message message) { try { MessageProperties properties = message.getMessageProperties(); - log().info("|--> Consumed query: " + properties.getReceivedRoutingKey()); + logger().info("|--> Consumed query: " + properties.getReceivedRoutingKey()); measureLatency(properties.getHeader(HEADER_X_QR_PUBLISHED), System.currentTimeMillis()); List.PublishedResponseEnvelope> responses = new ArrayList<>(); @@ -80,7 +80,7 @@ public void onMessage(Message message) { responses.addAll(buildResponses()); } - log().debug("Prepared response(s) {}", responses); + logger().debug("Prepared response(s) {}", responses); for (Response.PublishedResponseEnvelope response : responses) { byte[] body = writer.writeValueAsBytes(response); @@ -97,7 +97,7 @@ public void onMessage(Message message) { incrementPublishedResponseCounterStats(); } } catch (RuntimeException | JsonProcessingException e) { - log().error("Failed to publish response message", e); + logger().error("Failed to publish response message", e); } } diff --git a/src/main/java/com/studiomediatech/queryresponse/ResponseRegistry.java b/src/main/java/com/studiomediatech/queryresponse/ResponseRegistry.java index 93817b6..d513680 100644 --- a/src/main/java/com/studiomediatech/queryresponse/ResponseRegistry.java +++ b/src/main/java/com/studiomediatech/queryresponse/ResponseRegistry.java @@ -1,6 +1,6 @@ package com.studiomediatech.queryresponse; -import com.studiomediatech.queryresponse.util.Logging; +import com.studiomediatech.queryresponse.util.Loggable; import org.springframework.beans.BeansException; @@ -12,7 +12,7 @@ /** * Provides a way to create and register responses. */ -class ResponseRegistry implements ApplicationContextAware, Logging { +class ResponseRegistry implements ApplicationContextAware, Loggable { // WARNING: Non-exemplary use of static supplier, for lazy access to bean instance. protected static Supplier instance = () -> null; @@ -58,9 +58,9 @@ protected void doAccept(Response response) { facade.addListener(response); response.accept(facade, stats); - log().info("Registered {}", response); + logger().info("Registered {}", response); } catch (Throwable th) { - log().error("Failed to register response", th); + logger().error("Failed to register response", th); facade.removeListener(response); facade.removeQueue(response); } diff --git a/src/main/java/com/studiomediatech/queryresponse/Statistics.java b/src/main/java/com/studiomediatech/queryresponse/Statistics.java index fabef36..d31880c 100644 --- a/src/main/java/com/studiomediatech/queryresponse/Statistics.java +++ b/src/main/java/com/studiomediatech/queryresponse/Statistics.java @@ -37,9 +37,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.studiomediatech.queryresponse.util.DurationFormatter; -import com.studiomediatech.queryresponse.util.Logging; +import com.studiomediatech.queryresponse.util.Loggable; -class Statistics implements Logging { +class Statistics implements Loggable { private static final ObjectWriter writer = new ObjectMapper().writer(); @@ -107,7 +107,7 @@ protected void publishStats() { facade.publishStats(message, routingKey); } catch (RuntimeException | JsonProcessingException ex) { - log().error("Failed to publish stats", ex); + logger().error("Failed to publish stats", ex); } this.scheduler.schedule(this::publishStats, props.getStats().getDelay(), TimeUnit.MILLISECONDS); diff --git a/src/main/java/com/studiomediatech/queryresponse/util/Logging.java b/src/main/java/com/studiomediatech/queryresponse/util/Loggable.java similarity index 84% rename from src/main/java/com/studiomediatech/queryresponse/util/Logging.java rename to src/main/java/com/studiomediatech/queryresponse/util/Loggable.java index 926551a..41b251e 100644 --- a/src/main/java/com/studiomediatech/queryresponse/util/Logging.java +++ b/src/main/java/com/studiomediatech/queryresponse/util/Loggable.java @@ -8,14 +8,14 @@ /** * Trait for logging, with convenient access to a default instance based logger. */ -public interface Logging { +public interface Loggable { /** * Provides a default logger * * @return a logger instance, never {@code null} */ - default Logger log() { + default Logger logger() { return LoggerFactory.getLogger(getClass()); } @@ -30,9 +30,9 @@ default Logger log() { * * @return the current logger, for chaining */ - default T log(T obj) { + default T info(T obj) { - log().info("//> Declared {}", obj); + logger().info("//> Declared {}", obj); return obj; } @@ -50,7 +50,7 @@ default T log(T obj) { */ default void debugLogPublished(String type, String routingKey, Message message) { - log().debug("|<-- Published {}: {} - {}", type, routingKey, toStringRedacted(message)); + logger().debug("|<-- Published {}: {} - {}", type, routingKey, toStringRedacted(message)); } /** @@ -66,10 +66,10 @@ default void debugLogPublished(String type, String routingKey, Message message) */ default void logPublished(String type, String routingKey, Message message) { - if (log().isTraceEnabled()) { - log().debug("|<-- Published {}: {} - {}", type, routingKey, message); + if (logger().isTraceEnabled()) { + logger().debug("|<-- Published {}: {} - {}", type, routingKey, message); } else { - log().info("|<-- Published {}: {} - {}", type, routingKey, toStringRedacted(message)); + logger().info("|<-- Published {}: {} - {}", type, routingKey, toStringRedacted(message)); } } diff --git a/src/test/java/com/studiomediatech/queryresponse/RabbitFacadeLoggerTest.java b/src/test/java/com/studiomediatech/queryresponse/RabbitFacadeLoggerTest.java index 9e9fd42..df93cdf 100644 --- a/src/test/java/com/studiomediatech/queryresponse/RabbitFacadeLoggerTest.java +++ b/src/test/java/com/studiomediatech/queryresponse/RabbitFacadeLoggerTest.java @@ -76,7 +76,7 @@ public LogMockingRabbitFacade(Logger log) { } @Override - public Logger log() { + public Logger logger() { return this.log; } diff --git a/src/test/java/com/studiomediatech/queryresponse/util/LoggableTest.java b/src/test/java/com/studiomediatech/queryresponse/util/LoggableTest.java new file mode 100644 index 0000000..6e88755 --- /dev/null +++ b/src/test/java/com/studiomediatech/queryresponse/util/LoggableTest.java @@ -0,0 +1,19 @@ +package com.studiomediatech.queryresponse.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; + +class LoggableTest { + + @Test + void ensureExposesLogger() { + (new Loggable() { + void test() { + assertThat(logger()).isNotNull().isInstanceOfAny(Logger.class); + } + }).test(); + } + +}