From d641584f883ec333bd109e36af46026e5f65a67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mat=C4=9Bj=C4=8Dek?= Date: Mon, 4 Dec 2023 13:28:34 +0100 Subject: [PATCH] Switched to System.Logger and renameTo replaced by Files.move MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: David Matějček --- .../main/jul/rotation/LogFileManager.java | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/rotation/LogFileManager.java b/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/rotation/LogFileManager.java index 371f4d1b7e6..c14c44ee60b 100644 --- a/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/rotation/LogFileManager.java +++ b/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/rotation/LogFileManager.java @@ -23,20 +23,22 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.lang.System.Logger; +import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Comparator; -import java.util.logging.Logger; import java.util.logging.StreamHandler; import java.util.zip.GZIPOutputStream; import org.glassfish.main.jul.tracing.GlassFishLoggingTracer; -import static java.util.logging.Level.FINE; -import static java.util.logging.Level.SEVERE; +import static java.lang.System.Logger.Level.DEBUG; +import static java.lang.System.Logger.Level.ERROR; + /** * Manages the logging file, it's rotations, packing of rolled log file, etc. @@ -51,7 +53,7 @@ * @author David Matejcek */ public class LogFileManager { - private static final Logger LOG = Logger.getLogger(LogFileManager.class.getName()); + private static final Logger LOG = System.getLogger(LogFileManager.class.getName()); private static final DateTimeFormatter SUFFIX_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH-mm-ss"); private static final String GZIP_EXTENSION = ".gz"; @@ -132,7 +134,7 @@ public void rollIfFileNotEmpty() { * again. */ public synchronized void roll() { - LOG.log(FINE, "roll(); {0}", this.logFile); + LOG.log(DEBUG, "roll(); {0}", this.logFile); final boolean wasOutputEnabled = isOutputEnabled(); disableOutput(); final File archivedFile = rollToNewFile(); @@ -223,10 +225,10 @@ private boolean isRollFileSizeLimitReached() { private File rollToNewFile() { try { if (this.logFile.createNewFile()) { - LOG.log(FINE, "Created new log file: {0}", this.logFile); + LOG.log(DEBUG, "Created new log file: {0}", this.logFile); return null; } - LOG.log(FINE, "Rolling log file: {0}", this.logFile); + LOG.log(DEBUG, "Rolling log file: {0}", this.logFile); final File archivedLogFile = prepareAchivedLogFileTarget(); moveFile(logFile, archivedLogFile); forceOSFilesync(logFile); @@ -268,22 +270,18 @@ private void forceOSFilesync(final File file) throws IOException { private void moveFile(final File logFileToArchive, final File target) throws IOException { - LOG.log(FINE, () -> String.format("moveFile(logFileToArchive=%s, target=%s)", logFileToArchive, target)); - final boolean renameSuccess = logFileToArchive.renameTo(target); - if (!renameSuccess) { - logError(String.format( - "File %s could not be renamed to %s trying to copy and delete it with NIO.", - logFileToArchive, target)); + LOG.log(DEBUG, "moveFile(logFileToArchive={0}, target={1})", logFileToArchive, target); + try { + Files.move(logFileToArchive.toPath(), target.toPath(), StandardCopyOption.ATOMIC_MOVE); + } catch (UnsupportedOperationException | AtomicMoveNotSupportedException e) { // If we don't succeed with file rename which most likely can happen on // Windows because of multiple file handles opened. We go through Plan B to // copy bytes explicitly to a renamed file. - try { - Files.copy(logFileToArchive.toPath(), target.toPath(), StandardCopyOption.ATOMIC_MOVE); - } catch (UnsupportedOperationException e) { - // Can happen on some windows file systems - then we try non-atomic version at least. - Files.copy(logFileToArchive.toPath(), target.toPath()); - logFileToArchive.delete(); - } + // Can happen on some windows file systems - then we try non-atomic version at least. + logError(String.format( + "File %s could not be renamed to %s atomically, now trying to move it without this request.", + logFileToArchive, target)); + Files.move(logFileToArchive.toPath(), target.toPath()); } } @@ -303,8 +301,8 @@ private void compressFile(final File rotatedFile) { final boolean compressed = gzipFile(rotatedFile, outFile); if (compressed) { final long time = System.currentTimeMillis() - start; - LOG.log(FINE, "File {0} of size {1} has been archived to file {2} of size {3} in {4} ms", - new Object[] {rotatedFile, rotatedFile.length(), outFile, outFile.length(), time}); + LOG.log(DEBUG, "File {0} of size {1} has been archived to file {2} of size {3} in {4} ms", + rotatedFile, rotatedFile.length(), outFile, outFile.length(), time); final boolean deleted = rotatedFile.delete(); if (!deleted) { logError("Could not delete uncompressed log file: " + rotatedFile.getAbsolutePath()); @@ -367,13 +365,13 @@ private boolean gzipFile(final File inputFile, final File outputFile) { private void logError(final String message) { GlassFishLoggingTracer.error(getClass(), message); - LOG.log(SEVERE, message); + LOG.log(ERROR, message); } private void logError(final String message, final Throwable t) { GlassFishLoggingTracer.error(getClass(), message, t); - LOG.log(SEVERE, message, t); + LOG.log(ERROR, message, t); } /**