Skip to content

Commit

Permalink
Support method chaining in forage Logger
Browse files Browse the repository at this point in the history
method chaining can help reduce code verbosity so it's a nice quality of
life improvement that the pos repo already has

Signed-off-by: Devin Morgan <[email protected]>
  • Loading branch information
devinmorgan committed Jun 24, 2024
1 parent 4d9ed7f commit 259dfbc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import kotlin.random.Random

internal interface Log {
fun initializeDD(context: Context, config: ForageConfig)
fun d(msg: String, attributes: Map<String, Any?> = emptyMap())
fun i(msg: String, attributes: Map<String, Any?> = emptyMap())
fun w(msg: String, attributes: Map<String, Any?> = emptyMap())
fun e(msg: String, throwable: Throwable? = null, attributes: Map<String, Any?> = emptyMap())
fun d(msg: String, attributes: Map<String, Any?> = emptyMap()): Log
fun i(msg: String, attributes: Map<String, Any?> = emptyMap()): Log
fun w(msg: String, attributes: Map<String, Any?> = emptyMap()): Log
fun e(msg: String, throwable: Throwable? = null, attributes: Map<String, Any?> = emptyMap()): Log
fun getTraceIdValue(): String
fun addAttribute(key: String, value: Any?): Log

Expand Down Expand Up @@ -70,20 +70,24 @@ internal interface Log {
logger?.addAttribute(TRACE_ID, traceId)
}

override fun d(msg: String, attributes: Map<String, Any?>) {
logger?.d(msg, attributes = attributes)
override fun d(msg: String, attributes: Map<String, Any?>): Log {
logger!!.d(msg, attributes = attributes)
return this
}

override fun i(msg: String, attributes: Map<String, Any?>) {
logger?.i(msg, attributes = attributes)
override fun i(msg: String, attributes: Map<String, Any?>): Log {
logger!!.i(msg, attributes = attributes)
return this
}

override fun w(msg: String, attributes: Map<String, Any?>) {
logger?.w(msg, attributes = attributes)
override fun w(msg: String, attributes: Map<String, Any?>): Log {
logger!!.w(msg, attributes = attributes)
return this
}

override fun e(msg: String, throwable: Throwable?, attributes: Map<String, Any?>) {
logger?.e(msg, throwable, attributes)
override fun e(msg: String, throwable: Throwable?, attributes: Map<String, Any?>): Log {
logger!!.e(msg, throwable, attributes)
return this
}

override fun getTraceIdValue(): String {
Expand All @@ -102,13 +106,13 @@ internal interface Log {
private val SILENT = object : Log {
override fun initializeDD(context: Context, config: ForageConfig) {}

override fun d(msg: String, attributes: Map<String, Any?>) {}
override fun d(msg: String, attributes: Map<String, Any?>): Log = this

override fun i(msg: String, attributes: Map<String, Any?>) {}
override fun i(msg: String, attributes: Map<String, Any?>): Log = this

override fun w(msg: String, attributes: Map<String, Any?>) {}
override fun w(msg: String, attributes: Map<String, Any?>): Log = this

override fun e(msg: String, throwable: Throwable?, attributes: Map<String, Any?>) {}
override fun e(msg: String, throwable: Throwable?, attributes: Map<String, Any?>): Log = this

override fun getTraceIdValue(): String { return "" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class ForageSDK {
val logger = Log.getInstance()
.addAttribute("merchant_ref", merchantId)
.addAttribute("customer_id", customerId)
logger.i("[ForageSDK] Tokenizing Payment Method")
.i("[ForageSDK] Tokenizing Payment Method")

val tokenizeCardService = ServiceFactory(sessionToken, merchantId, logger)
.createTokenizeCardService()
Expand Down Expand Up @@ -206,7 +206,7 @@ class ForageSDK {
val logger = Log.getInstance()
.addAttribute("merchant_ref", merchantId)
.addAttribute("payment_method_ref", paymentMethodRef)
logger.i("[ForageSDK] Called checkBalance for Payment Method $paymentMethodRef")
.i("[ForageSDK] Called checkBalance for Payment Method $paymentMethodRef")

// This block is used for Metrics Tracking!
// ------------------------------------------------------
Expand Down Expand Up @@ -303,7 +303,7 @@ class ForageSDK {
val logger = Log.getInstance()
.addAttribute("merchant_ref", merchantId)
.addAttribute("payment_ref", paymentRef)
logger.i("[ForageSDK] Called capturePayment for Payment $paymentRef")
.i("[ForageSDK] Called capturePayment for Payment $paymentRef")

// This block is used for Metrics Tracking!
// ------------------------------------------------------
Expand Down Expand Up @@ -392,7 +392,7 @@ class ForageSDK {
val logger = Log.getInstance()
.addAttribute("merchant_ref", merchantId)
.addAttribute("payment_ref", paymentRef)
logger.i("[ForageSDK] Called deferPaymentCapture for Payment $paymentRef")
.i("[ForageSDK] Called deferPaymentCapture for Payment $paymentRef")

val deferPaymentCaptureService = ServiceFactory(sessionToken, merchantId, logger)
.createDeferPaymentCaptureRepository(foragePinEditText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,29 @@ internal class MockLogger : Log {
return
}

override fun d(msg: String, attributes: Map<String, Any?>) {
return
override fun d(msg: String, attributes: Map<String, Any?>): MockLogger {
return this
}

override fun i(msg: String, attributes: Map<String, Any?>) {
override fun i(msg: String, attributes: Map<String, Any?>): MockLogger {
infoLogs.add(LogEntry(msg, cumulativeAttributes.plus(attributes)))
return this
}

override fun w(msg: String, attributes: Map<String, Any?>) {
override fun w(msg: String, attributes: Map<String, Any?>): MockLogger {
warnLogs.add(LogEntry(msg, cumulativeAttributes.plus(attributes)))
return this
}

override fun e(msg: String, throwable: Throwable?, attributes: Map<String, Any?>) {
override fun e(msg: String, throwable: Throwable?, attributes: Map<String, Any?>): MockLogger {
errorLogs.add(
LogEntry(
msg,
cumulativeAttributes.plus(attributes),
error = throwable
)
)
return this
}

override fun addAttribute(key: String, value: Any?): Log {
Expand Down

0 comments on commit 259dfbc

Please sign in to comment.