-
Notifications
You must be signed in to change notification settings - Fork 2
feat: save app and ldk logs to same file #713
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
base: master
Are you sure you want to change the base?
Changes from all commits
cf34be2
13b3376
778ca10
15d4091
05314d4
12b219f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,9 +34,18 @@ enum class LogLevel { PERF, VERBOSE, GOSSIP, TRACE, DEBUG, INFO, WARN, ERROR; } | |
|
|
||
| val Logger = AppLogger() | ||
|
|
||
| class AppLogger(private val source: LogSource = LogSource.Bitkit) { | ||
| class AppLogger { | ||
| companion object { | ||
| private const val TAG = "Logger" | ||
| private var saver: LogSaver? = null | ||
|
|
||
| fun getOrCreateSaver(): LogSaver { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this method is called by two classes on init(). could have a race condittion
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but as far as I can see, it would just replace the instance. WDYT? |
||
| if (saver == null) { | ||
| val sessionPath = runCatching { buildSessionLogFilePath() }.getOrElse { "" } | ||
| saver = LogSaverImpl(sessionPath) | ||
| } | ||
| return requireNotNull(saver) { "saver should be initialized" } | ||
| } | ||
| } | ||
|
|
||
| private var delegate: LoggerImpl? = null | ||
|
|
@@ -46,13 +55,13 @@ class AppLogger(private val source: LogSource = LogSource.Bitkit) { | |
| } | ||
|
|
||
| private fun createDelegate(): LoggerImpl { | ||
| val sessionPath = runCatching { buildSessionLogFilePath(source) }.getOrElse { "" } | ||
| return LoggerImpl(APP, LogSaverImpl(source, sessionPath)) | ||
| return LoggerImpl(APP, getOrCreateSaver()) | ||
| } | ||
|
|
||
| fun reset() { | ||
| warn("Wiping entire logs directory…", context = TAG) | ||
| runCatching { Env.logDir.deleteRecursively() } | ||
| saver = null | ||
| delegate = runCatching { createDelegate() }.getOrNull() | ||
| } | ||
|
Comment on lines
61
to
66
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on app wipe, |
||
|
|
||
|
|
@@ -114,7 +123,7 @@ class AppLogger(private val source: LogSource = LogSource.Bitkit) { | |
| } | ||
| } | ||
|
|
||
| class LoggerImpl( | ||
| private class LoggerImpl( | ||
| private val tag: String = APP, | ||
| private val saver: LogSaver, | ||
| private val compact: Boolean = COMPACT, | ||
|
|
@@ -198,7 +207,6 @@ interface LogSaver { | |
| } | ||
|
|
||
| class LogSaverImpl( | ||
| source: LogSource, | ||
| private val sessionFilePath: String, | ||
| ) : LogSaver { | ||
| private val queue: CoroutineScope by lazy { | ||
|
|
@@ -207,7 +215,7 @@ class LogSaverImpl( | |
|
|
||
| init { | ||
| if (sessionFilePath.isNotEmpty()) { | ||
| log("Log session for '${source.name}' initialized with file path: '$sessionFilePath'") | ||
| log("Log session initialized with file path: '$sessionFilePath'") | ||
|
|
||
| // Clean all old log files in background | ||
| CoroutineScope(Dispatchers.IO).launch { | ||
|
|
@@ -221,8 +229,9 @@ class LogSaverImpl( | |
|
|
||
| queue.launch { | ||
| runCatching { | ||
| val sanitized = message.replace("\n", " ") | ||
| FileOutputStream(File(sessionFilePath), true).use { stream -> | ||
| stream.write("$message\n".toByteArray()) | ||
| stream.write("$sanitized\n".toByteArray()) | ||
| } | ||
| }.onFailure { | ||
| Log.e(APP, "Error writing to log file: '$sessionFilePath'", it) | ||
|
|
@@ -273,8 +282,7 @@ class LogSaverImpl( | |
|
|
||
| class LdkLogWriter( | ||
| private val maxLogLevel: LdkLogLevel = Env.ldkLogLevel, | ||
| private val source: LogSource = LogSource.Ldk, | ||
| saver: LogSaver = LogSaverImpl(source, buildSessionLogFilePath(source)), | ||
| saver: LogSaver = AppLogger.getOrCreateSaver(), | ||
| ) : LogWriter { | ||
| private val delegate: LoggerImpl = LoggerImpl(LDK, saver) | ||
|
|
||
|
|
@@ -296,11 +304,10 @@ class LdkLogWriter( | |
| } | ||
| } | ||
|
|
||
| private fun buildSessionLogFilePath(source: LogSource): String { | ||
| private fun buildSessionLogFilePath(): String { | ||
| val logDir = Env.logDir | ||
| val sourceName = source.name.lowercase() | ||
| val timestamp = utcDateFormatterOf(DatePattern.LOG_FILE).format(Date()) | ||
| val path = logDir.resolve("${sourceName}_$timestamp.log").path | ||
| val path = logDir.resolve("bitkit_$timestamp.log").path | ||
| return path | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could make
LoggerImpl