Logback RollingFileAppender不工作

时间:2013-09-13 16:35:22

标签: java logging logback

我有以下logback.xml文件:

<configuration>

    <!--Daily rolling file appender -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>/usr/share/tomcat6/logs/api.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>/usr/share/tomcat6/logs/api/api.%d{yyyy-MM-dd}.gz</FileNamePattern>
        </rollingPolicy>
        <encoder>
          <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%msg%n</pattern>
        </encoder>
    </appender>

      <root level="debug">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
      </root>
</configuration>

我的日志文件运行正常。然而,folling文件方面不是。它不是将文件压缩并将其移动到api文件夹中,而是将其放在同一目录中并将其重命名为

api.log(string of numbers).tmp

e.g。

api.log849916939395200.tmp

有谁知道为什么会这样?

2 个答案:

答案 0 :(得分:8)

只需从appender中删除文件标记即可。使用类似的东西,

<appender name="contentDeliveryLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <!-- daily rollover -->
    <fileNamePattern>${ICEX_HOME}/logs/content-delivery.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- keep 1 days' worth of history -->
      <maxHistory>30</maxHistory>
  </rollingPolicy>
  <encoder>
    <pattern>%d [%thread] %-5level %logger{36} H:${HOSTNAME} - SC:%X{optionalParam} %msg%n</pattern>
  </encoder>
</appender>

根据logback here

的文档建议,这对我有用

答案 1 :(得分:3)

我有类似的问题。要解决此问题,请将模式更改为     /usr/share/tomcat6/logs/api/api.%d{yyyy-MM-dd}.%i.gz

你最后错过了%i