如何屏蔽第三方Jar的System.out.println,仅显示SLF4J日志(Maven环境)
Got it, let's break this down for you—you want to keep your SLF4J logs front-and-center in the build console while shoving all those unwanted System.out.println() calls from the third-party JAR into a file, no custom parsing scripts required. Here are the cleanest Maven-based solutions tailored to different scenarios:
Option 1: For Apps Run via Maven Exec Plugin
If you're launching your application using mvn exec:java, you can directly configure the Exec plugin to redirect System.out to a file. Since SLF4J logs use their own appenders (like ConsoleAppender in Logback/Log4j), they won't be caught by this redirect and will stay in your console.
Add this to your pom.xml:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>your.package.MainClass</mainClass> <!-- Send all System.out output to this file --> <outputFile>target/third-party-system-out.log</outputFile> </configuration> </plugin> </plugins> </build>
Run your app with mvn exec:java—you'll see only SLF4J logs in the console, and all third-party System.out lines will land in target/third-party-system-out.log.
Option 2: For Test Execution (Maven Surefire Plugin)
If the unwanted output is coming during test runs, configure the Surefire plugin to split test output into files while keeping SLF4J logs in the console.
Update your pom.xml with this Surefire config:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <configuration> <!-- Redirect test-related System.out/err to files --> <redirectTestOutputToFile>true</redirectTestOutputToFile> <testOutputDirectory>target/test-outputs</testOutputDirectory> <!-- Optional: Customize file names per test class --> <redirectTestOutputToFile>true</redirectTestOutputToFile> <useFile>true</useFile> </configuration> </plugin> </plugins> </build>
When you run mvn test, Surefire will write all test-related System.out output to individual files in target/test-outputs, while your SLF4J logs will still print directly to the build console.
Option 3: Advanced JVM-Level Redirect (For Edge Cases)
If the first two options don't cover your scenario (e.g., third-party output happens during non-exec/test build phases), you can use a tiny Java agent to redirect System.out at startup—no changes to your main code needed.
- Create a simple agent class:
import java.io.FileOutputStream; import java.io.PrintStream; public class SystemOutRedirectAgent { public static void premain(String args, java.lang.instrument.Instrumentation inst) throws Exception { // Redirect System.out to a file PrintStream fileOut = new PrintStream(new FileOutputStream("target/global-system-out.log")); System.setOut(fileOut); // Uncomment below to redirect System.err too // System.setErr(fileOut); } }
- Add this agent to your Maven build and reference it in your execution plugin:
<build> <plugins> <!-- Compile the agent --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- Package agent into a JAR with manifest --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifestEntries> <Premain-Class>SystemOutRedirectAgent</Premain-Class> </manifestEntries> </archive> </configuration> </plugin> <!-- Use the agent when running your app --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <configuration> <mainClass>your.package.MainClass</mainClass> <jvmArgs>-javaagent:target/your-agent-jar-name.jar</jvmArgs> </configuration> </plugin> </plugins> </build>
Note: If your SLF4J setup uses System.out directly (uncommon, but possible), you'll need to tweak your log config to use the original console stream instead of the redirected one. For Logback, you can define a custom appender that references the original System.out captured in the agent.
内容的提问来源于stack exchange,提问作者Kumar Gaurav

