Spring Boot Webflux与Morgan JS库等效吗?

时间:2019-02-13 12:43:33

标签: java spring-boot logging kotlin

Javascript有一个了不起的库,名为morgan,可以注销所有传入的HTTP请求。想知道Java / Kotlin中是否有一个等效的库来记录spring boot webflux请求。

1 个答案:

答案 0 :(得分:0)

这是在看完repo

之后对我有用的东西

我整夜都在尝试获取请求正文,但始终出错:每个请求仅单个订户。这是不可能的,相当困难,或者在请求量很大的情况下不建议这样做(因为它可能会阻塞您的服务器)。因此,如果要在发布请求中记录变量,我强烈建议您将@RequestBody对象转换为@RequestParam查询参数。

package com.example.demo

import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import org.springframework.web.server.ServerWebExchange
import org.springframework.web.server.WebFilter
import org.springframework.web.server.WebFilterChain
import reactor.core.publisher.Mono


@Component
class LogFilter: WebFilter {

    private val logger = LoggerFactory.getLogger(LogFilter::class.java)

    override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
        val startTime = System.currentTimeMillis()
        val request = exchange.request
        val path = request.uri.path

        val requestPrintMap = mutableMapOf<Any, Any>()
        requestPrintMap["headers"] = request.headers
        requestPrintMap["uri"] = request.uri
        requestPrintMap["params"] = request.queryParams

        return chain
                .filter(exchange)
                .doAfterTerminate {

            logger.info("Served '{}' as {} in {} msec",
                    path,
                    exchange.response.statusCode,
                    System.currentTimeMillis() - startTime)
        }
    }

}