Spring应用SLF4J日志未在控制台显示问题求助
问题:Spring Boot中SLF4J DEBUG日志无法在控制台输出
在Spring Boot应用中使用SLF4J Logger打印DEBUG日志,但控制台看不到自定义的DEBUG日志内容,以下是相关代码、日志、配置及依赖信息:
应用类代码
@SpringBootApplication public class InternalReplacerServiceApplication { public static void main(String[] args) { SpringApplication.run(InternalReplacerServiceApplication.class, args); Logger LOG = LoggerFactory.getLogger(InternalReplacerServiceApplication.class); LOG.debug("inside app"); } }
运行时控制台日志
2022-08-29 13:00:21.570 INFO 17420 --- [ restartedMain] c.j.j.InternalReplacerServiceApplication : Starting InternalReplacerServiceApplication using Java 17.0.4 on W10-lTkFoL0JX1t with PID 17420 (C:\Users\wgupta\Backend\internal_replacer_service\target\classes started by wgupta in C:\Users\wgupta\Backend\internal_replacer_service) 2022-08-29 13:00:21.572 INFO 17420 --- [ restartedMain] c.j.j.InternalReplacerServiceApplication : No active profile set, falling back to 1 default profile: "default" 2022-08-29 13:00:21.648 INFO 17420 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2022-08-29 13:00:21.648 INFO 17420 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2022-08-29 13:00:22.480 INFO 17420 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode 2022-08-29 13:00:22.484 INFO 17420 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode. 2022-08-29 13:00:22.519 INFO 17420 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 15 ms. Found 0 Redis repository interfaces. 2022-08-29 13:00:23.409 INFO 17420 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http) 2022-08-29 13:00:23.430 INFO 17420 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-08-29 13:00:23.431 INFO 17420 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.64] 2022-08-29 13:00:23.559 INFO 17420 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-08-29 13:00:23.559 INFO 17420 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1910 ms 2022-08-29 13:00:24.807 INFO 17420 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2022-08-29 13:00:24.861 INFO 17420 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '' 2022-08-29 13:00:24.879 INFO 17420 --- [ restartedMain] c.j.j.InternalReplacerServiceApplication : Started InternalReplacerServiceApplication in 3.832 seconds (JVM running for 4.524)
application.properties配置
server.port=8081 # Redis Config spring.cache.type=redis spring.redis.host=127.0.0.1 spring.redis.port=6379
pom.xml依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.lindstrom</groupId> <artifactId>m3u8-parser</artifactId> <version>0.22</version> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <!-- Parameter names --> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-parameter-names</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jdk8</artifactId> </dependency> </dependencies>
排查原因及解决方案
核心原因
Spring Boot默认的日志级别是INFO,低于INFO级别的DEBUG日志会被默认过滤,所以控制台看不到自定义的DEBUG日志。另外代码中Logger的初始化位置不够规范,但不是导致日志不输出的直接原因。
解决方案
- 配置DEBUG日志级别
在application.properties中添加日志级别配置,指定你的类所在包或具体类的日志级别为DEBUG:
# 配置目标类所在包的日志级别为DEBUG logging.level.c.j.j=DEBUG # 或者精准配置到单个类 # logging.level.c.j.j.InternalReplacerServiceApplication=DEBUG
如果需要全局开启DEBUG日志(不推荐,会输出大量框架内部日志),可以配置:
logging.level.root=DEBUG
- 规范Logger初始化(可选)
将Logger声明为类的静态成员,避免在方法内局部初始化:
@SpringBootApplication public class InternalReplacerServiceApplication { private static final Logger LOG = LoggerFactory.getLogger(InternalReplacerServiceApplication.class); public static void main(String[] args) { SpringApplication.run(InternalReplacerServiceApplication.class, args); LOG.debug("inside app"); } }
内容的提问来源于stack exchange,提问作者WISHY
相关产品推荐
相关产品推荐

