Skip to content

Commit

Permalink
fix: use logging guards (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
anssari1 authored Jun 10, 2024
1 parent 305c19b commit 0db7362
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,24 @@ import com.expediagroup.sdk.core.constant.LoggingMessage.LOGGING_PREFIX
import org.slf4j.Logger

internal class ExpediaGroupLogger(private val logger: Logger, private val client: Client? = null) : Logger by logger {
override fun info(msg: String) = logger.info(decorate(msg))
override fun info(msg: String) {
if (logger.isInfoEnabled) {
logger.info(decorate(msg))
}
}

override fun warn(msg: String) = logger.warn(decorate(msg))
override fun warn(msg: String) {
if (logger.isWarnEnabled) {
logger.warn(decorate(msg))
}

override fun debug(msg: String) = logger.debug(decorate(msg))
}

override fun debug(msg: String) {
if (logger.isDebugEnabled) {
logger.debug(decorate(msg))
}
}

private fun decorate(msg: String): String = "$LOGGING_PREFIX ${mask(msg, getMaskedBodyFields())}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal data class LoggingConfiguration(
override val httpClientConfiguration: HttpClientConfig<out HttpClientEngineConfig>,
val maskedLoggingHeaders: Set<String>,
val maskedLoggingBodyFields: Set<String>,
val level: LogLevel = LogLevel.ALL,
val level: LogLevel = LogLevel.HEADERS,
val getLogger: (client: Client) -> Logger = createCustomLogger
) : KtorPluginConfiguration(httpClientConfiguration) {
companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class ExpediaGroupLoggerTest {
val mockedLogger: Logger = mockkClass(Logger::class)
every { mockedLogger.info(any()) }.answers { }
every { mockedLogger.warn(any()) }.answers { }
every { mockedLogger.isInfoEnabled }.answers { true }
every { mockedLogger.isWarnEnabled }.answers { true }
return mockedLogger
}
}

0 comments on commit 0db7362

Please sign in to comment.