SoFunction
Updated on 2025-03-05

How to set springboot to prohibit log output to console

We have a Spring Boot project that uses org. to log.

Similar codes are as follows:

@Slf4j
public class CTest {
    public void test() {
    	。。。
        ("Hello World!");
    }
}

When the result is running, the system not only records the logs to the log file, but also prints the logs to the console. Because there are too many logs and too dense, the screen keeps scrolling, which is dazzling, and with garbled code, it is simply unreasonable.

Why are logs automatically output to the console? I have always been ignorant and curious to ask about AI. AI tells me that the system uses org. to record logs, while SLF4J is just a log facade. The specific logging behavior is determined by the bound specific log implementation framework (such as Logback, Log4j, etc.). It is equivalent to org. only defines the log interface. The specific implementation depends on what log toolkit is used. If not specified, Spring Boot will use Logback by default.

I looked at it and found no log packages referenced, so I can be sure that our system should use Logback.

I remember that the heart of Spring Boot is control inversion (IoC), AOP and containers, and then AI said that in addition to that, there are conventions greater than configuration, automatic configuration, start dependencies, etc.

Starting dependencies provide many preparations for program startup. Automatic configuration should include what is mentioned above, and the default method of using Logback as the log framework.

1. Disable log output to the console

Back to the default output of log content to the console, this is Logback's style. If you want to disable it needs to be specified in the configuration file or code.

This configuration file does not refer to this Spring Boot project-level configuration file. They can only simply set the output level and output path of the log, but prohibit output to the console and can only be configured in proprietary configuration.

1. You can only simply set the output level and output path of the log

logging:
  level:
    root: info      # Set global log level    : debug  # Log level for specific packages  file:
    path: ./log/  # Log output path  pattern:
    file: '%d{yyyy-MM-dd HH:mm:ss} - %msg%n'  # Log file format    console: '%d{yyyy-MM-dd HH:mm:ss} - %msg%n'  # Log format for console output

2. Log configuration file settings

It needs to be created manually, and the location is also in the resources root directory, and SpringBoot will automatically recognize it.

The file contents are as follows:

resources/

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Define file log output -->
    <appender name="FILE" class="">
        <file>./log/</file>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Configuration root Use only file output -->
    <root level="info">
        <appender-ref ref="FILE" />
    </root>
</configuration>

3. You can also set it through the configuration class

Of course, you can also set it by creating a configuration class:

import ;
import ;
import ;
import ;
import ;
import ;
import org.;
import ;
import ;
import ;
import ;

@Configuration
public class LoggingConfig {

    @Bean
    public void configureLogging() {
        LoggerContext loggerContext = (LoggerContext) ();
        Logger rootLogger = ("ROOT");

        // Remove ConsoleAppender        removeConsoleAppender(rootLogger);

        // Add FileAppender        addFileAppender(loggerContext, rootLogger);
    }

    private void removeConsoleAppender(Logger rootLogger) {
        Iterator<Appender<ILoggingEvent>> iterator = ();
        while (()) {
            Appender<ILoggingEvent> appender = ();
            if (appender instanceof ConsoleAppender) {
                (appender);
                ();
            }
        }
    }

    private void addFileAppender(LoggerContext context, Logger rootLogger) {
        // Create FileAppender        FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
        (context);
        ("./log/");

        // Set log output format        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        (context);
        ("%d{yyyy-MM-dd HH:mm:ss} - %msg%n");
        ();

        (encoder);
        ();

        // Add to rootLogger        (fileAppender);
    }
}

2. Set the log output level

Logback's log output has the so-called level: Level: FATAL 0 ERROR 3 WARN 4 INFO 6 DEBUG 7

Among them, FATAL is the highest and DEBUG is the lowest. The higher the level, the smaller the chance of logging. If set to DEBUG, there may be a lot of log content.

This is part of resources/ in another project I have

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="false">
	。。。
    <!-- Level: FATAL 0  ERROR 3  WARN 4  INFO 6  DEBUG 7 -->
    <root level="warn">
        <appender-ref ref="console"/>
        <appender-ref ref="debug"/>
        <appender-ref ref="error"/>
    </root>
</configuration>

3. The problem of garbled log output to the console

I found that the log content is output to the console, and the Chinese characters in it will be garbled. The IDE, the command line that runs the jar package, and the log configuration files are all set up with UTF-8, but they are still garbled. But if you use () to output it directly to the console, it is normal.

This problem has not been resolved yet. Record it.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.