Skip to content

Commit

Permalink
fix: implement logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Oct 19, 2024
1 parent 2d638d2 commit a600c51
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions modules/logger/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(libs.kermit.logging)
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
package com.sphereon.oid.fed.common.logging
package com.sphereon.oid.fed.logger

import co.touchlab.kermit.Logger
import co.touchlab.kermit.*
import co.touchlab.kermit.Logger as KermitLogger
import kotlinx.datetime.Clock

object Logger {
object TimestampFormatter : MessageStringFormatter {
override fun formatMessage(severity: Severity?, tag: Tag?, message: Message): String {
val sb = StringBuilder()
val now = Clock.System.now()
sb.append("[${severity.toString().uppercase()}] ")
sb.append(now.toString())
sb.append(" ")
sb.append(super.formatMessage(null, tag, message))
return sb.toString()
}
}

class Logger(private val tag: String = "") {
fun verbose(message: String, tag: String = this.tag) {
KermitLogger.v(tag = tag, messageString = message)
}

fun verbose(tag: String, message: String) {
Logger.v(tag = tag, messageString = message)
fun debug(message: String, tag: String = this.tag) {
KermitLogger.d(tag = tag, messageString = message)
}

fun debug(tag: String, message: String) {
Logger.d(tag = tag, messageString = message)
fun info(message: String, tag: String = this.tag) {
KermitLogger.i(tag = tag, messageString = message)
}

fun info(tag: String, message: String) {
Logger.i(tag = tag, messageString = message)
fun warn(message: String, tag: String = this.tag) {
KermitLogger.w(tag = tag, messageString = message)
}

fun warn(tag: String, message: String) {
Logger.w(tag = tag, messageString = message)
fun error(message: String, throwable: Throwable? = null, tag: String = this.tag) {
KermitLogger.e(tag = tag, messageString = message, throwable = throwable)
}

fun error(tag: String, message: String, throwable: Throwable? = null) {
Logger.e(tag = tag, messageString = message, throwable = throwable)
fun setMinSeverity(severity: Severity) = KermitLogger.setMinSeverity(severity)
fun setLogWriters() = KermitLogger.setLogWriters(platformLogWriter(TimestampFormatter))

object Static {
fun tag(tag: String = "", severity: Severity = Severity.Info) =
Logger(tag).also { it.setMinSeverity(severity) }.also { it.setLogWriters() }
}
}

val DefaultLogger = Logger.Static.tag("")
1 change: 1 addition & 0 deletions modules/openid-federation-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ kotlin {
val commonMain by getting {
dependencies {
api(projects.modules.openapi)
api(projects.modules.logger)
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-logging:$ktorVersion")
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sphereon.oid.fed.client

import com.sphereon.oid.fed.logger.Logger

object ClientConstants {
private const val LOG_NAMESPACE = "sphereon:oid:fed:client"
val LOG = Logger.Static.tag(LOG_NAMESPACE)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sphereon.oid.fed.client.trustchain

import com.sphereon.oid.fed.client.ClientConstants
import com.sphereon.oid.fed.client.crypto.ICryptoCallbackService
import com.sphereon.oid.fed.client.crypto.findKeyInJwks
import com.sphereon.oid.fed.client.fetch.IFetchCallbackService
Expand Down Expand Up @@ -33,9 +34,10 @@ class TrustChain(private val fetchService: IFetchCallbackService, private val cr
val cache = SimpleCache<String, String>()
val chain: MutableList<String> = arrayListOf()
return try {
ClientConstants.LOG.debug("Resolving trust chain for entity $entityIdentifier and trust anchors $trustAnchors")
buildTrustChainRecursive(entityIdentifier, trustAnchors, chain, cache)
} catch (_: Exception) {
// Log error
} catch (e: Exception) {
ClientConstants.LOG.debug("Failed to resolve trust chain for entity $entityIdentifier and trust anchors $trustAnchors, error: $e")
null
}
}
Expand Down Expand Up @@ -176,7 +178,8 @@ class TrustChain(private val fetchService: IFetchCallbackService, private val cr
if (result != null) return result
chain.removeLast()
}
} catch (_: Exception) {
} catch (e: Exception) {
ClientConstants.LOG.debug("Failed to process authority $authority, error: $e")
return null
}

Expand Down

0 comments on commit a600c51

Please sign in to comment.