Skip to content

Commit

Permalink
Fix HTTP communication logging in native images
Browse files Browse the repository at this point in the history
Resolves #28
  • Loading branch information
akobor committed Aug 23, 2020
1 parent cd0cd3f commit 13036fd
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -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
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,12 +21,15 @@ data class HttpMessageLog(
val method: String,
val uri: URI,
val headers: List<String>?,
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 {
Expand All @@ -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" } }
Expand Down
16 changes: 0 additions & 16 deletions src/main/kotlin/com/kuvaszuptime/kuvasz/util/Optional+.kt

This file was deleted.

5 changes: 2 additions & 3 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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`}
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/logback-dev.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<configuration>
<contextListener class="com.kuvaszuptime.kuvasz.config.LoggerStartupListener"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<!-- encoders are assigned the type
Expand All @@ -11,7 +10,6 @@
</encoder>
</appender>

<logger name="com.kuvaszuptime.kuvasz.services.HttpCommunicationLogger" level="${HTTP_COMMUNICATION_LOG_LEVEL}"/>
<logger name="org.jooq.Constants" level="WARN"/>
<logger name="org.jooq" level="INFO"/>

Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<configuration>
<contextListener class="com.kuvaszuptime.kuvasz.config.LoggerStartupListener"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<!-- encoders are assigned the type
Expand All @@ -12,7 +11,6 @@
</appender>

<logger name="org.jooq" level="ERROR"/>
<logger name="com.kuvaszuptime.kuvasz.services.HttpCommunicationLogger" level="${HTTP_COMMUNICATION_LOG_LEVEL}"/>

<root level="info">
<appender-ref ref="STDOUT"/>
Expand Down
6 changes: 3 additions & 3 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ datasources:
default:
maximumPoolSize: 3
---
app-config:
http-communication-logging:
enabled: true
http-communication-log:
enabled: true
level: TRACE
---
admin-auth:
username: test-user
Expand Down
2 changes: 0 additions & 2 deletions src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<configuration>
<contextListener class="com.kuvaszuptime.kuvasz.config.LoggerStartupListener"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<!-- encoders are assigned the type
Expand All @@ -11,7 +10,6 @@
</encoder>
</appender>

<logger name="com.kuvaszuptime.kuvasz.services.HttpCommunicationLogger" level="TRACE"/>
<logger name="org.jooq.Constants" level="WARN"/>
<logger name="org.jooq" level="DEBUG"/>

Expand Down

0 comments on commit 13036fd

Please sign in to comment.