Ktor 2.3.12 Help

Logging

Ktor provides different means of logging your application depending on the used platform:

  • On JVM, Ktor 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.

  • For the Native server, Ktor provides a logger that prints everything to the standard output.

JVM

Add logger dependencies

To enable logging, you need to include artifacts for the desired logging framework. For example, Logback requires the following dependency:

implementation("ch.qos.logback:logback-classic:$logback_version")
implementation "ch.qos.logback:logback-classic:$logback_version"
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback_version}</version> </dependency>

To use Log4j, you need to add the org.apache.logging.log4j:log4j-core and org.apache.logging.log4j:log4j-slf4j-impl artifacts.

Configure logger

To learn how to configure the selected logging framework, see its documentation, for example:

For instance, to configure Logback, you need to put a logback.xml file in the root of the classpath (for example, in src/main/resources). The example below shows a sample Logback configuration with the STDOUT appender, which outputs logs to the console.

<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="trace"> <appender-ref ref="STDOUT"/> </root> <logger name="io.netty" level="INFO"/> </configuration>

If you want to output logs to a file, you can use the FILE appender.

<configuration> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>testFile.log</file> <append>true</append> <encoder> <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="trace"> <appender-ref ref="FILE"/> </root> <logger name="io.netty" level="INFO"/> </configuration>

You can find the full example here: logging.

Native

To configure the logging level for the Native server, assign one of the following values to the KTOR_LOG_LEVEL environment variable when running the application:

  • TRACE

  • DEBUG

  • INFO

  • WARN

  • ERROR

For example, the TRACE level enables route tracing that helps you determine why some routes are not being executed.

Access logger in code

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.log property. For example, the code snippet below shows how to add a message to a log inside the module.

import io.ktor.server.application.* fun Application.module() { log.info("Hello from module!") }

You can also access the Logger from ApplicationCall using the call.application.environment.log property.

routing { get("/api/v1") { call.application.environment.log.info("Hello from /api/v1!") } }

Logging in Plugins and Files

Using application log inside plugins and files is not recommended. It is better to use a separate logger for each plugin or file. To do this, you can use any logging library.

For multiplatform projects, you can use the KtorSimpleLogger class:

package com.example import io.ktor.server.application.* import io.ktor.server.request.* import io.ktor.util.logging.* internal val LOGGER = KtorSimpleLogger("com.example.RequestTracePlugin") val RequestTracePlugin = createRouteScopedPlugin("RequestTracePlugin", { }) { onCall { call -> LOGGER.trace("Processing call: ${call.request.uri}") } }
Last modified: 02 April 2024