在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
答案 0 :(得分:1)
您不需要在pom.xml中为Logback添加依赖项,因为Spring引导提供了SLF4J和Logback依赖项。 Spring Boot为配置文件中的Logback提供默认配置,默认情况下可以在应用程序中打印INFO消息。如果要在日志中获取DEBUG消息,则必须在application.properties文件中进行配置。在属性文件中添加logging.level.your package = DEBUG。例如,你的包是&#34; com.my.app&#34;然后使用
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
查看日志记录级别的详细信息