Products
GG网络技术分享 2025-10-25 10:01 1
我们兴许希望少许些日志输出,特别是INFO级别的日志,以少许些日志文件的体积和搞优良性能嗯。

在SpringBoot中,最常用的配置文件是application.yml。在yml文件中,我们能通过设置logging.level.root的值来改变日志级别。
logging:
level:
root: ERROR
如果需要在代码中动态改变日志级别,能用Logback的API进行设置。
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.Level;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.loggingLogbackClassicLoggerFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class LoggingLevelApplication {
public static void main {
SpringApplication.run;
}
@Bean
public org.springframework.boot.loggingLogbackClassicLoggerFactory customizeLogging {
LoggerContext loggerContext = LoggerFactory.getILoggerFactory;
Logger rootLogger = loggerContext.getLogger;
rootLogger.setLevel;
return new org.springframework.boot.loggingLogbackClassicLoggerFactory;
}
}
在启动SpringBoot应用时能通过命令行参数来设置日志级别。
比方说:java -jar yourapp.jar --logging.level.root=ERROR
通过以上三种方法,我们能轻巧松地在SpringBoot中禁用INFO级别的日志输出。根据具体场景选择合适的方法,能有效提升应用的性能和用户体验。
欢迎用实际体验验证以上观点。
Demand feedback