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.
Add logger dependencies
To enable logging, you need to include artifacts for the desired logging framework. For example, Logback requires the following dependency:
To use Log4j, you need to add the
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.
If you want to output logs to a file, you can use the
You can find the full example here: logging.
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.
You can also access the Logger from ApplicationCall using the
To enable logging of client requests, you can use the CallLogging plugin.