File tree Expand file tree Collapse file tree 6 files changed +18
-8
lines changed
src/main/kotlin/com/icerockdev/sample
src/main/kotlin/com/icerockdev/webserver Expand file tree Collapse file tree 6 files changed +18
-8
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ repositories {
12
12
}
13
13
14
14
// Append dependency
15
- implementation(" com.icerockdev:web-utils:0.9.1 " )
15
+ implementation(" com.icerockdev:web-utils:0.9.2 " )
16
16
````
17
17
18
18
## Library usage
Original file line number Diff line number Diff line change @@ -30,22 +30,22 @@ Content-Type: application/json
30
30
31
31
###
32
32
33
- GET http://127.0.0.1:8080/custom-object
33
+ GET http://127.0.0.1:8080/custom-object?userId=123456
34
34
Accept: application/json
35
35
36
36
###
37
37
38
- GET http://127.0.0.1:8080/exception
38
+ GET http://127.0.0.1:8080/exception?userId=123456
39
39
Accept: application/json
40
40
41
41
###
42
42
43
- GET http://127.0.0.1:8080/handled
43
+ GET http://127.0.0.1:8080/handled?userId=123456
44
44
Accept: application/json
45
45
46
46
###
47
47
48
- GET http://127.0.0.1:8080/handled2
48
+ GET http://127.0.0.1:8080/handled2?userId=123456
49
49
Accept: application/json
50
50
51
51
###
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ import com.icerockdev.webserver.log.LoggingConfiguration
27
27
import com.icerockdev.webserver.log.jsonLogger
28
28
import com.icerockdev.webserver.tools.receiveRequest
29
29
import io.ktor.application.Application
30
+ import io.ktor.application.ApplicationCall
30
31
import io.ktor.application.call
31
32
import io.ktor.application.install
32
33
import io.ktor.features.CORS
@@ -57,7 +58,9 @@ fun Application.main() {
57
58
}
58
59
install(DefaultHeaders )
59
60
install(ApplicationCallLogging ) {
60
- applyDefaultLogging()
61
+ applyDefaultLogging { call: ApplicationCall ->
62
+ call.parameters[" userId" ]
63
+ }
61
64
// Log only /api requests
62
65
// filter { call -> call.request.path().startsWith("/api") }
63
66
}
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ apply(plugin = "java")
17
17
apply (plugin = " kotlin" )
18
18
19
19
group = " com.icerockdev"
20
- version = " 0.9.1 "
20
+ version = " 0.9.2 "
21
21
22
22
val sourcesJar by tasks.registering(Jar ::class ) {
23
23
archiveClassifier.set(" sources" )
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ object Constants {
15
15
const val LOG_FIELD_TRACE_UUID : String = " traceUUID"
16
16
const val LOG_FIELD_STATUS_CODE : String = " statusCode"
17
17
const val LOG_FIELD_ENV : String = " env"
18
+ const val LOG_FIELD_USER_ID : String = " userId"
18
19
}
19
20
20
21
enum class Environment (val value : String ) {
Original file line number Diff line number Diff line change @@ -72,7 +72,10 @@ fun CORS.Configuration.applyDefaultCORS() {
72
72
allowNonSimpleContentTypes = true
73
73
}
74
74
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
+ ) {
76
79
level = Level .TRACE
77
80
callIdMdc(Constants .LOG_FIELD_TRACE_UUID )
78
81
mdc(Constants .LOG_FIELD_ENV ) {
@@ -109,6 +112,9 @@ fun ApplicationCallLogging.Configuration.applyDefaultLogging(secretFieldList: Li
109
112
call.attributes.getOrNull(key = it.loggerDataKey)?.responseBody
110
113
}
111
114
}
115
+ mdc(Constants .LOG_FIELD_USER_ID ) { call: ApplicationCall ->
116
+ userExtractor(call)
117
+ }
112
118
}
113
119
114
120
internal fun getDefaultSecretFieldList (): List <String > {
You can’t perform that action at this time.
0 commit comments