-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin for call loggin til fler loggere
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
val ktorVersion = "3.0.0" | ||
|
||
dependencies { | ||
api(project(":libs:utils")) | ||
|
||
implementation("io.ktor:ktor-server-core:$ktorVersion") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Ktor Plugins | ||
|
||
## CallLog | ||
Ktor sin `CallLogging` støtter ikke logging til forskjellige loggere. | ||
|
||
Hvis man ønsker å logge til både applikasjonslogger og til secure-log kan man bruke denne. | ||
|
||
Denne er avhengig av ktor-pluginen `install(DoubleReceive)`. | ||
|
||
Usage: | ||
```kotlin | ||
install(DoubleReceive) | ||
install(CallLog) { | ||
exclude { call -> call.request.path().startsWith("/probes") } | ||
log { call -> | ||
appLog.info("${call.request.httpMethod.value} ${call.request.local.uri} gave ${call.response.status()} in ${call.processingTimeMs()}ms") | ||
secureLog.info(call.bodyAsText()) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package libs.ktor | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.application.hooks.* | ||
import io.ktor.server.request.* | ||
import io.ktor.util.* | ||
import io.ktor.util.date.* | ||
import kotlinx.coroutines.runBlocking | ||
|
||
private val CALL_START_TIME = AttributeKey<Long>("CallStartTime") | ||
|
||
class CallLogConfig { | ||
var clock: () -> Long = { getTimeMillis() } | ||
var exclusion: (ApplicationCall) -> Boolean = { false } | ||
var action: (ApplicationCall) -> Unit = {} | ||
|
||
fun exclude(filter: (ApplicationCall) -> Boolean) { | ||
exclusion = filter | ||
} | ||
|
||
fun log(block: (ApplicationCall) -> Unit) { | ||
action = block | ||
} | ||
} | ||
|
||
val CallLog = createApplicationPlugin("CallLog", ::CallLogConfig) { | ||
on(CallSetup) { call -> | ||
call.attributes.put(CALL_START_TIME, pluginConfig.clock()) | ||
} | ||
|
||
on(ResponseSent) { call -> | ||
if (!pluginConfig.exclusion(call)) { | ||
pluginConfig.action(call) | ||
} | ||
} | ||
} | ||
|
||
fun ApplicationCall.processingTimeMs(clock: () -> Long = { getTimeMillis() }): Long { | ||
val startTime = attributes[CALL_START_TIME] | ||
return clock() - startTime | ||
} | ||
|
||
fun ApplicationCall.bodyAsText(): String = runBlocking { receiveText() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters