如何禁用Thymeleaf日志?仅保留异常日志输出
解决Thymeleaf控制台日志过多问题:仅保留异常日志输出
Thymeleaf基于SLF4J日志门面输出日志,默认未指定具体日志实现时会启用SLF4J的SimpleLogger,输出大量INFO、DEBUG级别的日志。要只保留异常日志,只需将Thymeleaf相关包的日志级别设置为ERROR即可,以下是两种可行方案:
方案一:使用Logback(推荐,功能更灵活)
- 添加Logback依赖到pom.xml:
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.4.8</version> </dependency>
- 在
src/main/resources下创建logback.xml配置文件:
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- 屏蔽Thymeleaf非异常日志 --> <logger name="org.thymeleaf" level="ERROR" /> <logger name="org.thymeleaf.templateparser" level="ERROR" /> <root level="INFO"> <appender-ref ref="CONSOLE" /> </root> </configuration>
配置完成后,仅当Thymeleaf抛出异常时才会输出日志,其余INFO/DEBUG级别的日志会被屏蔽。
方案二:使用SLF4J SimpleLogger(无需额外依赖)
如果不想引入新框架,可直接调整SimpleLogger的级别:
方式1:通过JVM启动参数设置
运行Test类时添加以下参数:
-Dorg.slf4j.simpleLogger.defaultLogLevel=info -Dorg.slf4j.simpleLogger.log.org.thymeleaf=error
方式2:通过配置文件设置
在src/main/resources下创建simplelogger.properties文件:
org.slf4j.simpleLogger.defaultLogLevel=info org.slf4j.simpleLogger.log.org.thymeleaf=error
这种方式适合简单测试项目,无需额外依赖。
测试你的代码后,控制台只会输出程序正常结果,仅当Thymeleaf出现异常(如模板语法错误)时才会输出错误日志。
内容的提问来源于stack exchange,提问作者vished2000
相关产品推荐
相关产品推荐

