0

我知道 logback.xml 可以轻松地每天、每小时或每周轮换我的日志。如何配置时间戳fileNamePattern以便每半小时或“x”分钟轮换一次日志?

或者这里是否需要进行代码级别的更改?如果是这样,对于高性能业务级应用程序来说,它是线程安全且高效的吗?

4

3 回答 3

1

您无需更改代码。更改配置文件 (logback.xml) 就足够了。将 appender 的配置更改为类似于

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- keep 30 days' worth of history capped at 3GB total size -->
      <maxHistory>30</maxHistory>
      <totalSizeCap>3GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

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

有关详细信息,请参阅https://logback.qos.ch/manual/appenders.html 。例如使用 logFile.%d{yyyy-MM-dd_HH-mm}.log
在每分钟开始滚动。

于 2018-06-19T12:15:23.407 回答
0

我像这样创建了 TriggeringPolicy 和 RollingPolicy:

class MyTriggeringPolicy<E> extends DefaultTimeBasedFileNamingAndTriggeringPolicy<E> {
  protected int periodMinutes = 1;

  public int getPeriodMinutes() {
    return this.periodMinutes;
  }

  public void setPeriodMinutes(int minutes) {
    if (minutes > 0) {
      this.periodMinutes = minutes;
    }
  }

  private long roundTimestamp(long time) = {
    int periodTicks = periodMinutes * 60000;
    return periodTicks * (time / periodTicks);
  }

  @Override
  public void setDateInCurrentPeriod(long now) {
    dateInCurrentPeriod.setTime(roundTimestamp(now));
  }

  @Override
  public void setDateInCurrentPeriod(Date _dateInCurrentPeriod) {
    dateInCurrentPeriod = new Date(roundTimestamp(_dateInCurrentPeriod.getTime()));
  }

  @Override
  public void computeNextCheck() {
    rc.setTime(dateInCurrentPeriod);
    rc.set(Calendar.SECOND, 0);
    rc.set(Calendar.MILLISECOND, 0);
    rc.add(Calendar.MINUTE, periodMinutes);

    nextCheck = rc.getTime().getTime();
  }

  @Override
  public String toString() {
    return "MyTriggeringPolicy";
  }
}
class MyRollingPolicy<E> extends TimeBasedRollingPolicy<E> {
  private int periodMinutes;

  public int getPeriodMinutes() {
    return this.periodMinutes;
  }

  public void setPeriodMinutes(int minutes) {
    this.periodMinutes = minutes;
  }

  @Override
  public void start() {
    MyTriggeringPolicy<E> triggeringPolicy = new MyTriggeringPolicy<E>();
    triggeringPolicy.setPeriodMinutes(periodMinutes);
    setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);

    super.start();
  }

  @Override
  public String toString() {
    return "MyRollingPolicy";
  }
}
    <rollingPolicy class="MyRollingPolicy">
      <periodMinutes>5</periodMinutes>
      <fileNamePattern>application-%d{yyyyMMddHHmm}.log</fileNamePattern>
    </rollingPolicy>

这是 5,10,15... 分钟后的翻转。

例如,“application-201910301100.log”包含从 2019-10-30 11:00:00 到 2019-10-30 11:04:59 的日志。

于 2019-10-30T02:29:49.133 回答
0
import ch.qos.logback.core.joran.spi.NoAutoStart;
import ch.qos.logback.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy;

@NoAutoStart
public class MyTimeBasedFileNamingAndTriggeringPolicy<E> extends DefaultTimeBasedFileNamingAndTriggeringPolicy<E> {
    // This is used to specify the time interval
    private Integer multiple = 1;

    @Override
    protected void computeNextCheck() {
        nextCheck = rc.getEndOfNextNthPeriod(dateInCurrentPeriod, multiple).getTime();
    }

    public Integer getMultiple() {
        return multiple;
    }

    public void setMultiple(Integer multiple) {
        if (multiple > 1) {
            this.multiple = multiple;
        }
    }
}
    <appender name="FILE-SFTP-OperationLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>${FILE_PATH_SFTP_OperationLog}</File>
        
        <!--        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">-->
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          
            <FileNamePattern>${FILE_PATH_SFTP_OperationLog}_%d{"yyyy-MM-dd_HH-mm", UTC}_0.zip</FileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="xxx.yyy.zzz.api.config.MyTimeBasedFileNamingAndTriggeringPolicy">
                <multiple>5</multiple>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--            <MaxFileSize>100MB</MaxFileSize>-->
            <!--            <totalSizeCap>1GB</totalSizeCap>-->
            <MaxHistory>90</MaxHistory>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <encoder>
            <pattern>
{
    "time": "%d{yyyy-MM-dd HH:mm:ss.SSS}",
    "level": "%level",
    "thread": "%thread",
    "class": "%logger{40}",
    "message": "%message"
}%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
于 2021-11-23T05:36:46.620 回答