【问题标题】:Spring Boot Logging Properties vs Log4J 2 PropertiesSpring Boot 日志记录属性与 Log4J 2 属性
【发布时间】: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


【解决方案1】:

如果你通过这个 Spring Boot 属性定义它,它应该使用你的配置文件:

logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback

【讨论】:

  • 添加 logging.config=classpath:log4j2-spring.xml 不起作用。一个最小的例子是github.com/ximanta/springbootwebapp-part2
  • 存储库中缺少文件 log4j2-spring.xml。
  • @luboskmac 对不起!刚刚添加。
猜你喜欢
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 2019-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
相关资源
最近更新 更多