Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 161
Add configurable logging system#407
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
Merged
tnull
merged 5 commits into
lightningdevkit:main
from
enigbe:2024-11-configurable-logging-systemJan 30, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a88dc5c
feat(logger): implement configurable log writer with filesystem support
enigbe 6d8c65e
feat(logger): add & impl LogWriter for Writer
enigbe fe0f547
feat(facade): forward logs to `log` facade
enigbe 63f08b2
feat(custom): forward logs to a custom logger
enigbe e509cf8
refactor: optimize logging system
enigbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 5 additions & 10 deletions
15 ...ldk-node-android/lib/src/androidTest/kotlin/org/lightningdevkit/ldknode/AndroidLibTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,19 +3,16 @@ | ||
| */ | ||
| package org.lightningdevkit.ldknode | ||
| import kotlin.UInt | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import kotlin.io.path.createTempDirectory | ||
| import kotlin.test.Test | ||
| import org.junit.runner.RunWith | ||
| import org.lightningdevkit.ldknode.*; | ||
| import android.content.Context.MODE_PRIVATE | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import org.lightningdevkit.ldknode.* | ||
| @RunWith(AndroidJUnit4::class) | ||
| class AndroidLibTest { | ||
| @Test fun node_start_stop() { | ||
| @Test | ||
| fun node_start_stop() { | ||
| val tmpDir1 = createTempDirectory("ldk_node").toString() | ||
| println("Random dir 1: $tmpDir1") | ||
| val tmpDir2 = createTempDirectory("ldk_node").toString() | ||
| @@ -28,13 +25,11 @@ class AndroidLibTest { | ||
| config1.storageDirPath = tmpDir1 | ||
| config1.listeningAddresses = listOf(listenAddress1) | ||
| config1.network = Network.REGTEST | ||
| config1.logLevel = LogLevel.TRACE | ||
tnull marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| val config2 = defaultConfig() | ||
| config2.storageDirPath = tmpDir2 | ||
| config2.listeningAddresses = listOf(listenAddress2) | ||
| config2.network = Network.REGTEST | ||
| config2.logLevel = LogLevel.TRACE | ||
| val builder1 = Builder.fromConfig(config1) | ||
| val builder2 = Builder.fromConfig(config2) | ||
66 changes: 64 additions & 2 deletions
66 bindings/kotlin/ldk-node-jvm/lib/src/test/kotlin/org/lightningdevkit/ldknode/LibraryTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -9,6 +9,7 @@ import java.net.http.HttpRequest | ||
| import java.net.http.HttpResponse | ||
| import kotlin.io.path.createTempDirectory | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertTrue | ||
| fun runCommandAndWait(vararg cmd: String): String { | ||
| println("Running command \"${cmd.joinToString(" ")}\"") | ||
| @@ -92,6 +93,60 @@ fun waitForBlock(esploraEndpoint: String, blockHash: String) { | ||
| } | ||
| } | ||
| class CustomLogWriter(private var currentLogLevel: LogLevel = LogLevel.INFO) : | ||
| LogWriter { | ||
| enum class LogLevel { | ||
| ERROR, WARN, INFO, DEBUG, TRACE, GOSSIP | ||
| } | ||
| private val logMessages = mutableListOf<String>() | ||
| fun setLogLevel(level: LogLevel) { | ||
| currentLogLevel = level | ||
| } | ||
| fun getLogMessages(): List<String> { | ||
| return logMessages.toList() | ||
| } | ||
| override fun log(record: LogRecord) { | ||
| val recordLevel = | ||
| when (record.level.toString().lowercase()) { | ||
| "error" -> LogLevel.ERROR | ||
| "warn" -> LogLevel.WARN | ||
| "info" -> LogLevel.INFO | ||
| "debug" -> LogLevel.DEBUG | ||
| "trace" -> LogLevel.TRACE | ||
| "gossip" -> LogLevel.GOSSIP | ||
| else -> LogLevel.INFO | ||
| } | ||
| if (isLevelEnabled(recordLevel)) { | ||
| val logMessage = formatRecord(record) | ||
| logMessages.add(logMessage) | ||
tnull marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| println("$logMessage") | ||
| } | ||
| } | ||
| private fun formatRecord(record: LogRecord): String { | ||
| val timestamp = | ||
| java.time.LocalDateTime.now() | ||
| .format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) | ||
| return String.format( | ||
| "%s %-6s [%s:%s] %s\n", | ||
| timestamp, | ||
| record.level, | ||
| record.modulePath, | ||
| record.line, | ||
| record.args | ||
| ) | ||
| } | ||
| private fun isLevelEnabled(level: LogLevel): Boolean { | ||
| return level.ordinal <= currentLogLevel.ordinal | ||
| } | ||
| } | ||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| class LibraryTest { | ||
| @@ -106,6 +161,9 @@ class LibraryTest { | ||
| } | ||
| @Test fun fullCycle() { | ||
| val logWriter1 = CustomLogWriter(CustomLogWriter.LogLevel.GOSSIP) | ||
| val logWriter2 = CustomLogWriter(CustomLogWriter.LogLevel.GOSSIP) | ||
| val tmpDir1 = createTempDirectory("ldk_node").toString() | ||
| println("Random dir 1: $tmpDir1") | ||
| val tmpDir2 = createTempDirectory("ldk_node").toString() | ||
| @@ -118,21 +176,22 @@ class LibraryTest { | ||
| config1.storageDirPath = tmpDir1 | ||
| config1.listeningAddresses = listOf(listenAddress1) | ||
| config1.network = Network.REGTEST | ||
| config1.logLevel = LogLevel.TRACE | ||
| println("Config 1: $config1") | ||
| val config2 = defaultConfig() | ||
| config2.storageDirPath = tmpDir2 | ||
| config2.listeningAddresses = listOf(listenAddress2) | ||
| config2.network = Network.REGTEST | ||
| config2.logLevel = LogLevel.TRACE | ||
| println("Config 2: $config2") | ||
| val builder1 = Builder.fromConfig(config1) | ||
| builder1.setChainSourceEsplora(esploraEndpoint, null) | ||
| builder1.setCustomLogger(logWriter1) | ||
| val builder2 = Builder.fromConfig(config2) | ||
| builder2.setChainSourceEsplora(esploraEndpoint, null) | ||
| builder2.setCustomLogger(logWriter2) | ||
| val node1 = builder1.build() | ||
| val node2 = builder2.build() | ||
| @@ -265,6 +324,9 @@ class LibraryTest { | ||
| assert(spendableBalance1AfterClose < 100000u) | ||
| assertEquals(102500uL, spendableBalance2AfterClose) | ||
| assertTrue(logWriter1.getLogMessages().isNotEmpty()) | ||
| assertTrue(logWriter2.getLogMessages().isNotEmpty()) | ||
| node1.stop() | ||
| node2.stop() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.