Skip to content
New issue

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

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

Already on GitHub? # to your account

Close file before rotating in RollingFileLogWriter #433

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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 @@ -104,11 +104,9 @@ open class RollingFileLogWriter(
private fun formatMessage(severity: Severity, tag: Tag?, message: Message): String =
messageStringFormatter.formatMessage(severity, if (config.logTag) tag else null, message)

private fun maybeRollLogs(size: Long): Boolean {
return if (size > config.rollOnSize) {
rollLogs()
true
} else false
private fun shouldRollLogs(logFilePath: Path): Boolean {
Copy link
Author

Choose a reason for hiding this comment

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

I changed the signature of this function because calling maybeRollLogs(fileSizeOrZero()) seemed cumbersome.

val size = fileSizeOrZero(logFilePath)
return size > config.rollOnSize
}

private fun rollLogs() {
Expand Down Expand Up @@ -136,8 +134,8 @@ open class RollingFileLogWriter(
private suspend fun writer() {
val logFilePath = pathForLogIndex(0)

if (fileSystem.exists(logFilePath)) {
maybeRollLogs(fileSizeOrZero(logFilePath))
if (fileSystem.exists(logFilePath) && shouldRollLogs(logFilePath)) {
Copy link
Author

Choose a reason for hiding this comment

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

AFAICT we should just do this:

Suggested change
if (fileSystem.exists(logFilePath) && shouldRollLogs(logFilePath)) {
if (shouldRollLogs(logFilePath)) {

because metadataOrNull() already handled the case where the file doesn't exist.

rollLogs()
}

fun createNewLogSink(): Sink = fileSystem
Expand All @@ -151,9 +149,9 @@ open class RollingFileLogWriter(
val result = loggingChannel.receiveCatching()

// check if logs need rolling
val rolled = maybeRollLogs(fileSizeOrZero(logFilePath))
if (rolled) {
if (shouldRollLogs(logFilePath)) {
currentLogSink.close()
rollLogs()
currentLogSink = createNewLogSink()
}

Expand Down
Loading