Не подтверждена Коммит 203c29c9 создал по автору Gleb Nazarov's avatar Gleb Nazarov Зафиксировано автором GitHub
Просмотр файлов

KTOR-7797 Remove application lifecycle logs from CallLogging plugin (#4520)

* remove application lifecycle logs from CallLogging server plugin

* remove test with application lifecycle events

* add client call to custom logger test, to check if custom logger is configured successfully

* update license headers
владелец 7eb8e762
/* /*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/ */
package io.ktor.server.plugins.calllogging package io.ktor.server.plugins.calllogging
import io.ktor.events.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.application.hooks.* import io.ktor.server.application.hooks.*
import io.ktor.server.http.content.* import io.ktor.server.http.content.*
...@@ -59,7 +58,6 @@ public val CallLogging: ApplicationPlugin<CallLoggingConfig> = createApplication ...@@ -59,7 +58,6 @@ public val CallLogging: ApplicationPlugin<CallLoggingConfig> = createApplication
} }
setupMDCProvider() setupMDCProvider()
setupLogging(application.monitor, ::log)
on(CallSetup) { call -> on(CallSetup) { call ->
call.attributes.put(CALL_START_TIME, clock()) call.attributes.put(CALL_START_TIME, clock())
...@@ -96,23 +94,3 @@ private fun PluginBuilder<CallLoggingConfig>.logCallsWithMDC(logSuccess: (Applic ...@@ -96,23 +94,3 @@ private fun PluginBuilder<CallLoggingConfig>.logCallsWithMDC(logSuccess: (Applic
} }
} }
} }
private fun setupLogging(events: Events, log: (String) -> Unit) {
val starting: (Application) -> Unit = { log("Application starting: $it") }
val started: (Application) -> Unit = { log("Application started: $it") }
val stopping: (Application) -> Unit = { log("Application stopping: $it") }
var stopped: (Application) -> Unit = {}
stopped = {
log("Application stopped: $it")
events.unsubscribe(ApplicationStarting, starting)
events.unsubscribe(ApplicationStarted, started)
events.unsubscribe(ApplicationStopping, stopping)
events.unsubscribe(ApplicationStopped, stopped)
}
events.subscribe(ApplicationStarting, starting)
events.subscribe(ApplicationStarted, started)
events.subscribe(ApplicationStopping, stopping)
events.subscribe(ApplicationStopped, stopped)
}
/* /*
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/ */
package io.ktor.server.plugins.calllogging package io.ktor.server.plugins.calllogging
...@@ -63,33 +63,6 @@ class CallLoggingTest { ...@@ -63,33 +63,6 @@ class CallLoggingTest {
messages = ArrayList() messages = ArrayList()
} }
@Test
fun `can log application lifecycle events`() = runTest {
var hash: String? = null
runTestApplication {
environment { environment() }
application {
install(CallLogging) { clock { 0 } }
hash = hashCode().toString(radix = 16)
}
}
assertTrue(messages.size >= 3, "It should be at least 3 message logged:\n$messages")
val startingMessageIndex = messages.indexOfFirst {
it == "INFO: Application started: io.ktor.server.application.Application@$hash"
}
val stoppingMessageIndex = messages.indexOfFirst {
it == "INFO: Application stopping: io.ktor.server.application.Application@$hash"
}
val stoppedMessageIndex = messages.indexOfFirst {
it == "INFO: Application stopped: io.ktor.server.application.Application@$hash"
}
assertTrue { startingMessageIndex >= 0 }
assertTrue { startingMessageIndex < stoppingMessageIndex }
assertTrue { stoppingMessageIndex < stoppedMessageIndex }
}
@Test @Test
fun `can log an unhandled get request`() = testApplication { fun `can log an unhandled get request`() = testApplication {
environment { environment() } environment { environment() }
...@@ -398,7 +371,6 @@ class CallLoggingTest { ...@@ -398,7 +371,6 @@ class CallLoggingTest {
} }
} }
} }
lateinit var hash: String
runTestApplication { runTestApplication {
application { application {
...@@ -407,11 +379,11 @@ class CallLoggingTest { ...@@ -407,11 +379,11 @@ class CallLoggingTest {
clock { 0 } clock { 0 }
} }
} }
application { hash = hashCode().toString(radix = 16) } client.get("/")
} }
assertTrue(customMessages.isNotEmpty()) assertTrue(customMessages.isNotEmpty())
assertTrue(customMessages.all { it.startsWith("CUSTOM TRACE:") && it.contains(hash) }) assertTrue(customMessages.all { it.startsWith("CUSTOM TRACE:") })
assertTrue(messages.isEmpty()) assertTrue(messages.isEmpty())
} }
......
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать