1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| package com.lingshiedu.lszy.filter
import org.slf4j.LoggerFactory import org.slf4j.MDC import org.springframework.stereotype.Component import org.springframework.web.util.ContentCachingRequestWrapper import org.springframework.web.util.ContentCachingResponseWrapper import org.springframework.web.util.WebUtils import java.io.UnsupportedEncodingException import java.nio.charset.Charset import java.util.* import javax.servlet.* import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse
@Component class LogFilter : Filter { val Logger = LoggerFactory.getLogger(LogFilter::class.java) val SPLIT_STRING_M = "=" val SPLIT_STRING_DOT = ", " override fun destroy() { }
override fun doFilter(req: ServletRequest?, res: ServletResponse?, filterChain: FilterChain?) { val request = req as HttpServletRequest val response = res as HttpServletResponse MDC.clear() MDC.put("trade_id", UUID.randomUUID().toString().replace("-", ""))
val wrapperRequest = ContentCachingRequestWrapper(request) val wrapperResponse = ContentCachingResponseWrapper(response)
Logger.info("method[{}] | queryUrl[{}] | headers[{}] | queryParams[{}] | body:{}", request.method, request.requestURL, getHeaders(req), getRequestParams(request), getRequestBody(wrapperRequest)) filterChain!!.doFilter(wrapperRequest, wrapperResponse) Logger.info("resp body:{}", getResponseBody(wrapperResponse))
wrapperResponse.copyBodyToResponse() }
private fun getRequestBody(request: ContentCachingRequestWrapper): String? { val wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper::class.java) if (wrapper != null) { val buf = wrapper.contentAsByteArray if (buf.isNotEmpty()) { val payload: String = try { String(buf, 0, buf.size, Charset.forName(wrapper.characterEncoding)) } catch (e: UnsupportedEncodingException) { "[unknown]" }
return payload.replace("\\n".toRegex(), "") } }
return "" }
private fun getHeaders(req: HttpServletRequest): String? { var header = "" for (key in req.headerNames) { val value = req.getHeader(key) header += " [key:${key} value:${value}] " }
return header }
private fun getResponseBody(response: ContentCachingResponseWrapper): String? { val wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper::class.java) if (wrapper != null) { val buf = wrapper.contentAsByteArray if (buf.isNotEmpty()) { val payload: String = try { String(buf, 0, buf.size, Charset.forName(wrapper.characterEncoding)) } catch (e: UnsupportedEncodingException) { "[unknown]" }
return payload } }
return "" }
fun getRequestParams(request: HttpServletRequest): String? { val sb = StringBuilder() val enu = request.parameterNames while (enu.hasMoreElements()) { val name = enu.nextElement() sb.append(name + SPLIT_STRING_M).append(request.getParameter(name)) if (enu.hasMoreElements()) { sb.append(SPLIT_STRING_DOT) } }
return sb.toString() }
override fun init(p0: FilterConfig?) { } }
|