Spring Boot - >使用logback-spring.xml,翻转无效

时间:2016-09-30 11:48:07

标签: spring spring-boot logback

我正在使用Spring Boot 1.3.2,我有如下的logback-spring.xml -

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/defaults.xml" />
  <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
  <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
 <root level="INFO">
    <appender-ref ref="FILE" />
  </root> 
</configuration>

我的pom.xml有 -

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
</dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
     </dependency>

我的application.properties文件是 -

logging.file=xyz.log

但是当日志文件达到10 MB时,它们不会回滚。尝试使用下面的代码,仍然没有回滚日志文件。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/defaults.xml" />
  <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
  <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/log/xyz.log</file>       
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <fileNamePattern>/log/xyz%i.log</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>10</maxIndex>      
    </rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <encoder>

        <pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>
  <root level="INFO">
    <appender-ref ref="FILE" />
  </root> 
</configuration>

感谢任何帮助/建议!!!

1 个答案:

答案 0 :(得分:1)

Spring-boot自动滚动10MB点的日志文件 只要您配置了日志文件属性,就不需要执行任何其他操作 启动器pom中包含了logback依赖项,同时你也不应该使用log4j和logback。 删除日志记录xml配置,从您的pom中删除所有日志记录依赖项,它将正常工作 为了完整起见,如果您想使用10MB以外的大小,则需要添加一个logback配置 最简单的方法是复制现有的基本配置并实现自己的文件追加器规则。

示例logback-spring.xml,使用20MB滚动尺寸:
如果您只想要默认的10MB,则无需使用此功能。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Import default spring logback configuration -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

    <!-- Define your file-appender -->
    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>20MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</configuration>