简体   繁体   English

Spring Boot application.properties 中的日志级别问题

[英]Issues with Log Levels in Spring Boot application.properties

I am using Log4J 2 in a Spring Boot application and trying to configure few basic logging options via application.properties.我在 Spring Boot 应用程序中使用 Log4J 2 并尝试通过 application.properties 配置几个基本的日志记录选项。

I have setup a logger in my controller class, like this.我在我的控制器类中设置了一个记录器,就像这样。

package guru.springframework.controllers;
- - - -
@Controller
public class IndexController {
  private final Logger logger = LoggerFactory.getLogger(this.getClass());
  @RequestMapping("/")
  String index(){
    logger.debug("This is a debug message");
    logger.info("This is an info message");
    logger.warn("This is a warn message");
    logger.error("This is an error message");
    return "index";
   }
}

In application.properties, I have the following properties.在 application.properties 中,我有以下属性。

logging.level.guru.springframework.controllers=DEBUG
logging.level.org.springframework.web=INFO
logging.file=logs/spring-boot-logging.log

Spring internal loggigng messages are getting logged with INFO level in the console when I start the app.当我启动应用程序时,Spring 内部日志消息在控制台中以 INFO 级别记录。 This is expected from the logging.level.org.springframework.web property.这是logging.level.org.springframework.web属性所预期的。 However, when the controller gets invoked, only messages of info() and lower gets logged.但是,当控制器被调用时,只会记录info()和 lower 的消息。 With the logging.level.guru.springframework.controllers property set to DEBUG, why isn't the message of the debug() method getting logged.logging.level.guru.springframework.controllers属性设置为 DEBUG,为什么没有记录debug()方法的消息。 If I add the following to application.properties.如果我将以下内容添加到 application.properties。

logging.level.root=ERROR

Now, no messages are sent to console on Spring startup (Obviously the Error level has been picked up this time).现在,Spring 启动时没有消息发送到控制台(显然这次错误级别已被拾取)。 Similarly the controller emits only the error() method message.类似地,控制器仅发出error()方法消息。 So, is there any specific priority of picking up levels from application.properties.那么,从 application.properties 中获取级别是否有任何特定的优先级。

Is there a way to set up different levels for a different package (Say, the controller package) independent of the level specified for logging.level.root and logging.level.org.springframework.web in application.properties.有没有办法为不同的包(比如控制器包)设置不同的级别,独立于 application.properties 中为logging.level.rootlogging.level.org.springframework.web指定的级别。

I don't want to use any other external configuration file.我不想使用任何其他外部配置文件。

You are using an old version of Spring Boot (1.2.4.RELEASE) that contains a bug .您正在使用包含bug的旧版本 Spring Boot (1.2.4.RELEASE)。 It was fixed in 1.2.6.RELEASE, but I'd recommend upgrading to 1.2.8.RELEASE (if you need to stick with 1.2.x) or, even better, to 1.3.3.RELEASE (the latest version at the time of writing).它在 1.2.6.RELEASE 中得到修复,但我建议升级到 1.2.8.RELEASE(如果您需要坚持使用 1.2.x),或者更好的是升级到 1.3.3.RELEASE(最新版本)写作时间)。 You can upgrade by updating the version of spring-boot-starer-parent that you are using in your project's pom:您可以通过更新您在项目 pom 中使用的spring-boot-starer-parent版本来升级:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以通过Spring Boot中的application.properties修改日志附加程序参数? - Is it possible to modify log appender parameters via application.properties in Spring Boot? 在 Spring Boot 中的 application.properties 中获取用户主路径 - Getting the user home path in application.properties in Spring Boot Spring Boot:Logback不从application.properties中选择值 - Spring Boot :Logback not picking values from application.properties Spring日志和application.properties - Spring logging and application.properties 如何知道生产 spring 引导应用程序中使用的日志级别? - How to know what log levels used in production spring boot application? 如何通过Spring Boot应用程序中的application.properties禁用WARN消息并仅在日志中启用INFO消息? - How to disable WARN messages and enable only INFO messages in logs through application.properties in Spring boot application? Spring Boot:如何使用 application.properties 设置日志记录级别? - Spring Boot: How can I set the logging level with application.properties? 我可以将 Spring 引导日志记录设置从 application.properties 文件移到未跟踪的文件中吗? - Can I move Spring Boot logging settings out of application.properties file and into an untracked file? Spring启动应用程序意外关闭并记录问题 - Spring boot application shutting down unexpectedly and log issues Spring-boot-每个级别的日志文件的应用程序属性配置 - Spring-boot - application properties configuration for log file per level
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM