8000 added user id to default call logging (#42) · icerockdev/web-utils@6bf4319 · GitHub
[go: up one dir, main page]

Skip to c 8000 ontent

Commit 6bf4319

Browse files
authored
added user id to default call logging (#42)
1 parent 03f5307 commit 6bf4319

File tree

6 files changed

+18
-8
lines changed

6 files changed

+18
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repositories {
1212
}
1313

1414
// Append dependency
15-
implementation("com.icerockdev:web-utils:0.9.1")
15+
implementation("com.icerockdev:web-utils:0.9.2")
1616
````
1717

1818
## Library usage

sample/collection.http

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ Content-Type: application/json
3030

3131
###
3232

33-
GET http://127.0.0.1:8080/custom-object
33+
GET http://127.0.0.1:8080/custom-object?userId=123456
3434
Accept: application/json
3535

3636
###
3737

38-
GET http://127.0.0.1:8080/exception
38+
GET http://127.0.0.1:8080/exception?userId=123456
3939
Accept: application/json
4040

4141
###
4242

43-
GET http://127.0.0.1:8080/handled
43+
GET http://127.0.0.1:8080/handled?userId=123456
4444
Accept: application/json
4545

4646
###
4747

48-
GET http://127.0.0.1:8080/handled2
48+
GET http://127.0.0.1:8080/handled2?userId=123456
4949
Accept: application/json
5050

5151
###

sample/src/main/kotlin/com/icerockdev/sample/server.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.icerockdev.webserver.log.LoggingConfiguration
2727
import com.icerockdev.webserver.log.jsonLogger
2828
import com.icerockdev.webserver.tools.receiveRequest
2929
import io.ktor.application.Application
30+
import io.ktor.application.ApplicationCall
3031
import io.ktor.application.call
3132
import io.ktor.application.install
3233
import io.ktor.features.CORS
@@ -57,7 +58,9 @@ fun Application.main() {
5758
}
5859
install(DefaultHeaders)
5960
install(ApplicationCallLogging) {
60-
applyDefaultLogging()
61+
applyDefaultLogging { call: ApplicationCall ->
62+
call.parameters["userId"]
63+
}
6164
// Log only /api requests
6265
// filter { call -> call.request.path().startsWith("/api") }
6366
}

web-utils/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ apply(plugin = "java")
1717
apply(plugin = "kotlin")
1818

1919
group = "com.icerockdev"
20-
version = "0.9.1"
20+
version = "0.9.2"
2121

2222
val sourcesJar by tasks.registering(Jar::class) {
2323
archiveClassifier.set("sources")

web-utils/src/main/kotlin/com/icerockdev/webserver/Constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ object Constants {
1515
const val LOG_FIELD_TRACE_UUID: String = "traceUUID"
1616
const val LOG_FIELD_STATUS_CODE: String = "statusCode"
1717
const val LOG_FIELD_ENV: String = "env"
18+
const val LOG_FIELD_USER_ID: String = "userId"
1819
}
1920

2021
enum class Environment(val value: String) {

web-utils/src/main/kotlin/com/icerockdev/webserver/FeatureConfiguration.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ fun CORS.Configuration.applyDefaultCORS() {
7272
allowNonSimpleContentTypes = true
7373
}
7474

75-
fun ApplicationCallLogging.Configuration.applyDefaultLogging(secretFieldList: List<String> = getDefaultSecretFieldList()) {
75+
fun ApplicationCallLogging.Configuration.applyDefaultLogging(
76+
secretFieldList: List<String> = getDefaultSecretFieldList(),
77+
userExtractor: (ApplicationCall) -> String? = { null }
78+
) {
7679
level = Level.TRACE
7780
callIdMdc(Constants.LOG_FIELD_TRACE_UUID)
7881
mdc(Constants.LOG_FIELD_ENV) {
@@ -109,6 +112,9 @@ fun ApplicationCallLogging.Configuration.applyDefaultLogging(secretFieldList: Li
109112
call.attributes.getOrNull(key = it.loggerDataKey)?.responseBody
110113
}
111114
}
115+
mdc(Constants.LOG_FIELD_USER_ID) { call: ApplicationCall ->
116+
userExtractor(call)
117+
}
112118
}
113119

114120
internal fun getDefaultSecretFieldList(): List<String> {

0 commit comments

Comments
 (0)
0