Reputation: 2316
I have a Dropwizard application using logback with SizeBased Rolling Policy enabled. However, after the current log file is rolled out, the process still keeps the FD links to the older file open. Hence, the disk usage explodes. Digging into the source code I came across this function, which is called at the time of Rollover.
* Sets and opens the file where the log output will go. The specified * file must be writable. * *
* If there was already an opened file, then the previous file is closed first. * *
* Do not use this method directly. To configure a FileAppender or one of * its subclasses, set its properties one by one and then call start().
- @param file_name
- The path to the log file.
public void openFile(String file_name) throws IOException {
lock.lock();
try {
File file = new File(file_name);
if (FileUtil.isParentDirectoryCreationRequired(file)) {
boolean result = FileUtil.createMissingParentDirectories(file);
if (!result) {
addError("Failed to create parent directories for ["
+ file.getAbsolutePath() + "]");
}
}
ResilientFileOutputStream resilientFos = new ResilientFileOutputStream(
file, append);
resilientFos.setContext(context);
setOutputStream(resilientFos);
} finally {
lock.unlock();
}
}
The documentation states that "If there was already an opened file, then the previous file is closed first"
Can anyone tell exactly which part of the above function deals with closing the file? And also, the possible solution to the problem.
Upvotes: 0
Views: 893
Reputation: 6456
if you dig deeper, your last call is:
setOutputStream(resilientFos);
The impl of that call looks like this:
/**
* <p>
* Sets the @link OutputStream} where the log output will go. The specified
* <code>OutputStream</code> must be opened by the user and be writable. The
* <code>OutputStream</code> will be closed when the appender instance is
* closed.
*
* @param outputStream
* An already opened OutputStream.
*/
public void setOutputStream(OutputStream outputStream) {
lock.lock();
try {
// close any previously opened output stream
closeOutputStream();
this.outputStream = outputStream;
if (encoder == null) {
addWarn("Encoder has not been set. Cannot invoke its init method.");
return;
}
encoderInit();
} finally {
lock.unlock();
}
}
closeOutputStream() will then close the previous stream.
Looking at the impl, the FileAppender has exactly one stream. It is closed whenever setOutputStream is called or whenever the appender is closed altogether. It appears your problem may be someplace else.
Hope that helps,
Artur
Upvotes: 1