Skip to content

Commit

Permalink
chore: minor logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Dwairi committed Dec 1, 2024
1 parent 71662c0 commit 4c90863
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.expediagroup.sdk.core.http.MediaType
import com.expediagroup.sdk.core.http.Request
import com.expediagroup.sdk.core.http.RequestBody
import com.expediagroup.sdk.core.http.Response
import com.expediagroup.sdk.core.logging.common.Constant.EXPEDIA_GROUP_SDK
import com.expediagroup.sdk.core.logging.common.RequestLogger
import com.expediagroup.sdk.core.logging.common.ResponseLogger
import com.expediagroup.sdk.core.model.exception.client.ExpediaGroupResponseParsingException
Expand All @@ -46,7 +47,7 @@ class BearerAuthenticationManager(
private val transport: Transport,
private val credentials: Credentials
) : AuthenticationManager {
private val logger = LoggerFactory.getLogger(BearerAuthenticationInterceptor::class.simpleName)
private val logger = LoggerFactory.getLogger(EXPEDIA_GROUP_SDK)

@Volatile
private var bearerTokenStorage = BearerTokenStorage.empty
Expand All @@ -65,11 +66,11 @@ class BearerAuthenticationManager(
clearAuthentication()
.let {
buildAuthenticationRequest().also {
RequestLogger.log(logger, it)
RequestLogger.log(logger, it, tags = listOf("Authentication"))
}
}.let {
executeAuthenticationRequest(it).also {
ResponseLogger.log(logger, it)
ResponseLogger.log(logger, it, tags = listOf("Authentication"))
}
}.let {
TokenResponse.parse(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.expediagroup.sdk.core.logging
import com.expediagroup.sdk.core.http.Response
import com.expediagroup.sdk.core.interceptor.Interceptor
import com.expediagroup.sdk.core.logging.common.Constant.DEFAULT_MAX_BODY_SIZE
import com.expediagroup.sdk.core.logging.common.Constant.EXPEDIA_GROUP_SDK
import com.expediagroup.sdk.core.logging.common.RequestLogger
import com.expediagroup.sdk.core.logging.common.ResponseLogger
import java.io.IOException
Expand All @@ -16,17 +17,17 @@ import org.slf4j.LoggerFactory
class LoggingInterceptor(
private val maxBodyLogSize: Long = DEFAULT_MAX_BODY_SIZE
) : Interceptor {
private val logger = LoggerFactory.getLogger(this::class.simpleName)
private val logger = LoggerFactory.getLogger(EXPEDIA_GROUP_SDK)

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()

RequestLogger.log(logger, request, maxBodyLogSize)
RequestLogger.log(logger, request, maxBodyLogSize = maxBodyLogSize)

val response = chain.proceed(request)

ResponseLogger.log(logger, response, maxBodyLogSize)
ResponseLogger.log(logger, response, maxBodyLogSize = maxBodyLogSize)

return response
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ object Constant {
const val NEWLINE = "\n"
const val OMITTED = "<-- omitted -->"
const val DEFAULT_MAX_BODY_SIZE = 8192L //8KB
const val EXPEDIA_GROUP_SDK = "Expedia Group SDK"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ import okio.Buffer
import org.slf4j.Logger

object RequestLogger {
fun log(logger: Logger, request: Request, maxBodyLogSize: Long = DEFAULT_MAX_BODY_SIZE) {
fun log(
logger: Logger,
request: Request,
tags: List<String>? = null,
maxBodyLogSize: Long = DEFAULT_MAX_BODY_SIZE
) {
try {
val requestBodyString = request.body?.let { it.peekContent(maxBodyLogSize, it.mediaType()?.charset) }

buildString {
tags?.let {
append("[")
append(it.joinToString(", "))
append("] - ")
}
append("[Outgoing] - ")
append("[Method=${request.method}, URL=${request.url}, Headers=[${request.headers}], Body=[${requestBodyString}]")
}.also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ import okio.Buffer
import org.slf4j.Logger

object ResponseLogger {
fun log(logger: Logger, response: Response, maxBodyLogSize: Long = DEFAULT_MAX_BODY_SIZE) {
fun log(
logger: Logger,
response: Response,
tags: List<String>? = null,
maxBodyLogSize: Long = DEFAULT_MAX_BODY_SIZE
) {
try {
val responseBodyString = response.body?.let { it.peekContent(maxBodyLogSize, it.mediaType()?.charset) }

buildString {
tags?.let {
append("[")
append(it.joinToString(", "))
append("] - ")
}
append("[Incoming] - ")
append("[Code=${response.code}, URL=${response.request.url}, Headers=[${response.headers}], Body=[${responseBodyString}]")
}.also {
Expand Down

0 comments on commit 4c90863

Please sign in to comment.