From c0b19e61b552c93a78a854c3be566548b0c5c3ee Mon Sep 17 00:00:00 2001
From: Artem Bilan
false by default.
- *
* @param shouldLogFullMessage true if the complete message should be logged.
*/
public void setShouldLogFullMessage(boolean shouldLogFullMessage) {
Assert.isTrue(!(this.expressionSet), "Cannot set both 'expression' AND 'shouldLogFullMessage' properties");
this.shouldLogFullMessageSet = true;
- this.expression = (shouldLogFullMessage) ? EXPRESSION_PARSER.parseExpression("#root") : EXPRESSION_PARSER
- .parseExpression("payload");
+ this.expression = (shouldLogFullMessage)
+ ? EXPRESSION_PARSER.parseExpression("#root")
+ : EXPRESSION_PARSER.parseExpression("payload");
}
@Override
@@ -132,7 +169,10 @@ public class LoggingHandler extends AbstractMessageHandler {
@Override
protected void onInit() throws Exception {
super.onInit();
- this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory());
+ this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
+ if (this.expression == null) {
+ this.expression = EXPRESSION_PARSER.parseExpression("payload");
+ }
}
@Override
diff --git a/spring-integration-core/src/test/java/org/springframework/integration/handler/LoggingHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/handler/LoggingHandlerTests.java
index 2aabc4edf3..57cd9f9a7c 100644
--- a/spring-integration-core/src/test/java/org/springframework/integration/handler/LoggingHandlerTests.java
+++ b/spring-integration-core/src/test/java/org/springframework/integration/handler/LoggingHandlerTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.integration.handler;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
@@ -29,6 +30,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.DirectFieldAccessor;
+import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
@@ -61,7 +63,7 @@ public class LoggingHandlerTests {
@Test
public void assertMutuallyExclusive() {
LoggingHandler loggingHandler = new LoggingHandler("INFO");
- loggingHandler.setExpression("'foo'");
+ loggingHandler.setLogExpressionString("'foo'");
try {
loggingHandler.setShouldLogFullMessage(true);
fail("Expected IllegalArgumentException");
@@ -73,7 +75,7 @@ public class LoggingHandlerTests {
loggingHandler = new LoggingHandler("INFO");
loggingHandler.setShouldLogFullMessage(true);
try {
- loggingHandler.setExpression("'foo'");
+ loggingHandler.setLogExpressionString("'foo'");
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
@@ -84,6 +86,9 @@ public class LoggingHandlerTests {
@Test
public void testDontEvaluateIfNotEnabled() {
LoggingHandler loggingHandler = new LoggingHandler("INFO");
+ loggingHandler.setBeanFactory(mock(BeanFactory.class));
+ loggingHandler.afterPropertiesSet();
+
DirectFieldAccessor accessor = new DirectFieldAccessor(loggingHandler);
Log log = (Log) accessor.getPropertyValue("messageLogger");
log = spy(log);
@@ -102,7 +107,10 @@ public class LoggingHandlerTests {
@Test
public void testChangeLevel() {
- LoggingHandler loggingHandler = new LoggingHandler("INFO");
+ LoggingHandler loggingHandler = new LoggingHandler(Level.INFO);
+ loggingHandler.setBeanFactory(mock(BeanFactory.class));
+ loggingHandler.afterPropertiesSet();
+
DirectFieldAccessor accessor = new DirectFieldAccessor(loggingHandler);
Log log = (Log) accessor.getPropertyValue("messageLogger");
log = spy(log);
diff --git a/src/reference/asciidoc/logging-adapter.adoc b/src/reference/asciidoc/logging-adapter.adoc
index 39c9d46ed4..e822932fc1 100644
--- a/src/reference/asciidoc/logging-adapter.adoc
+++ b/src/reference/asciidoc/logging-adapter.adoc
@@ -40,3 +40,44 @@ This attribute cannot be specified if `expression` is specified.
<5> Specifies the _name_ of the logger (known as `category` in `log4j`) used for log messages created by this adapter.
This enables setting the log name (in the logging subsystem) for individual adapters.
By default, all adapters will log under the name `org.springframework.integration.handler.LoggingHandler`.
+
+==== Configuring with Java Configuration
+
+The following Spring Boot application provides an example of configuring the `LoggingHandler` using Java configuration:
+[source, java]
+----
+@SpringBootApplication
+public class LoggingJavaApplication {
+
+ public static void main(String[] args) {
+ ConfigurableApplicationContext context =
+ new SpringApplicationBuilder(LoggingJavaApplication.class)
+ .web(false)
+ .run(args);
+ MyGateway gateway = context.getBean(MyGateway.class);
+ gateway.sendToLogger("foo");
+ }
+
+ @Bean
+ public MessageChannel logInputChannel() {
+ return new DirectChannel();
+ }
+
+ @Bean
+ @ServiceActivator(inputChannel = "logChannel")
+ public LoggingHandler logging() {
+ LoggingHandler adapter = new LoggingHandler(LoggingHandler.Level.DEBUG);
+ adapter.setLoggerName("TEST_LOGGER");
+ adapter.setLogExpressionString("headers.id + ': ' + payload");
+ return adapter;
+ }
+
+ @MessagingGateway(defaultRequestChannel = "logChannel")
+ public interface MyGateway {
+
+ void sendToLogger(String data);
+
+ }
+
+}
+----