Skip to content

Commit aa59103

Browse files
feat: merge app and ldk logs into unified file
- Both APP and LDK loggers now write to same file (bitkit_<timestamp>.log) - Logs are automatically in chronological order due to shared file - File writes serialized via single-threaded coroutine queue - APP/LDK tag prefixes distinguish log sources Co-authored-by: Ovi Trif <[email protected]>
1 parent e9bff4a commit aa59103

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

app/src/main/java/to/bitkit/utils/Logger.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ val Logger = AppLogger()
3737
class AppLogger(private val source: LogSource = LogSource.Bitkit) {
3838
companion object {
3939
private const val TAG = "Logger"
40+
private var sharedSaver: LogSaver? = null
41+
42+
fun getOrCreateSharedSaver(): LogSaver {
43+
if (sharedSaver == null) {
44+
val sessionPath = runCatching { buildSessionLogFilePath() }.getOrElse { "" }
45+
sharedSaver = LogSaverImpl(sessionPath)
46+
}
47+
return sharedSaver!!
48+
}
4049
}
4150

4251
private var delegate: LoggerImpl? = null
@@ -46,13 +55,13 @@ class AppLogger(private val source: LogSource = LogSource.Bitkit) {
4655
}
4756

4857
private fun createDelegate(): LoggerImpl {
49-
val sessionPath = runCatching { buildSessionLogFilePath(source) }.getOrElse { "" }
50-
return LoggerImpl(APP, LogSaverImpl(source, sessionPath))
58+
return LoggerImpl(APP, getOrCreateSharedSaver())
5159
}
5260

5361
fun reset() {
5462
warn("Wiping entire logs directory…", context = TAG)
5563
runCatching { Env.logDir.deleteRecursively() }
64+
sharedSaver = null
5665
delegate = runCatching { createDelegate() }.getOrNull()
5766
}
5867

@@ -198,7 +207,6 @@ interface LogSaver {
198207
}
199208

200209
class LogSaverImpl(
201-
source: LogSource,
202210
private val sessionFilePath: String,
203211
) : LogSaver {
204212
private val queue: CoroutineScope by lazy {
@@ -207,7 +215,7 @@ class LogSaverImpl(
207215

208216
init {
209217
if (sessionFilePath.isNotEmpty()) {
210-
log("Log session for '${source.name}' initialized with file path: '$sessionFilePath'")
218+
log("Unified log session initialized with file path: '$sessionFilePath'")
211219

212220
// Clean all old log files in background
213221
CoroutineScope(Dispatchers.IO).launch {
@@ -273,8 +281,7 @@ class LogSaverImpl(
273281

274282
class LdkLogWriter(
275283
private val maxLogLevel: LdkLogLevel = Env.ldkLogLevel,
276-
private val source: LogSource = LogSource.Ldk,
277-
saver: LogSaver = LogSaverImpl(source, buildSessionLogFilePath(source)),
284+
saver: LogSaver = AppLogger.getOrCreateSharedSaver(),
278285
) : LogWriter {
279286
private val delegate: LoggerImpl = LoggerImpl(LDK, saver)
280287

@@ -296,11 +303,10 @@ class LdkLogWriter(
296303
}
297304
}
298305

299-
private fun buildSessionLogFilePath(source: LogSource): String {
306+
private fun buildSessionLogFilePath(): String {
300307
val logDir = Env.logDir
301-
val sourceName = source.name.lowercase()
302308
val timestamp = utcDateFormatterOf(DatePattern.LOG_FILE).format(Date())
303-
val path = logDir.resolve("${sourceName}_$timestamp.log").path
309+
val path = logDir.resolve("bitkit_$timestamp.log").path
304310
return path
305311
}
306312

0 commit comments

Comments
 (0)