View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  import java.io.File;
17  import java.util.Date;
18  
19  public class SizeAndTimeBasedArchiveRemover extends DefaultArchiveRemover {
20  
21    public SizeAndTimeBasedArchiveRemover(FileNamePattern fileNamePattern,
22        RollingCalendar rc) {
23      super(fileNamePattern, rc);
24    }
25  
26    public void cleanByPeriodOffset(Date now, int periodOffset) {
27      Date dateOfPeriodToClean = rc.getRelativeDate(now, periodOffset);
28  
29      String regex = fileNamePattern.toRegex(dateOfPeriodToClean);
30      String stemRegex = FileFilterUtil.afterLastSlash(regex);
31      File archive0 = new File(fileNamePattern.convertMultipleArguments(
32          dateOfPeriodToClean, 0));
33      // in case the file has no directory part, i.e. if it's written into the
34      // user's current directory.
35      archive0 = archive0.getAbsoluteFile();
36  
37      File parentDir = archive0.getAbsoluteFile().getParentFile();
38      File[] matchingFileArray = FileFilterUtil.filesInFolderMatchingStemRegex(
39          parentDir, stemRegex);
40  
41      for (File f : matchingFileArray) {
42        f.delete();
43      }
44  
45      if (parentClean) {
46        removeFolderIfEmpty(parentDir);
47      }
48    }
49  
50  
51  }