4

logback文件包含需要按顺序进行配置。我排除了根并将其保留在${catalina.base}/conf/includedConfig.xml

<?xml version="1.0"?>
<included>
  <root level="INFO">
    <appender-ref ref="FILE"/>
  </root>
</included>

和 src/main/resources 下的 logback.xml

<?xml version="1.0"?>
<configuration>
   <include file="${catalina.base}/conf/includedConfig.xml"/>

 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${catalina.base}/logs/logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
        <maxHistory>7</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>%date [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>

它抱怨找不到附加程序。虽然我改变了标签的位置并在 appender 之后保留它,但它抱怨同样的错误。

17:05:12,566 |-ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - Cou
ld not find an appender named [FILE]. Did you define it below instead of above in 
the configuration file?

如果我以相反的顺序执行相同的操作,即将附加程序放在外部文件中,并将根放在 logback.xml 中,它工作正常。

我想这样做的原因是,我想在构建(相同的工件)被提升到不同的环境时更改日志级别,如果日志级别处于应用程序战争中,这是不可能的。

4

1 回答 1

0

尝试像这样添加:`

<appender name="all_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>INFO</level>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>./log/mylog_all_info.%d{yyyy-MM-dd}.log</FileNamePattern>
        <MaxHistory>30</MaxHistory>
    </rollingPolicy>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <MaxFileSize>5MB</MaxFileSize>
    </triggeringPolicy>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n</Pattern>
    </layout>
</appender>

<root>
    <appender-ref ref="all_info" level="INFO" />
</root>

`

于 2016-12-29T04:29:16.883 回答