From 4ce87d9bbd40e8984bf7c6fa207c4429cc734480 Mon Sep 17 00:00:00 2001 From: Gleb Nazarov Date: Sat, 30 Nov 2024 17:57:39 +0500 Subject: [PATCH 1/4] remove application lifecycle logs from CallLogging server plugin --- .../server/plugins/calllogging/CallLogging.kt | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/src/io/ktor/server/plugins/calllogging/CallLogging.kt b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/src/io/ktor/server/plugins/calllogging/CallLogging.kt index 27c3c91981f..b90f6e0bfe3 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/src/io/ktor/server/plugins/calllogging/CallLogging.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/src/io/ktor/server/plugins/calllogging/CallLogging.kt @@ -1,10 +1,9 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. +* Copyright 2014-2024 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 -import io.ktor.events.* import io.ktor.server.application.* import io.ktor.server.application.hooks.* import io.ktor.server.http.content.* @@ -59,7 +58,6 @@ public val CallLogging: ApplicationPlugin = createApplication } setupMDCProvider() - setupLogging(application.monitor, ::log) on(CallSetup) { call -> call.attributes.put(CALL_START_TIME, clock()) @@ -96,23 +94,3 @@ private fun PluginBuilder.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) -} From e016343e075ce8a141a3cd8b824b77846c33c4f0 Mon Sep 17 00:00:00 2001 From: Gleb Nazarov Date: Sat, 30 Nov 2024 21:02:25 +0500 Subject: [PATCH 2/4] remove test with application lifecycle events --- .../plugins/calllogging/CallLoggingTest.kt | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt index f2873d553ba..93bef0ea83a 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt @@ -63,33 +63,6 @@ class CallLoggingTest { 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 fun `can log an unhandled get request`() = testApplication { environment { environment() } From ce4809c5cbbe9d51822b341b002aa3721df95607 Mon Sep 17 00:00:00 2001 From: Gleb Nazarov Date: Sat, 30 Nov 2024 21:03:11 +0500 Subject: [PATCH 3/4] add client call to custom logger test, to check if custom logger is configured successfully --- .../io/ktor/server/plugins/calllogging/CallLoggingTest.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt index 93bef0ea83a..44b1710dc4e 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt @@ -371,7 +371,6 @@ class CallLoggingTest { } } } - lateinit var hash: String runTestApplication { application { @@ -380,11 +379,11 @@ class CallLoggingTest { clock { 0 } } } - application { hash = hashCode().toString(radix = 16) } + client.get("/") } assertTrue(customMessages.isNotEmpty()) - assertTrue(customMessages.all { it.startsWith("CUSTOM TRACE:") && it.contains(hash) }) + assertTrue(customMessages.all { it.startsWith("CUSTOM TRACE:") }) assertTrue(messages.isEmpty()) } From c75c6c48350f761de3960636856f174883f7e07d Mon Sep 17 00:00:00 2001 From: Gleb Nazarov Date: Mon, 10 Feb 2025 19:45:44 +0500 Subject: [PATCH 4/4] update license headers --- .../jvm/src/io/ktor/server/plugins/calllogging/CallLogging.kt | 2 +- .../test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/src/io/ktor/server/plugins/calllogging/CallLogging.kt b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/src/io/ktor/server/plugins/calllogging/CallLogging.kt index b90f6e0bfe3..c309899d1d7 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/src/io/ktor/server/plugins/calllogging/CallLogging.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/src/io/ktor/server/plugins/calllogging/CallLogging.kt @@ -1,5 +1,5 @@ /* -* 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 diff --git a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt index 44b1710dc4e..be244e8bff0 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt @@ -1,5 +1,5 @@ /* - * 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