Skip to content

Commit

Permalink
Remote IP support for logging filter
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmin-marginean committed Apr 29, 2024
1 parent 052c792 commit 7fe2fff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
invirtVersion = 0.8.22
invirtVersion = 0.8.23
kotlinVersion = 1.9.23
http4kVersion = 5.17.0.0
mockkVersion = 1.13.9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package invirt.http4k.filters

import io.github.oshai.kotlinlogging.KotlinLogging
import org.http4k.core.Filter
import org.http4k.core.Request
import org.http4k.filter.ResponseFilters

object HttpAccessLogFilter {

internal val log = KotlinLogging.logger {}

operator fun invoke(errorsOnly: Boolean = true): Filter {
operator fun invoke(
errorsOnly: Boolean = true,
remoteIp: (Request) -> String = { it.source?.address ?: "no-remote-ip" }
): Filter {
return ResponseFilters.ReportHttpTransaction(recordFn = { tx ->
if (!errorsOnly || tx.response.status.code >= 400) {
log.atInfo {
Expand All @@ -17,7 +21,8 @@ object HttpAccessLogFilter {
"method" to tx.request.method,
"uri" to tx.request.uri,
"status" to tx.response.status,
"durationMs" to tx.duration.toMillis()
"durationMs" to tx.duration.toMillis(),
"remoteIp" to remoteIp(tx.request)
)
}
}
Expand Down

0 comments on commit 7fe2fff

Please sign in to comment.