首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >登录后不打印SpringApplication.run

登录后不打印SpringApplication.run
EN

Stack Overflow用户
提问于 2016-09-25 07:14:24
回答 1查看 1.4K关注 0票数 3

不知何故,Logback不是打印,而是System.out.println,仅次于SpringApplication.run。我试图打印出在上下文中加载的bean。当然,我可以用System.out做到这一点。但我想用日志来做。有人能解释我在下面的代码中遗漏了什么吗?

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
logging.level.root=DEBUG
EN

回答 1

Stack Overflow用户

发布于 2016-09-25 12:39:36

您不需要在pom.xml中为Logback添加依赖项,因为spring引导提供了SLF4J和Logback依赖项。Spring在配置文件中为Logback提供了默认配置,并且在应用程序中默认情况下可以打印信息消息。如果希望在日志中获取调试消息,则必须在application.properties文件中配置该消息。在属性文件中添加logging.level.your package=DEBUG。例如,您的包是"com.my.app“,然后使用

代码语言:javascript
复制
logging.level.com.my.app=DEBUG

而且Spring调试日志可以启用如下

代码语言:javascript
复制
logging.level.org.springframework.web=DEBUG

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39684358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档