【发布时间】:2016-07-19 07:51:16
【问题描述】:
在 Spring Boot Web 应用程序中,我在 application.properties 中有以下日志记录属性。
logging.level.guru.springframework.controllers=DEBUG
logging.level.org.springframework.web=DEBUG
logging.file=logs/spring-boot-logging.log
一切正常 - 内部 Spring Boot 和应用程序的调试消息都记录到 logs/spring-boot-logging.log。但是,即使我添加了具有不同日志记录级别的 log4j2-spring.xml,application.properties 的级别仍然会被选中。
我的 log4j2-spring.xml 是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
<Properties>
<Property name="log-path">applogs</Property>
</Properties>
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>>
</PatternLayout>
</Console>
<File name="File-Appender" fileName="${log-path}/app_log.log" >
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="org.springframework.web" level="info">
<AppenderRef ref="File-Appender"/>
</Logger>
<Logger name="guru.springframework.controllers" level="info">
<AppenderRef ref="File-Appender"/>
</Logger>
<Root level="info">
<AppenderRef ref="Console-Appender"/>
</Root>
</Loggers>
</Configuration>
通过这些设置,调试(在 application.properties 中定义)消息被发送到 applogs/app_log.log(在 log4j2-spring.xml 中定义)
在应用程序属性中注释掉 logging.level.* 时,会使用 log4j2-spring.xml 中的日志级别(信息)。我假设 application.properties 的默认属性优先于 log4j2-spring.xml。有什么方法可以将 Spring Boot 配置为使用 log4j2-spring.xml 属性而不是 application.properties。另外,如果 application.properties 具有更大的优先级,为什么 log4j2-spring.xml 的文件 appender 被使用而不是 logging.file 中的那个?
【问题讨论】:
-
将
log4j2-spring.xml文件移动到项目文件夹的根目录。将此添加到您的application.properties文件logging.config=log4j2-spring.xml。 Spring boot 会直接从项目目录的根目录中拾取。
标签: spring-boot log4j2