From f25df0850b90d7f280b0c56ff2f4c035b884399e Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Mon, 6 Nov 2023 11:05:19 +0100 Subject: [PATCH] more informative message when deleting files Signed-off-by: Ceki Gulcu --- .../helper/TimeBasedArchiveRemover.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedArchiveRemover.java b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedArchiveRemover.java index 71c6438766..a49ec4efb0 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedArchiveRemover.java +++ b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedArchiveRemover.java @@ -1,15 +1,13 @@ /** - * Logback: the reliable, generic, fast and flexible logging framework. - * Copyright (C) 1999-2015, QOS.ch. All rights reserved. + * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights + * reserved. * - * This program and the accompanying materials are dual-licensed under - * either the terms of the Eclipse Public License v1.0 as published by - * the Eclipse Foundation + * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License + * v1.0 as published by the Eclipse Foundation * - * or (per the licensee's choosing) + * or (per the licensee's choosing) * - * under the terms of the GNU Lesser General Public License version 2.1 - * as published by the Free Software Foundation. + * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. */ package ch.qos.logback.core.rolling.helper; @@ -86,8 +84,7 @@ public void cleanPeriod(Instant instantOfPeriodToClean) { File[] matchingFileArray = getFilesInPeriod(instantOfPeriodToClean); for (File f : matchingFileArray) { - addInfo("deleting " + f); - f.delete(); + checkAndDeleteFile(f); } if (parentClean && matchingFileArray.length > 0) { @@ -96,6 +93,23 @@ public void cleanPeriod(Instant instantOfPeriodToClean) { } } + private boolean checkAndDeleteFile(File f) { + addInfo("deleting " + f); + if (f == null) { + addWarn("Cannot delete empty file"); + return false; + } else if (!f.exists()) { + addWarn("Cannot delete non existent file"); + return false; + } + + boolean result = f.delete(); + if (!result) { + addWarn("Failed to delete file " + f.toString()); + } + return result; + } + void capTotalSize(Instant now) { long totalSize = 0; long totalRemoved = 0; @@ -107,8 +121,10 @@ void capTotalSize(Instant now) { long size = f.length(); if (totalSize + size > totalSizeCap) { addInfo("Deleting [" + f + "]" + " of size " + new FileSize(size)); + // assume that deletion attempt will succeed. totalRemoved += size; - f.delete(); + + checkAndDeleteFile(f); } totalSize += size; } @@ -141,7 +157,7 @@ int computeElapsedPeriodsSinceLastClean(long nowInMillis) { /** * Computes whether the fileNamePattern may create sub-folders. - * + * * @param fileNamePattern * @return */ @@ -183,9 +199,8 @@ void removeFolderIfEmpty(File dir) { } /** - * Will remove the directory passed as parameter if empty. After that, if the - * parent is also becomes empty, remove the parent dir as well but at most 3 - * times. + * Will remove the directory passed as parameter if empty. After that, if the parent is also becomes empty, remove + * the parent dir as well but at most 3 times. * * @param dir * @param depth @@ -197,7 +212,7 @@ private void removeFolderIfEmpty(File dir, int depth) { } if (dir.isDirectory() && FileFilterUtil.isEmptyDirectory(dir)) { addInfo("deleting folder [" + dir + "]"); - dir.delete(); + checkAndDeleteFile(dir); removeFolderIfEmpty(dir.getParentFile(), depth + 1); } }