-
Notifications
You must be signed in to change notification settings - Fork 304
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -696,7 +696,7 @@ public void log() throws IOException { | |
} | ||
|
||
} | ||
|
||
|
||
|
||
/* | ||
|
@@ -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 ) { | ||
|
@@ -1044,6 +1050,63 @@ private synchronized void open(String dateStamp, | |
} | ||
|
||
} | ||
|
||
/** | ||
* Cleanup the history logC 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) { | ||
return; | ||
|
||
} | ||
|
||
synchronized (lock) { | ||
File fileDirectory = logFile.getParentFile(); | ||
String logFileName = logFile.getName(); | ||
|
||
if (fileDirectory == null) { | ||
return; | ||
} | ||
|
||
File[] allFilesInDirectory = fileDirectory.listFiles(); | ||
ArrayList<String> candidateListOfLogFiles = new ArrayList<>(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use |
||
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()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you could then keep the |
||
} | ||
} | ||
|
||
if (candidateListOfLogFiles.size() <= maxHistoryFiles) { | ||
return; | ||
} | ||
|
||
Object[] pathes = candidateListOfLogFiles.toArray(); | ||
Arrays.sort(pathes); | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you probably rather should use |
||
for (int i = 0; i < pathes.length - maxHistoryFiles; i++) { | ||
File logFile = new File((String) pathes[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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 <=
There was a problem hiding this comment.
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.