CallLogging plugin allows you to log incoming client requests.
CallLogging, you need to include the
ktor-server-call-logging artifact in the build script:
To install the
CallLogging plugin, pass it to the
install function in the application initialization code. Depending on the way used to create a server, this can be the
embeddedServer function call ...
... or a specified module.
Configure logging settings
You can configure
CallLogging in multiple ways: specify a logging level, filter requests based on a specified condition, customize log messages, and so on. You can see the available configuration settings at CallLoggingConfig.
Set the logging level
By default, Ktor uses the
Level.INFO logging level. To change it, use the
Filter log requests
filter property allows you to add conditions for filtering requests. In the example below, only requests made to
/api/v1 get into a log:
Customize a log message format
By using the
format function, you can put any data related to a request/response into a log. The example below shows how to log a response status, a request HTTP method, and the
User-Agent header value for each request.
You can find the full example here: logging.
Put call parameters in MDC
CallLogging plugin supports MDC (Mapped Diagnostic Context). You can put a desired context value with the specified name to MDC using the
mdc function. For example, in the code snippet below, a
name query parameter is added to MDC:
You can access the added value during an