Skip to content

Commit

Permalink
refactor: use Kermit for logging instead of slf4j
Browse files Browse the repository at this point in the history
slf4j is JVM only and blocks the lib from migrating to KMP.

This change migrates to use [Kermit](https://github.com/touchlab/Kermit), developed by touchlab. Kermit is a very simple logger implementation that supports many KMP targets.

Kermit is also very lightweight and has no additional dependencies:
```kotlin
\--- co.touchlab:kermit:2.0.4
     \--- co.touchlab:kermit-jvm:2.0.4
          +--- co.touchlab:kermit-core:2.0.4
          |    \--- co.touchlab:kermit-core-jvm:2.0.4
          |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.9.22 (*)
```

With slf4j (before, jvm only):
```
2024-06-25 15:36:41 [Test worker @kotlinx.coroutines.test runner#17] INFO  me.tb.cashuclient.Wallet - Wallet initialized with mint url https://testnut.cashu.space and unit 'sat'.
```

With Kermit (now, kmp):
```
Info: (me.tb.cashuclient.Wallet) Wallet initialized with mint url https://testnut.cashu.space and unit 'sat'.
````

Main difference is that `slf4j` by default prints out timestamp of the log along with a thread name, whereas kermit does not.

If needed, implementing a more custom log formatting is easy by providing a custom [LogWriter](https://kermit.touchlab.co/docs/details/LOG_WRITER/).

Work towards resolving issue #3.
  • Loading branch information
kirillzh authored and thunderbiscuit committed Jun 26, 2024
1 parent b1b8e84 commit afe963f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 38 deletions.
4 changes: 1 addition & 3 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ dependencies {
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.1")

// Logging
// TODO: The logging needs work.
implementation("org.slf4j:slf4j-api:1.7.30")
runtimeOnly("ch.qos.logback:logback-classic:1.4.12")
implementation("co.touchlab:kermit:2.0.4")

testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.1")
}
Expand Down
23 changes: 11 additions & 12 deletions lib/src/main/kotlin/me/tb/cashuclient/Wallet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import me.tb.cashuclient.types.PreRequestBundle
import me.tb.cashuclient.types.Proof
import me.tb.cashuclient.types.SpecificKeysetResponse
import me.tb.cashuclient.types.SwapRequired
import org.slf4j.LoggerFactory

public typealias NewAvailableDenominations = List<ULong>
public typealias MintInfo = InfoResponse
Expand All @@ -74,10 +73,10 @@ public class Wallet(
private val db: CashuDB = SQLiteDB()
) {
public val inactiveKeysets: MutableList<Keyset> = mutableListOf()
private val logger = LoggerFactory.getLogger(Wallet::class.java)
private val logger = co.touchlab.kermit.Logger.withTag(Wallet::class.java.name)

init {
logger.info("Wallet initialized with mint url $mintUrl and unit '$unit'.")
logger.i("Wallet initialized with mint url $mintUrl and unit '$unit'.")
}

// ---------------------------------------------------------------------------------------------
Expand All @@ -90,7 +89,7 @@ public class Wallet(
private fun addKeyset(keyset: Keyset) {
val currentActiveKeyset = this.activeKeyset
if (currentActiveKeyset != null) inactiveKeysets.add(currentActiveKeyset)
logger.info("Rotating keysets. Setting keyset ${keyset.keysetId.value} as the new active keyset.")
logger.i("Rotating keysets. Setting keyset ${keyset.keysetId.value} as the new active keyset.")

this.activeKeyset = keyset
}
Expand All @@ -100,7 +99,7 @@ public class Wallet(
* Query the mint for the active [Keyset] and set it as the active keyset.
*/
public suspend fun getActiveKeyset(): Unit = withContext(Dispatchers.IO) {
logger.info("Getting active keyset from mint.")
logger.i("Getting active keyset from mint.")

val client = createClient()
val response = client.get("$mintUrl$ACTIVE_KEYSET_ENDPOINT")
Expand All @@ -117,7 +116,7 @@ public class Wallet(
* Query the mint for the [Keyset] associated with a given [KeysetId].
*/
public suspend fun getSpecificKeyset(keysetId: KeysetId): Keyset = withContext(Dispatchers.IO) {
logger.info("Getting specific keyset from mint.")
logger.i("Getting specific keyset from mint.")

val keysetIdHex: String = keysetId.value
val client = createClient()
Expand Down Expand Up @@ -148,7 +147,7 @@ public class Wallet(
* @param amount The total value to mint.
*/
public suspend fun mint(amount: Satoshi): Unit = withContext(Dispatchers.IO) {
logger.info("Requesting a mint.")
logger.i("Requesting a mint.")

val client = createClient()
val scopedActiveKeyset = activeKeyset ?: throw Exception("The wallet must have an active keyset for the mint when attempting a mint operation.")
Expand Down Expand Up @@ -179,7 +178,7 @@ public class Wallet(
// might pay an invoice and then get wiped out before calling the mint again, but if it doesn't know the quote
// id then it will not be able to prove to the mint that it paid the invoice.
public suspend fun requestMintQuote(amount: Satoshi, paymentMethod: PaymentMethod): MintQuoteData = withContext(Dispatchers.IO) {
logger.info("Requesting a mint quote for $amount satoshis.")
logger.i("Requesting a mint quote for $amount satoshis.")

val client = createClient()
val mintQuoteRequest = MintQuoteRequest(amount.sat.toULong(), unit.toString())
Expand All @@ -192,7 +191,7 @@ public class Wallet(
client.close()

val mintQuoteResponse: MintQuoteResponse = response.body<MintQuoteResponse>()
logger.info("Quote response: $mintQuoteResponse")
logger.i("Quote response: $mintQuoteResponse")

MintQuoteData.fromMintQuoteResponse(amount, mintQuoteResponse)
}
Expand Down Expand Up @@ -283,7 +282,7 @@ public class Wallet(

// TODO: PaymentRequest.read() now returns a Try<PaymentRequest> so we need to handle the error case.
public suspend fun requestMeltQuote(pr: PaymentRequest): MeltQuoteResponse = withContext(Dispatchers.IO) {
logger.info("Requesting a melt quote.")
logger.i("Requesting a melt quote.")

val client = createClient()
val meltQuoteRequest = MeltQuoteRequest(pr, EcashUnit.SAT)
Expand Down Expand Up @@ -316,7 +315,7 @@ public class Wallet(
// ---------------------------------------------------------------------------------------------

private suspend fun swap(availableForSwap: List<ULong>, requiredAmount: ULong): NewAvailableDenominations = withContext(Dispatchers.IO) {
logger.info("Requesting a swap.")
logger.i("Requesting a swap.")

val client = createClient()
val scopedActiveKeyset = activeKeyset ?: throw Exception("The wallet must have an active keyset for the mint when attempting a swap operation.")
Expand Down Expand Up @@ -395,7 +394,7 @@ public class Wallet(
// ---------------------------------------------------------------------------------------------

public suspend fun getInfo(): MintInfo = withContext(Dispatchers.IO) {
logger.info("Getting info from mint.")
logger.i("Getting info from mint.")

val client = createClient()
val response = client.get("$mintUrl$INFO_ENDPOINT")
Expand Down
23 changes: 0 additions & 23 deletions lib/src/test/resources/logback.xml

This file was deleted.

0 comments on commit afe963f

Please sign in to comment.