Enhanced Logging in ktor server with CallLogging Feature
When building web applications, especially APIs, it’s crucial to have robust logging to monitor requests, performance, and errors. Ktor provides a convenient CallLogging feature that can be easily integrated into your server setup to log important information about each HTTP request and response. This logging can be fine-tuned to include custom formatting, highlighting, and filtering.
In this article, we will explore how to use Ktor’s CallLogging to log detailed information for each incoming request, including HTTP method, request path, query parameters, response status, and more. We will also demonstrate how to color-code the logs for better readability in the console.
Setting Up Call Logging in Ktor
To get started, you need to install the CallLogging feature in your Ktor server. Here is a code snippet that demonstrates a highly customized logging configuration:
install(CallLogging){
level = Level.INFO
filter { call -> call.request.path().startsWith("/") }
format { call ->
val status = call.response.status()
val httpMethod = call.request.httpMethod.value
val userAgent = call.request.headers["User-Agent"]
val path = call.request.path()
val queryParams =
call.request.queryParameters
.entries()
.joinToString(", ") { "${it.key}=${it.value}" }
val duration = call.processingTimeMillis()
val remoteHost = call.request.origin.remoteHost
val coloredStatus =
when {
status == null -> "\u001B[33mUNKNOWN\u001B[0m"
status.value < 300 -> "\u001B[32m$status\u001B[0m"
status.value < 400 -> "\u001B[33m$status\u001B[0m"
else -> "\u001B[31m$status\u001B[0m"
}
val coloredMethod = "\u001B[36m$httpMethod\u001B[0m"
"""
|
|------------------------ Request Details ------------------------
|Status: $coloredStatus
|Method: $coloredMethod
|Path: $path
|Query Params: $queryParams
|Remote Host: $remoteHost
|User Agent: $userAgent
|Duration: ${duration}ms
|------------------------------------------------------------------
|
""".trimMargin()
}
}
Key Features of This Configuration
1. Log Level:
The level = Level.INFO specifies that the logging will capture INFO level messages. This can be adjusted based on your needs (e.g., DEBUG, WARN, ERROR).
2. Filtering:
The filter function ensures that only requests starting from the root path (”/”) are logged. You can customize this to log requests to specific paths or APIs.
3. Custom Format:
The format block allows you to create a personalized log message that includes key details about each request:
• Status: The HTTP response status code (e.g., 200, 404), is color-coded based on the status range.
• Method: The HTTP method (GET, POST, etc.), is displayed in cyan.
• Path: The request path.
• Query Parameters: The query parameters in a key-value format.
• Remote Host: The IP address of the client making the request.
• User Agent: The User-Agent header, which tells you what browser or client made the request.
• Duration: The time it took for the server to process the request.
This gives you a full picture of each request’s lifecycle in a clean and organized format.
4. Color Coding:
The log output uses ANSI escape codes to add colors:
• Green for success (2xx),
• Yellow for redirection (3xx) or unknown status,
• Red for error statuses (4xx, 5xx),
• Cyan for HTTP methods.
These colors help differentiate important information quickly when scanning through logs.
Example Output
When a request comes into the server, the log output would look something like this:
------------------------ Request Details ------------------------
Status: [32m200[0m
Method: [36mGET[0m
Path: /api/products
Query Params: id=123, sort=asc
Remote Host: 192.168.1.1
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
Duration: 45ms
------------------------------------------------------------------
This format makes it easy to read and troubleshoot requests at a glance, with different colors highlighting important parts of the log.
Benefits of Using Call Logging in Ktor
1. Improved Debugging: Logs provide essential information for troubleshooting failed requests or performance bottlenecks, allowing developers to understand the request flow.
2. Performance Monitoring: Tracking the processing time of each request helps in identifying slow endpoints.
3. Security Insights: Logging details such as the client’s IP and User-Agent can help detect suspicious activity or malformed requests.
4. Customizable Output: As shown in the code, the format can be adjusted to fit your exact logging needs. You can add or remove details based on what’s important for your application.
GitHub Example: https://github.com/piashcse/ktor-E-Commerce
Blog: https://piashcse.blogspot.com/2024/10/enhanced-logging-in-ktor-server-with.html