首页 文章

Spring Boot Logging属性与Log4J 2属性

提问于
浏览
0

在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和应用程序的DEBUG消息都记录到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>

使用这些设置,debug(在application.properties中定义)消息将发送到applogs / app_log.log(在log4j2-spring.xml中定义)

在注释掉应用程序属性中的logging.level . *时,将使用log4j2-spring.xml中的日志级别(info) . 我假设默认情况下,application.properties的属性优先于log4j2-spring.xml . 有没有什么办法可以配置Spring Boot来使用log4j2-spring.xml属性而不是application.properties . 另外,如果application.properties具有更高的优先级,为什么要使用log4j2-spring.xml的文件追加器而不是logging.file中的文件追加器?

1 回答

  • 0

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

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

相关问题