Java 回滚日志文件大小和时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2602415/
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-13 09:44:34  来源:igfitidea点击:

Rolling logback logs on filesize and time

javalogginglogback

提问by Naftuli Kay

I've been trying to set up a simple logback project to roll my log files by date and by filesize, and so far I have been unable to get my appender to roll over to another file. Instead, it writes to the log specified by the <file/> tag.

我一直在尝试建立一个简单的 logback 项目来按日期和文件大小滚动我的日志文件,到目前为止我一直无法让我的 appender 滚动到另一个文件。相反,它写入由 <file/> 标记指定的日志。

Here is my logback.xml configuration file:

这是我的 logback.xml 配置文件:

<?xml version="1.0"?>
<configuration scan="true" scanPeriod="10 seconds">
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
        </layout>
    </appender>

    <appender name="milliroller" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>log/output.log</file>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
        </layout>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>log/mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>1KB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="stdout"/>
        <appender-ref ref="milliroller"/>
    </root>

    <logger name="com.tkassembled.logback" level="DEBUG"/>
</configuration>

At first glance, it looks like it should work, right? Is there something I'm doing wrong? My entire, buildable project is available in a zip here: http://www.mediafire.com/file/2bxokkdyz2i/logback.zip

乍一看,它应该可以工作,对吧?有什么我做错了吗?我的整个可构建项目在此处以 zip 格式提供:http: //www.mediafire.com/file/2bxokkdyz2i/logback.zip

采纳答案by Robert H

Although this is an old question, I felt that a working answer is appropriate to help anyone who requires this kind of implementation.

虽然这是一个老问题,但我觉得一个有效的答案适合帮助任何需要这种实现的人。

I use the following logback configuration to provide an HTML log, rolled over by date and filesize, as well as logging to console for debugging output.

我使用以下 logback 配置来提供一个 HTML 日志,按日期和文件大小滚动,以及记录到控制台以进行调试输出。

Logfiles are stored in a logsdirectory with a name of logFile.htmlwhile its active, and logFile.2013-mm-dd.i.htmlwhen it rolls over, where i is the number of 50MB log files. For instance logFile.2013-01-07.0.html.

日志文件存储在一个logs名为 的目录中,logFile.html当它处于活动状态时,logFile.2013-mm-dd.i.html当它翻转时,其中 i 是 50MB 日志文件的数量。例如logFile.2013-01-07.0.html

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <charset>UTF-8</charset>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{35}) - %msg %n</pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logs\logFile.html</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logs\logFile.%d{yyyy-MM-dd}.%i.html</fileNamePattern>
      <timeBasedFileNamingAndTriggeringPolicy
          class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
          <!-- or whenever the file size reaches 50MB -->
        <maxFileSize>50MB</maxFileSize>
      </timeBasedFileNamingAndTriggeringPolicy>
      <!-- keep 30 days' worth of history -->
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
      <charset>UTF-8</charset>
      <layout class="ch.qos.logback.classic.html.HTMLLayout">
        <pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
      </layout>         
    </encoder>
  </appender> 

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

</configuration>

回答by Freiheit

You should be able to set up your log using a FileHandler.

您应该能够使用 FileHandler 设置日志。

http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/FileHandler.html#FileHandler(java.lang.String, int, int)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/FileHandler.html#FileHandler(java.lang.String, int, int)

This takes a file size limit and rotates the log when it hits that limit.

这需要文件大小限制并在达到该限制时轮换日志。

回答by AndreLDM

Since logback 1.1.7 (released March 2016) a new appender called SizeAndTimeBasedRollingPolicyis available that dramatically simplifies what you need to do:

自 logback 1.1.7(2016 年 3 月发布)以来,一个名为的新 appenderSizeAndTimeBasedRollingPolicy可用,它极大地简化了您需要做的事情:

<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>app.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <fileNamePattern>app-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <maxFileSize>10MB</maxFileSize>    
        <maxHistory>30</maxHistory>
        <totalSizeCap>1GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <pattern>%msg%n</pattern>
    </encoder>
</appender>

See herefor further info.

请参阅此处了解更多信息。