[英]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.root和logging.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.