logback文件包含取决于内容的顺序

时间:2013-01-17 01:18:33

标签: slf4j logback

logback file inclusion正在按顺序进行配置。我排除了根目录并将其保留在${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。虽然我在appender之后改变了标签和kep的位置,但是它会抱怨同样的错误。

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?

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

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

1 个答案:

答案 0 :(得分: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>

`