Skip to content

Commit

Permalink
KTOR-7797 Remove application lifecycle logs from CallLogging plugin (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
stokado authored Feb 11, 2025
1 parent 7eb8e76 commit 203c29c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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-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

import io.ktor.events.*
import io.ktor.server.application.*
import io.ktor.server.application.hooks.*
import io.ktor.server.http.content.*
Expand Down Expand Up @@ -59,7 +58,6 @@ public val CallLogging: ApplicationPlugin<CallLoggingConfig> = createApplication
}

setupMDCProvider()
setupLogging(application.monitor, ::log)

on(CallSetup) { call ->
call.attributes.put(CALL_START_TIME, clock())
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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() }
Expand Down Expand Up @@ -398,7 +371,6 @@ class CallLoggingTest {
}
}
}
lateinit var hash: String

runTestApplication {
application {
Expand All @@ -407,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())
}

Expand Down

0 comments on commit 203c29c

Please sign in to comment.