-
Notifications
You must be signed in to change notification settings - Fork 3
Logging
For logging what is happening on routing infrastructure, you can create a KtorSimpleLogger
instance and pass to the routing instance:
internal val LOGGER = KtorSimpleLogger("com.example.application")
val router = routing(logger = LOGGER) {
handle("/hello") {
// Handle any call to the "/hello" route
}
}
- On JVM,
KtorSimpleLogger
uses SLF4J API as a facade for various logging frameworks (for example, Logback or Log4j) and allows you to log application events. To enable logging, you need to add dependencies for the desired framework and provide configuration specific for this framework.
You can also install and configure the CallLogging plugin to log route requests.
- For others
KtorSimpleLogger
provides a logger that prints everything to the standard output.
More info about setup Logging can be found in the origin docs here.
The Logger
instance is represented by a class that implements the Logger interface. You can access the Logger instance inside the Application using the Application.logger
property. For example, the code snippet below shows how to add a message to a log inside the module.
import dev.programadorthi.routing.core.application
// ...
val router = routing(logger = LOGGER) {
application.logger?.info("Hello from Kotlin Routing!")
// ...
}
You can also access the Logger
from ApplicationCall
using the call.application.environment.log property.
import dev.programadorthi.routing.core.application.application
// ...
val router = routing(logger = LOGGER) {
handle("/hello") {
application.logger?.info("Hello from /hello!")
}
}
The CallLogging
plugin allows you to log incoming route requests.
sourceSets {
commonMain.dependencies {
implementation("dev.programadorthi.routing:call-logging:$version")
}
}
val router = routing(logger = LOGGER) {
install(CallLogging)
// ...
}
By default, it uses the CallLevel.INFO
logging level. To change it, use the level property:
val router = routing(logger = LOGGER) {
install(CallLogging) {
level = CallLevel.DEBUG
}
// ...
}
The filter
property allows you to add conditions for filtering requests. In the example below, only requests made to /home/profile
get into a log:
val router = routing(logger = LOGGER) {
install(CallLogging) {
filter { call ->
call.path().startsWith("/home/profile")
}
}
// ...
}
By using the format
function, you can put any data related to a route into a log. The example below shows how to log a route method, a name, a URI, and the parameters value to each route.
val router = routing(logger = LOGGER) {
install(CallLogging) {
format { call ->
"Method: ${call.routeMethod}, name: ${call.name}, uri: ${call.uri}, parameters: ${call.parameters.toMap()}"
}
}
// ...
}
Important
There is an embedded support to MDC (Mapped Diagnostic Context) by default but it hasn't been tested yet