Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-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.*
Expand Down Expand Up @@ -55,7 +54,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 @@ -92,23 +90,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
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
Loading