You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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来过滤掉这条特定日志:

  1. 在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);
        }
    }
}
  1. 在启动方法中替换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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 16:42:15