Is it possible to log requests in kotlin spring mvc? I tried using content caching wrappers. I am able to see the request input in the logs but the request responses are empty. Is there something that I am missing or I will have to use spring webflux with kotlin to achieve this?

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
class RequestLoggingFilter : OncePerRequestFilter() {

    override fun doFilterInternal(request: HttpServletRequest, response: HttpServletResponse, filterChain: FilterChain) {
        val startTime = Instant.now().toEpochMilli()
        val requestWrapper = ContentCachingRequestWrapper(request)
        val responseWrapper = ContentCachingResponseWrapper(response)
        filterChain.doFilter(requestWrapper, responseWrapper)
        logRequest(startTime, requestWrapper, responseWrapper)
    }

    private fun logRequest(
        startTime: Long,
        request: ContentCachingRequestWrapper,
        response: ContentCachingResponseWrapper,
    ) {
        log.info(
            "$REQUEST_LOG",
            kv("$REQUEST", String(request.contentAsByteArray)),
            kv("$RESPONSE", String(response.contentAsByteArray)),
            kv(
                "$REQUEST_TIME",
                StringBuilder()
                    .append(Instant.now().toEpochMilli().minus(startTime))
                    .append("ms"),
            ),
        )
        response.copyBodyToResponse()
    }

    @Throws(ServletException::class)
    override fun shouldNotFilter(request: HttpServletRequest): Boolean {
        return EXCLUDED_URLS.contains(request.requestURI)
    }

    companion object {
        private val log = LoggerFactory.getLogger(RequestLoggingFilter::class.java)
    }
}

Comment From: sdeleuze

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

From a quick look, this does not look like Kotlin specific.