Spring Cloud Stream使用StreamBridge推送Kafka时大量日志输出问题
解决StreamBridge推送Kafka时无法控制的控制台日志问题
这个日志是Spring Cloud Stream 3.2.7及更早版本中FunctionInvoker类通过System.out.println()直接打印的,没有使用SLF4J日志框架,所以Logback配置无法对其进行级别控制或路由到Logstash。以下是两种解决方法:
方法1:升级Spring Cloud Stream版本
- 升级到3.2.8及以上版本,该版本已修复此问题,将
System.out输出替换为SLF4J日志,之后即可通过Logback配置控制该日志的级别(比如设置为OFF或WARN)。 - 修改依赖管理中的版本号(以Maven为例):
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-dependencies</artifactId> <version>3.2.8</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
方法2:临时拦截System.out输出(无法升级时)
如果暂时无法升级版本,可以通过重定向System.out来过滤掉这条特定日志:
- 在Spring Boot启动类中添加自定义PrintStream实现:
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; public class FilteredPrintStream extends PrintStream { private static final String FILTER_LOG = "Invoking function: streamBridge<org.springframework.messaging.Message<java.lang.Object>, org.springframework.messaging.Message<java.lang.Object>>with input type: org.springframework.messaging.Message<java.lang.Object>"; private final PrintStream original; public FilteredPrintStream(PrintStream original) { super(new ByteArrayOutputStream(), true); this.original = original; } @Override public void println(String x) { if (!x.contains(FILTER_LOG)) { original.println(x); } } @Override public void write(byte[] buf, int off, int len) throws IOException { String content = new String(buf, off, len); if (!content.contains(FILTER_LOG)) { original.write(buf, off, len); } } }
- 在启动方法中替换System.out:
@SpringBootApplication public class YourApplication { public static void main(String[] args) { System.setOut(new FilteredPrintStream(System.out)); SpringApplication.run(YourApplication.class, args); } }
内容的提问来源于stack exchange,提问作者RichieK
相关产品推荐
相关产品推荐

