Skip to content

Add size based file rotation strategy #2

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
39 changes: 26 additions & 13 deletions filelogger/src/main/java/abbasi/android/filelogger/FileLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package abbasi.android.filelogger

import abbasi.android.filelogger.config.Config
import abbasi.android.filelogger.config.FileRotationStrategy
import abbasi.android.filelogger.config.RetentionPolicy
import abbasi.android.filelogger.file.FileWriter
import abbasi.android.filelogger.file.LogFileManager
import abbasi.android.filelogger.file.LogLevel
Expand Down Expand Up @@ -143,20 +144,32 @@ object FileLogger {

private fun openedFileWriter(): FileWriter {
val strategy = config?.fileRotationStrategy

if (strategy == null || strategy !is FileRotationStrategy.TimeBased) {
return fileWriter
}

if (System.currentTimeMillis() - logFileManager.lastCreationTime > strategy.intervalInMillis) {
fileWriter.close()
fileWriter = FileWriter(
dateFormat = dateFormat,
logFile = logFileManager.currentLogFile(),
startLogs = config?.startupData,
)
when (strategy) {
null -> {
}
is FileRotationStrategy.None -> {
}
is FileRotationStrategy.TimeBased -> {
if (System.currentTimeMillis() - logFileManager.lastCreationTime > strategy.intervalInMillis) {
fileWriter.close()
fileWriter = FileWriter(
dateFormat = dateFormat,
logFile = logFileManager.currentLogFile(),
startLogs = config?.startupData,
)
}
}
is FileRotationStrategy.SizeBased -> {
if (fileWriter.totalBytes > strategy.bytes) {
fileWriter.close()
fileWriter = FileWriter(
dateFormat = dateFormat,
logFile = logFileManager.currentLogFile(),
startLogs = config?.startupData,
)
}
}
}

return fileWriter
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package abbasi.android.filelogger.config
sealed interface FileRotationStrategy {
object None : FileRotationStrategy
data class TimeBased(val intervalInMillis: Long) : FileRotationStrategy
data class SizeBased(val bytes: ULong) : FileRotationStrategy
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package abbasi.android.filelogger.file

import abbasi.android.filelogger.time.FastDateFormat
import android.util.Log
import java.io.DataOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStreamWriter
Expand All @@ -17,39 +18,47 @@ internal class FileWriter(
logFile: File,
startLogs: Map<String, String>?,
) {
private var streamWriter: OutputStreamWriter? = null
private var streamWriter: DataOutputStream? = null
public var totalBytes:ULong = 0u;

init {
try {
val stream = FileOutputStream(logFile)
streamWriter = OutputStreamWriter(stream).apply {
write("File logger started at ${dateFormat.format(System.currentTimeMillis())}\n")
startLogs?.forEach {
write("${it.key}: ${it.value}\n")
}
write("\n\n")
flush()
streamWriter = DataOutputStream(stream)
var message = "File logger started at ${dateFormat.format(System.currentTimeMillis())}\n"
startLogs?.forEach {
message += "${it.key}: ${it.value}\n"
}
message += "\n\n"
writeNoTag(message)
} catch (e: Exception) {
e.printStackTrace()
}
}

fun write(message: String) {
fun writeNoTag(message: String) {
streamWriter?.let { writer ->
try {
writer.write("${dateFormat.format(System.currentTimeMillis())} $message")
val bytes = message.toByteArray(Charsets.UTF_8)
totalBytes += bytes.count().toUInt()
writer.write(bytes)
writer.flush()
} catch (e: Exception) {
Log.e(javaClass.simpleName, "e:", e)
}
}
}

fun write(message: String) {
val formattedMessage = "${dateFormat.format(System.currentTimeMillis())} $message"
writeNoTag(formattedMessage)
}

fun close() {
try {
streamWriter?.close()
streamWriter = null
totalBytes = 0u
} catch (e: Exception) {
e.printStackTrace()
}
Expand Down