Skip to content

Commit

Permalink
Delete old logs from database after specific number of days
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixam97 committed Jul 21, 2024
1 parent 6ad0a99 commit 4d644df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ interface LogDao {

@Query("DELETE FROM LogEntries")
fun clear()

@Query("DELETE FROM LogEntries WHERE epochTime < :timeLimit")
fun trim(timeLimit: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var AppPreferences.logLength: Int get() = LogLength.value; set(value) {LogLength

object InAppLogger {

// number of days to keep logs, delete after
private const val deleteDays: Int = 28

private val _realTimeLog = MutableStateFlow<LogEntry?>(null)
val realTimeLog = _realTimeLog.asStateFlow()

Expand Down Expand Up @@ -61,6 +64,11 @@ object InAppLogger {
message = message
)
logDao.insert(newLogEntry)

// Delete old log entries older than 'deleteDays'
val trimTime = System.currentTimeMillis() - (86_400_000L * deleteDays)
logDao.trim(trimTime)

_realTimeLog.value = newLogEntry// "${SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS").format(newLogEntry.epochTime)} ${typeSymbol(newLogEntry.type)}: ${newLogEntry.message}"
} catch (e: java.lang.Exception) {
e.printStackTrace()
Expand Down

0 comments on commit 4d644df

Please sign in to comment.