Skip to content

Commit

Permalink
Merge pull request #37 from Qase/fix/MAPP-3856
Browse files Browse the repository at this point in the history
fix: MAPP-3856 write queue to file before writing a throwable
  • Loading branch information
neubami94 authored Jul 15, 2024
2 parents 112b241 + b8fc56f commit 5508bfa
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion kotlinlog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdk 21
targetSdk 34
versionCode 8
versionName "2.2.14"
versionName "2.2.16"
buildConfigField 'int', 'VERSION_CODE', "$versionCode"
buildConfigField 'String', 'VERSION_NAME', "\"$versionName\""
consumerProguardFile('proguard-rules.pro')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ private fun emptyCheck(): Boolean {
/**
* @return Returns method name by reflexion
*/
@Suppress("UNREACHABLE_CODE")
private fun getMethodStackTraceElement(): StackTraceElement {
val ste = Thread.currentThread().stackTrace
var found = false
Expand Down
31 changes: 16 additions & 15 deletions kotlinlog/src/main/kotlin/quanti/com/kotlinlog/file/FileLogger.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package quanti.com.kotlinlog.file

import android.content.Context
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -71,10 +72,11 @@ class FileLogger(

override fun logSync(androidLogLevel: Int, tag: String, methodName: String, text: String) {
val formattedString = getFormatedString(appName, androidLogLevel, tag, methodName, text)
logFile.write(formattedString)
blockingQueue.add(formattedString)
forceWrite()
}


@OptIn(DelicateCoroutinesApi::class)
override fun logThrowable(androidLogLevel: Int, tag: String, methodName: String, text: String, t: Throwable) {

val formattedString = getFormatedString(appName, androidLogLevel, tag, methodName, text)
Expand All @@ -98,8 +100,9 @@ class FileLogger(
}
}

//we want errors to be sync
logFile.write(finalText)
blockingQueue.add(finalText)
// write errors and queued logs immediately
forceWrite()
}

override fun cleanResources() {
Expand Down Expand Up @@ -137,22 +140,20 @@ class FileLogger(
* Deletes all stored logs in app local memory
*/
fun deleteLogsForIdentifier(appCtx: Context, logIdentifier: String) {
appCtx
.logFilesDir
.listFiles()
.filter { it.name.contains(logIdentifier) }
.filter { it.name.endsWith(".log") }
.forEach { it.delete() }
appCtx.logFilesDir
.listFiles()
?.filter { it.name.contains(logIdentifier) }
?.filter { it.name.endsWith(".log") }
?.forEach { it.delete() }
}
/**
* Deletes all stored logs in app local memory
*/
fun deleteAllLogs(appCtx: Context) {
appCtx
.logFilesDir
.listFiles()
.filter { it.name.endsWith(".log") }
.forEach { it.delete() }
appCtx.logFilesDir
.listFiles()
?.filter { it.name.endsWith(".log") }
?.forEach { it.delete() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ abstract class AbstractLogFile(
createNewFile()
}
while (queue.isNotEmpty()) {
fos.write(queue.poll().toByteArray())
fos.write(queue.poll()?.toByteArray())
}
}
}
Expand Down Expand Up @@ -94,7 +94,7 @@ abstract class AbstractLogFile(
/**
* Returns all files that corresponds to this logger
*/
protected fun listOfLoggerFiles() = ctx.logFilesDir.listFiles().filter { it.name.contains(logIdentifier) }
protected fun listOfLoggerFiles() = ctx.logFilesDir.listFiles()?.filter { it.name.contains(logIdentifier) }


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class CircleLogFile(
}

//remove all zips
listOfLoggerFiles().deleteAllZips()
listOfLoggerFiles()?.deleteAllZips()

//remove all files that exceeds specified limit
listOfLoggerFiles()
.sortByAge()
.drop(bundle.numOfFiles)
.deleteAll()
?.sortByAge()
?.drop(bundle.numOfFiles)
?.deleteAll()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ class DayLogFile(
//switch to new file if needed
loga("fileAge: " + file.fileAge())
loga("fileName: " + file.name)
if (file.fileAge() > 0 || !file.name!!.contains(getFormattedFileNameForDayTemp())) {
if (file.fileAge() > 0 || !file.name.contains(getFormattedFileNameForDayTemp())) {
createNewFile()
}

//remove all zips
listOfLoggerFiles().deleteAllZips()
listOfLoggerFiles()?.deleteAllZips()

//remove all files older than x days
loga("max days saved: $maxDays")
listOfLoggerFiles().deleteAllOldFiles(maxDays)
listOfLoggerFiles()?.deleteAllOldFiles(maxDays)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class StrictCircleLogFile(
override fun writeBatch(queue: LinkedBlockingQueue<String>) {
lock.withLock {
while (queue.isNotEmpty()) {
writeWithCheck(queue.poll())
queue.poll()?.let { writeWithCheck(it) }
}
}
}
Expand All @@ -64,13 +64,13 @@ class StrictCircleLogFile(
override fun cleanFolder() {

//remove all zips
listOfLoggerFiles().deleteAllZips()
listOfLoggerFiles()?.deleteAllZips()

//remove all files that exceeds specified limit
listOfLoggerFiles()
.sortByAge()
.drop(bundle.numOfFiles)
.deleteAll()
?.sortByAge()
?.drop(bundle.numOfFiles)
?.deleteAll()
}

}

0 comments on commit 5508bfa

Please sign in to comment.