Add debug logging

This commit is contained in:
Erik C. Thauvin 2025-03-26 00:35:28 -07:00
parent 926c20e8b2
commit 686174b9cb
Signed by: erik
GPG key ID: 776702A6A2DA330E
3 changed files with 41 additions and 3 deletions

View file

@ -42,6 +42,8 @@ import java.math.BigDecimal
import java.text.NumberFormat
import java.time.LocalDate
import java.util.*
import java.util.logging.Level
import java.util.logging.Logger
/**
* Retrieves and holds a cryptocurrency price.
@ -57,6 +59,9 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe
// Coinbase API URL
private const val COINBASE_API_URL = "https://api.coinbase.com/v2/"
/** The logger instance. **/
val logger: Logger by lazy { Logger.getLogger(CryptoPrice::class.java.simpleName) }
/**
* Converts JSON data object to [CryptoPrice].
*
@ -105,6 +110,9 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe
id = "empty_response",
message = "Empty response."
)
if (logger.isLoggable(Level.FINE)) {
logger.fine(body)
}
try {
val json = JSONObject(body)
if (response.isSuccessful) {

View file

@ -42,9 +42,12 @@ import net.thauvin.erik.crypto.CryptoPrice.Companion.sellPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.toPrice
import org.json.JSONObject
import org.junit.jupiter.api.BeforeAll
import java.math.BigDecimal
import java.time.LocalDate
import java.util.*
import java.util.logging.ConsoleHandler
import java.util.logging.Level
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@ -239,4 +242,16 @@ class CryptoPriceTest {
assertEquals(json, price.toString(), "toString()")
assertEquals(price.toString(), price.toJson(""), "toString() = toJson('')")
}
companion object {
@JvmStatic
@BeforeAll
fun beforeAll() {
with(CryptoPrice.logger) {
addHandler(ConsoleHandler().apply { level = Level.FINE })
level = Level.FINE
useParentHandlers = false
}
}
}
}