Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYARA-2822 Access logs are not being purged #2824

Merged
merged 5 commits into from
Jun 15, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2017] [Payara Foundation]
// Portions Copyright [2016-2018] [Payara Foundation]

package com.sun.enterprise.web;

Expand Down Expand Up @@ -696,7 +696,7 @@ public void log() throws IOException {
}

}



/*
Expand Down Expand Up @@ -1030,6 +1030,12 @@ private synchronized void open(String dateStamp,
fos = new FileOutputStream(logFile, true);
fileChannel = fos.getChannel();

if (maxHistoryFiles > 0) {
synchronized (lock) {
cleanUpHistoryLogFiles();
}
}

} catch (IOException ioe) {
try {
if ( fileChannel != null ) {
Expand All @@ -1044,6 +1050,63 @@ private synchronized void open(String dateStamp,
}

}

/**
* Cleanup the history log file based on value set by <code>maxHistoryFiles</code>
* <p/>
* If it is not been specified without any value, then a default value of 10
* will be use.
* Else, if it is defined with value of 0, no history log file will be kept,
* and the current log file will be reset after each rotation.
* If undefined or value is less than 0, all history log are preserved.
*/
private void cleanUpHistoryLogFiles() {
if (maxHistoryFiles == 0 || maxHistoryFiles < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be written as <=

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svendiedrichsen thanks for the review.

return;

}

synchronized (lock) {
File fileDirectory = logFile.getParentFile();
String logFileName = logFile.getName();

if (fileDirectory == null) {
return;
}

File[] allFilesInDirectory = fileDirectory.listFiles();
ArrayList<String> candidateListOfLogFiles = new ArrayList<>();

Copy link
Contributor

@svendiedrichsen svendiedrichsen Jun 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use List<File>? You could spare some File-Instance creation later on.

if (allFilesInDirectory != null && allFilesInDirectory.length > 0) {
for (int i = 0; i < allFilesInDirectory.length; i++) {
if (!logFileName.equals(allFilesInDirectory[i].getName())
&& allFilesInDirectory[i].isFile()
&& allFilesInDirectory[i].getName().startsWith(prefix)) {
candidateListOfLogFiles.add(allFilesInDirectory[i].getAbsolutePath());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you could then keep the File instance.

}
}

if (candidateListOfLogFiles.size() <= maxHistoryFiles) {
return;
}

Object[] pathes = candidateListOfLogFiles.toArray();
Arrays.sort(pathes);
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you probably rather should use candidateListOfLogFiles.sort(<Comparator>) and provide a comparator which compares files by their absolute path.

for (int i = 0; i < pathes.length - maxHistoryFiles; i++) {
File logFile = new File((String) pathes[i]);
Copy link
Contributor

@svendiedrichsen svendiedrichsen Jun 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed anymore if you stayed with File and you could use a simpler for-loop syntax.

boolean delFile = logFile.delete();
if (!delFile) {
throw new IOException("Could not delete Access log file: "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems harsh to do. Wouldn't it be better to write a log message here and continue removing log files. Otherwise a single undeletable log will stop log file removal forever.

+ logFile.getAbsolutePath());
}
}
} catch (Exception ex) {
_logger.log(Level.INFO, "ERROR: COULD NOT DELETE LOG FILE." , ex);
}
}
}


// ------------------------------------------------------ Lifecycle Methods
Expand Down