Java Logback RollingFileAppender 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18791154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:06:05  来源:igfitidea点击:

Logback RollingFileAppender Not Working

javalogginglogback

提问by thatidiotguy

I have the following logback.xmlfile:

我有以下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>

My log file is working just fine. The folling file aspect however is not. Instead of gzipping the file and moving it into the api folder, it is putting it in the same directory and renaming it to

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

api.log(string of numbers).tmp

api.log(string of numbers).tmp

e.g.

例如

api.log849916939395200.tmp

api.log849916939395200.tmp

Does anyone know why this is happening?

有谁知道为什么会这样?

回答by SANN3

I had the similar issue. To fix this issue change the pattern to /usr/share/tomcat6/logs/api/api.%d{yyyy-MM-dd}.%i.gz.

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

You missed the %iin the end.

你最后错过了%i

回答by Prateek Jain

Just remove the file tag from appender. Use something like this,

只需从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>

This is working for me as recommended by documentation of logback here

按照此处的 logback 文档的建议,这对我有用