gpt4 book ai didi

java - SpringApplication.run 后不打印 Logback

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:29 26 4
gpt4 key购买 nike

在 SpringApplication.run 之后,Logback 不打印但 System.out.println 打印。我正在尝试打印出在上下文中加载的 bean。当然,我可以使用 System.out 做到这一点。但我想用日志来做到这一点。谁能解释我在下面的代码中缺少什么?

import java.util.Arrays;    

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class MyApplication {

final static Logger logger = LoggerFactory.getLogger(MyApplication.class);

public static void main(String[] args) {

// This works fine. I am using logback behind the slf4j api.
logger.debug("Hello world from Spring Boot.");

ApplicationContext appContext = SpringApplication.run(
MyApplication.class, args);

// The system.out works all right. The debug does not. Why?
logger.debug("Let's inspect the beans provided by Spring Boot:");
System.out.println("Let's inspect the beans provided by Spring Boot:");

String[] beanNames = appContext.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
}

/pom.xml

<groupId>fun.and.games</groupId>
<artifactId>learnspringboot</artifactId>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

</dependencies>

<build>
<plugins>

<!-- mvn -e clean install exec:java -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>fun.and.games.MyApplication</mainClass>
</configuration>
</plugin>

<!-- Configure the project to use java 8 version. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- Disable annotation processing for ourselves. -->
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>

<!-- mvn -e clean install spring-boot:run -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

src/main/resources/application.properties

logging.level.root=DEBUG

最佳答案

你不需要在 pom.xml 中添加 Logback 依赖,因为 spring boot 提供了 SLF4J 和 Logback 依赖。 Spring Boot 在配置文件中为 Logback 提供了一个默认配置,在你的应用程序中可以默认打印 INFO 消息。如果您想在日志中获取 DEBUG 消息,则必须在 application.properties 文件中进行配置。在属性文件中添加 logging.level.your package=DEBUG。例如你的包是“com.my.app”然后使用

logging.level.com.my.app=DEBUG

并且Spring web DEBUG日志可以像这样启用

logging.level.org.springframework.web=DEBUG

请在 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html 查看日志记录级别的详细信息

关于java - SpringApplication.run 后不打印 Logback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39684358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com