首页   注册   登录
 iiicarus 最近的时间轴更新

iiicarus

V2EX 第 418744 号会员,加入于 2019-06-04 19:23:14 +08:00
像鱼
随想  •  iiicarus  •  3 天前  •  最后回复来自 guolaopi
7
求教,如何从 HttpServletRequest 中获取类名和方法名
Java  •  iiicarus  •  5 天前  •  最后回复来自 iiicarus
5
B-Tree 和 B+Tree 怎么读
问与答  •  iiicarus  •  13 天前  •  最后回复来自 xiaottt
16
60 出一年腾讯视频会员,绑定你的手机号
二手交易  •  iiicarus  •  24 天前  •  最后回复来自 iiicarus
6
今天星期六,还要上班,苦逼
职场话题  •  iiicarus  •  23 天前  •  最后回复来自 shanlan
62
iiicarus 最近回复了
@Aresxue 谢谢!从 request 中获取类名和方法名太困难了,感觉违背了 AOP 存在的意义!我们领导也是非要我这样做,目前的做法是,从路径 path 中分析得到模块。也没有具体的类名和方法名,不过另外做了 AOP,没提交,等后面要的时候再说吧~~~
平凡的世界
想要中奖,就是这么直接!
代码如上
@Slf4j
public class HttpTraceLogFilter extends OncePerRequestFilter implements Ordered {

private static final String NEED_TRACE_PATH_PREFIX_BUSINESS = "/business";
private static final String NEED_TRACE_PATH_PREFIX_NPI = "/npi";
private static final String NEED_TRACE_PATH_PREFIX_SYSTEM = "/system";
private static final String IGNORE_CONTENT_TYPE = "multipart/form-data";

private final MeterRegistry registry;

public HttpTraceLogFilter(MeterRegistry registry) {
this.registry = registry;
}

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 10;
}

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (!isRequestValid(request)) {
filterChain.doFilter(request, response);
return;
}
if (!(request instanceof ContentCachingRequestWrapper)) {
request = new ContentCachingRequestWrapper(request);
}
if (!(response instanceof ContentCachingResponseWrapper)) {
response = new ContentCachingResponseWrapper(response);
}

int status = HttpStatus.INTERNAL_SERVER_ERROR.value();
long startTime = System.currentTimeMillis();
try {
filterChain.doFilter(request, response);
status = response.getStatus();
} finally {
String path = request.getRequestURI();
boolean pathFlag = path.startsWith(NEED_TRACE_PATH_PREFIX_BUSINESS) || path.startsWith(NEED_TRACE_PATH_PREFIX_NPI) || path.startsWith(NEED_TRACE_PATH_PREFIX_SYSTEM);
if (pathFlag && !Objects.equals(IGNORE_CONTENT_TYPE, request.getContentType())) {

String requestBody = IOUtils.toString(request.getInputStream(), Charsets.UTF_8);
log.info(requestBody);
//1. 记录日志
HttpTraceLog traceLog = new HttpTraceLog();
traceLog.setPath(path);
log.info("path ===================>: {}", path);
traceLog.setMethod(request.getMethod());
log.info("method ===================>: {}", request.getMethod());
long timeConsuming = System.currentTimeMillis() - startTime;
traceLog.setTimeTaken(timeConsuming);
log.info("timeConsuming ===================>: {} ms", timeConsuming);
//traceLog.setTime(LocalDateTime.now().toString());
//traceLog.setParameterMap(JsonMapper.INSTANCE.toJson(request.getParameterMap()));
traceLog.setStatus(status);
log.info("status ===================>: {}", status);
//traceLog.setRequestBody(getRequestBody(request));
//traceLog.setResponseBody(getResponseBody(response));
//log.info("Http trace log: {}", JsonMapper.INSTANCE.toJson(traceLog));
//log.info("Http trace log: {}===============================================>", traceLog);
}
updateResponse(response);
}
}

private boolean isRequestValid(HttpServletRequest request) {
try {
new URI(request.getRequestURL().toString());
return true;
} catch (URISyntaxException ex) {
return false;
}
}

private String getRequestBody(HttpServletRequest request) {
String requestBody = "";
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
try {
requestBody = IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
} catch (IOException e) {
// NOOP
}
}
return requestBody;
}

private String getResponseBody(HttpServletResponse response) {
String responseBody = "";
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
if (wrapper != null) {
try {
responseBody = IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
} catch (IOException e) {
// NOOP
}
}
return responseBody;
}

private void updateResponse(HttpServletResponse response) throws IOException {
ContentCachingResponseWrapper responseWrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
Objects.requireNonNull(responseWrapper).copyBodyToResponse();
}


@Data
private static class HttpTraceLog {
private String path;
private String parameterMap;
private String method;
private Long timeTaken;
private String time;
private Integer status;
private String requestBody;
private String responseBody;
}
}
@Configuration
@ConditionalOnWebApplication
public class HttpTraceConfiguration {

@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
static class ServletTraceFilterConfiguration {

@Bean
public HttpTraceLogFilter httpTraceLogFilter(MeterRegistry registry) {
return new HttpTraceLogFilter(registry);
}
}
}
15 天前
回复了 iiicarus 创建的主题 问与答 B-Tree 和 B+Tree 怎么读
@7654 get! 谢谢
15 天前
回复了 iiicarus 创建的主题 问与答 B-Tree 和 B+Tree 怎么读
@lhx2008 谢谢。以后就这样读。读英文可能有的人不喜欢
15 天前
回复了 iiicarus 创建的主题 问与答 B-Tree 和 B+Tree 怎么读
@yidinghe 好的 谢谢
关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   2047 人在线   最高记录 5043   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.3 · 21ms · UTC 10:13 · PVG 18:13 · LAX 03:13 · JFK 06:13
♥ Do have faith in what you're doing.