不知何故,Logback不是打印,而是System.out.println,仅次于SpringApplication.run。我试图打印出在上下文中加载的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发布于 2016-09-25 12:39:36
您不需要在pom.xml中为Logback添加依赖项,因为spring引导提供了SLF4J和Logback依赖项。Spring在配置文件中为Logback提供了默认配置,并且在应用程序中默认情况下可以打印信息消息。如果希望在日志中获取调试消息,则必须在application.properties文件中配置该消息。在属性文件中添加logging.level.your package=DEBUG。例如,您的包是"com.my.app“,然后使用
logging.level.com.my.app=DEBUG而且Spring调试日志可以启用如下
logging.level.org.springframework.web=DEBUG请查看http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html日志记录级别的详细信息
https://stackoverflow.com/questions/39684358
复制相似问题