Logging Channel Adapter

The <logging-channel-adapter> is often used in conjunction with a wire tap, as discussed in Wire Tap. However, it can also be used as the ultimate consumer of any flow. For example, consider a flow that ends with a <service-activator> that returns a result, but you wish to discard that result. To do that, you could send the result to NullChannel. Alternatively, you can route it to an INFO level <logging-channel-adapter>. That way, you can see the discarded message when logging at INFO level but not see it when logging at (for example) the WARN level. With a NullChannel, you would see only the discarded message when logging at the DEBUG level. The following listing shows all the possible attributes for the logging-channel-adapter element:

<int:logging-channel-adapter
    channel="" (1)
    level="INFO" (2)
    expression="" (3)
    log-full-message="false" (4)
    logger-name="" /> (5)
1 The channel connecting the logging adapter to an upstream component.
2 The logging level at which messages sent to this adapter will be logged. Default: INFO.
3 A SpEL expression representing exactly what parts of the message are logged. Default: payload — only the payload is logged. if log-full-message is specified, this attribute cannot be specified.
4 When true, the entire message (including headers) is logged. Default: false — only the payload is logged. This attribute cannot be specified if expression is specified.
5 Specifies the name of the logger (known as category in log4j). Used to identify log messages created by this adapter. This enables setting the log name (in the logging subsystem) for individual adapters. By default, all adapters log under the following name: org.springframework.integration.handler.LoggingHandler.

Using Java Configuration

The following Spring Boot application shows an example of configuring the LoggingHandler by using Java configuration:

@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
    @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);

    }

}

Configuring with the Java DSL

The following Spring Boot application shows an example of configuring the logging channel adapter by using the Java DSL:

@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 IntegrationFlow loggingFlow() {
        return IntegrationFlow.from(MyGateway.class)
                     .log(LoggingHandler.Level.DEBUG, "TEST_LOGGER",
                           m -> m.getHeaders().getId() + ": " + m.getPayload());
    }

    @MessagingGateway
    public interface MyGateway {

        void sendToLogger(String data);

    }

}