Logging Channel Adapter

通常将 <logging-channel-adapter>`与讨论于 Wire Tap中的备用源一起使用。但是,它也可被用作任意流的最终使用者。例如,考虑一个以返回结果的 `<service-activator>`结尾的流,但您希望放弃该结果。为此,您可以将结果发送到 `NullChannel。或者,您可以将它路由至 <logging-channel-adapter>`级别的 `INFO。如此一来,在 INFO`级别记录时,您可以看到被抛弃的消息,但在(例如)`WARN`级别记录时看不见它。使用 `NullChannel,您只有在 `DEBUG`级别记录时才能看到被抛弃的消息。以下清单显示了 `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

以下 Spring Boot 应用程序展示了如何使用 Java 配置配置 LoggingHandler 的示例:

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

以下 Spring Boot 应用程序展示了使用 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);

    }

}