Spring Integration SFTP:如何为指定Channel配置日志记录
解决SFTP流程日志记录问题
一、获取loggingChannel的日志
你已经配置了<int:logging-channel-adapter id="loggingChannel"/>,要看到输出的日志需完成两步:
- 配置日志框架级别
Spring Integration的日志依赖SLF4J+Logback/Log4j2实现,需要将org.springframework.integration的日志级别设为INFO或更低(如DEBUG)。以Logback为例,在logback.xml中添加:
<logger name="org.springframework.integration" level="INFO" />
这样就能捕获到loggingChannel输出的日志内容。
- 优化日志内容(可选)
默认日志仅输出消息体,可通过expression配置更详细的信息(比如包含消息头):
<int:logging-channel-adapter id="loggingChannel" expression="RM操作结果: payload, 远程文件: #{headers['file_remoteDirectory'] + '/' + headers['file_remoteFile']}" level="INFO"/>
二、给getChannel和removeChannel添加日志记录
推荐使用Wire Tap(线窃听),它不会干扰原消息流转,仅复制消息到日志通道,是无侵入式的日志方案。
配置方式:给通道添加wire-tap
修改getChannel和removeChannel的定义,添加wire-tap属性指向对应的日志适配器:
<int:channel id="getChannel"> <int:wire-tap channel="getLoggingChannel"/> </int:channel> <int:channel id="removeChannel"> <int:wire-tap channel="removeLoggingChannel"/> </int:channel> <!-- 定义专属日志适配器 --> <int:logging-channel-adapter id="getLoggingChannel" expression="准备GET远程文件: #{payload.remoteDirectory + '/' + payload.filename}" level="INFO"/> <int:logging-channel-adapter id="removeLoggingChannel" expression="准备RM远程文件: #{headers['file_remoteDirectory'] + '/' + headers['file_remoteFile']}" level="INFO"/>
修改后的完整SftpOutboundGateway.xml示例
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp" xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.1.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration/sftp https://www.springframework.org/schema/integration/sftp/spring-integration-sftp-4.1.xsd"> <import resource="web.xml"/> <int:gateway id="gw" service-interface="ToSftpFlowGateway" default-request-channel="inputChannel" default-reply-timeout="1000"/> <int:channel id="inputChannel"> </int:channel> <int-sftp:outbound-gateway id="gatewayLS" auto-startup="true" session-factory="sftpSessionFactory" request-channel="inputChannel" command="ls" expression="payload" reply-channel="toSplitter"> </int-sftp:outbound-gateway> <int:splitter input-channel="toSplitter" output-channel="getChannel" delimiters="/"/> <int:channel id="getChannel"> <int:wire-tap channel="getLoggingChannel"/> </int:channel> <int-sftp:outbound-gateway id="gatewayGET" local-directory-expression="'C:/temp/'" auto-create-local-directory="true" session-factory="sftpSessionFactory" request-channel="getChannel" reply-channel="removeChannel" command="get" mode="IGNORE" expression="payload.remoteDirectory + '/' + payload.filename"> </int-sftp:outbound-gateway> <int:channel id="removeChannel"> <int:wire-tap channel="removeLoggingChannel"/> </int:channel> <int-sftp:outbound-gateway id="gatewayRM" request-channel="removeChannel" session-factory="sftpSessionFactory" expression="headers['file_remoteDirectory'] + headers['file_remoteFile']" reply-channel="loggingChannel" command="rm"> </int-sftp:outbound-gateway> <int:logging-channel-adapter id="loggingChannel" expression="RM操作结果: payload, 远程文件: #{headers['file_remoteDirectory'] + '/' + headers['file_remoteFile']}" level="INFO"/> <int:logging-channel-adapter id="getLoggingChannel" expression="准备GET远程文件: #{payload.remoteDirectory + '/' + payload.filename}" level="INFO"/> <int:logging-channel-adapter id="removeLoggingChannel" expression="准备RM远程文件: #{headers['file_remoteDirectory'] + '/' + headers['file_remoteFile']}" level="INFO"/> </beans>
补充:Logback日志框架配置示例
在项目src/main/resources下创建logback.xml:
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="org.springframework.integration" level="INFO"/> <logger name="org.springframework.integration.sftp" level="DEBUG"/> <!-- 可选,查看SFTP操作细节 --> <root level="INFO"> <appender-ref ref="CONSOLE"/> </root> </configuration>
配置完成后,控制台会输出三类日志:
getChannel中待下载的文件信息removeChannel中待删除的文件信息loggingChannel中删除操作的结果
内容的提问来源于stack exchange,提问作者terrinizer
相关产品推荐
相关产品推荐

