diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/config/HttpCommunicationLogConfig.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/config/HttpCommunicationLogConfig.kt new file mode 100644 index 0000000..d0ad678 --- /dev/null +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/config/HttpCommunicationLogConfig.kt @@ -0,0 +1,13 @@ +package com.kuvaszuptime.kuvasz.config + +import io.micronaut.context.annotation.ConfigurationProperties +import io.micronaut.context.annotation.Context +import io.micronaut.core.annotation.Introspected +import io.micronaut.logging.LogLevel + +@ConfigurationProperties("http-communication-log") +@Context +@Introspected +class HttpCommunicationLogConfig { + var level: LogLevel = LogLevel.INFO +} diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/config/LoggerStartupListener.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/config/LoggerStartupListener.kt deleted file mode 100644 index 14b9204..0000000 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/config/LoggerStartupListener.kt +++ /dev/null @@ -1,40 +0,0 @@ -@file:Suppress("EmptyFunctionBlock") - -package com.kuvaszuptime.kuvasz.config - -import arrow.core.getOrElse -import arrow.core.toOption -import ch.qos.logback.classic.Level -import ch.qos.logback.classic.Logger -import ch.qos.logback.classic.LoggerContext -import ch.qos.logback.classic.spi.LoggerContextListener -import ch.qos.logback.core.Context -import ch.qos.logback.core.spi.ContextAwareBase -import ch.qos.logback.core.spi.LifeCycle - -class LoggerStartupListener : ContextAwareBase(), LoggerContextListener, LifeCycle { - private var isStarted = false - - override fun isStarted() = isStarted - - override fun isResetResistant() = true - - override fun onStart(context: LoggerContext?) {} - - override fun onReset(context: LoggerContext?) {} - - override fun onStop(context: LoggerContext?) {} - - override fun onLevelChange(logger: Logger?, level: Level?) {} - - override fun start() { - if (isStarted) return - val httpLogLevel = - System.getenv("HTTP_COMMUNICATION_LOG_LEVEL").toOption().getOrElse { Level.INFO.levelStr } - val context: Context = getContext() - context.putProperty("HTTP_COMMUNICATION_LOG_LEVEL", httpLogLevel) - isStarted = true - } - - override fun stop() {} -} diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/filters/LoggingHttpClientFilter.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/filters/LoggingHttpClientFilter.kt index fc1801d..00d2d86 100644 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/filters/LoggingHttpClientFilter.kt +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/filters/LoggingHttpClientFilter.kt @@ -13,7 +13,7 @@ import org.reactivestreams.Publisher import javax.inject.Inject @Filter("/**") -@Requires(property = "app-config.http-communication-logging.enabled", value = "true") +@Requires(property = "http-communication-log.enabled", value = "true") class LoggingHttpClientFilter @Inject constructor(private val service: HttpCommunicationLogger) : HttpClientFilter { diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/filters/LoggingHttpServerFilter.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/filters/LoggingHttpServerFilter.kt index 7b839c1..05abf4c 100644 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/filters/LoggingHttpServerFilter.kt +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/filters/LoggingHttpServerFilter.kt @@ -13,7 +13,7 @@ import org.reactivestreams.Publisher import javax.inject.Inject @Filter("/**") -@Requires(property = "app-config.http-communication-logging.enabled", value = "true") +@Requires(property = "http-communication-log.enabled", value = "true") class LoggingHttpServerFilter @Inject constructor(private val service: HttpCommunicationLogger) : HttpServerFilter, FilterOrderProvider { diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/services/HttpCommunicationLogger.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/services/HttpCommunicationLogger.kt index c127ffe..d79c76c 100644 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/services/HttpCommunicationLogger.kt +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/services/HttpCommunicationLogger.kt @@ -1,19 +1,18 @@ package com.kuvaszuptime.kuvasz.services import com.fasterxml.jackson.annotation.JsonInclude -import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule -import com.kuvaszuptime.kuvasz.util.toNullable -import com.kuvaszuptime.kuvasz.util.unnest +import com.kuvaszuptime.kuvasz.config.HttpCommunicationLogConfig import io.micronaut.context.annotation.Requires import io.micronaut.core.annotation.Introspected -import io.micronaut.core.io.buffer.ByteBuffer import io.micronaut.http.HttpHeaders import io.micronaut.http.HttpRequest import io.micronaut.http.HttpResponse +import io.micronaut.logging.LogLevel import org.slf4j.LoggerFactory import java.net.URI +import javax.inject.Inject import javax.inject.Singleton @Introspected @@ -22,12 +21,15 @@ data class HttpMessageLog( val method: String, val uri: URI, val headers: List?, - val body: Any? + val body: String? ) @Singleton -@Requires(property = "app-config.http-communication-logging.enabled", value = "true") -class HttpCommunicationLogger { +@Requires(property = "http-communication-log.enabled", value = "true") +class HttpCommunicationLogger @Inject constructor(httpLogConfig: HttpCommunicationLogConfig) { + + private val withHeaders = httpLogConfig.level.ordinal <= LogLevel.DEBUG.ordinal + private val withBody = httpLogConfig.level.ordinal <= LogLevel.TRACE.ordinal companion object { private val objectMapper = ObjectMapper().apply { @@ -39,59 +41,28 @@ class HttpCommunicationLogger { } fun log(request: HttpRequest<*>) { - logger.info( - objectMapper.writeValueAsString( - request.toLog( - withHeaders = logger.isDebugEnabled, - withBody = logger.isTraceEnabled - ) - ) - ) + logger.info(objectMapper.writeValueAsString(request.toLog())) } fun log(request: HttpRequest<*>, response: HttpResponse<*>) { - logger.info( - objectMapper.writeValueAsString( - response.toLog( - request, - withHeaders = logger.isDebugEnabled, - withBody = logger.isTraceEnabled - ) - ) - ) + logger.info(objectMapper.writeValueAsString(response.toLog(request))) } - private fun HttpRequest<*>.toLog( - withHeaders: Boolean, - withBody: Boolean - ): HttpMessageLog = + private fun HttpRequest<*>.toLog(): HttpMessageLog = HttpMessageLog( method = method.name, uri = uri, headers = if (withHeaders) headers.toList() else null, - body = if (withBody) body.unnest().toNullable() else null + body = if (withBody) getBody(String::class.java).orElse("") else null ) - private fun HttpResponse<*>.toLog( - request: HttpRequest<*>, - withHeaders: Boolean, - withBody: Boolean - ): HttpMessageLog = + private fun HttpResponse<*>.toLog(request: HttpRequest<*>): HttpMessageLog = HttpMessageLog( status = status.code, method = request.method.name, uri = request.uri, headers = if (withHeaders) headers.toList() else null, - body = if (withBody) { - body.unnest().toNullable()?.let { nonNullBody -> - when (nonNullBody) { - is ByteBuffer<*> -> getBody(JsonNode::class.java).toNullable() - else -> nonNullBody - } - } - } else { - null - } + body = if (withBody) getBody(String::class.java).orElse("") else null ) private fun HttpHeaders.toList() = flatMap { (key, value) -> value.map { "$key: $it" } } diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/util/Optional+.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/util/Optional+.kt deleted file mode 100644 index 285dae4..0000000 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/util/Optional+.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.kuvaszuptime.kuvasz.util - -import java.util.Optional - -fun Optional.toNullable(): T? = this.orElse(null) - -@Suppress("UNCHECKED_CAST") -fun Optional<*>.unnest(): Optional<*> = - if (isPresent) { - when (val inner = get()) { - is Optional<*> -> (inner as Optional).unnest() - else -> this - } - } else { - this - } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 925daa4..4493515 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -13,9 +13,8 @@ datasources: username: 'postgres' password: 'pass' --- -app-config: - http-communication-logging: - enabled: true +http-communication-log: + enabled: true --- handler-config: smtp-event-handler: diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index fb9403b..1f89795 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -74,10 +74,12 @@ jooq: sql-dialect: POSTGRES --- app-config: - http-communication-logging: - enabled: ${ENABLE_HTTP_COMMUNICATION_LOG:`false`} data-retention-days: ${DATA_RETENTION_DAYS:`30`} --- +http-communication-log: + enabled: ${ENABLE_HTTP_COMMUNICATION_LOG:`false`} + level: ${HTTP_COMMUNICATION_LOG_LEVEL:`INFO`} +--- handler-config: log-event-handler: enabled: ${ENABLE_LOG_EVENT_HANDLER:`true`} diff --git a/src/main/resources/logback-dev.xml b/src/main/resources/logback-dev.xml index 225b6e8..6f42709 100644 --- a/src/main/resources/logback-dev.xml +++ b/src/main/resources/logback-dev.xml @@ -1,5 +1,4 @@ - true