首页 文章

Spring启动 Logger 不记录任何内容

提问于
浏览
3

我使用Spring Boot(1.5.8.RELEASE)创建了一个Spring MVC项目 . 我选择了web,jpa和mysql,并在我的 build.gradle 中完全没有添加任何内容 .

这是我的相关部分 application.yml

logging:
  level: trace

这就是有关日志记录的所有配置 . 我只想让日志消息显示在控制台中,所以我只省略了 logging.path .

我有控制器

package com.example.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// other imports ...

@RestController
@RequestMapping("/")
public class DummyController {

  private static final Logger LOGGER = 
    LoggerFactory.getLogger(DummyController.class);

  @GetMapping
  public void foo() {
    LOGGER.info("foo");
  }
}

当我向 / 发送请求时,我收到 200 OK 响应,但日志中没有任何内容 . 奇怪的是,Spring框架和Hibernate的日志消息确实出现在控制台中 . 只是我的ad-hoc日志消息丢失了 .

然后我调试了我的代码,发现 LOGGER ( Logger 树的根)的祖先是一个日志级别为 INFO 的 Logger . 那's apparently not the logger I configured. However, the official documentation of Spring doesn'告诉我如何获得我想要的 Logger 实例 . 我怎样才能做到这一点?我的代码有问题,还是我在配置中遗漏了什么?

1 回答

  • 2

    official documentation很简单,可以使用 logging.level.root 配置根 Logger . 但实际上,您可以单独为每个包/类配置日志记录

    logging.level.root=TRACE                     # default for everything
    logging.level.com.example.controllers=TRACE  # for your controllers package
    

    类似的application.yml:

    logging:
     level:
       root: TRACE 
       com.example.controllers: TRACE
    

相关问题