Skip to content

Commit

Permalink
Switched to System.Logger and renameTo replaced by Files.move
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Dec 4, 2023
1 parent ce998b9 commit d641584
Showing 1 changed file with 22 additions and 24 deletions.
Expand Up @@ -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.
Expand All @@ -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";
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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());
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit d641584

Please sign in to comment.