一. 为什么要用到这个
用了 spingcloud 之后,很多业务功能都可以做分离,在网关 zuul 层的过滤器除了做一些验证之外,还可以用来打印请求日志。可以打印请求的req和回应的response
二.代码演示
public class PrintRequestLogFilter extends ZuulFilter {
private static final Logger LOGGER = LoggerFactory.getLogger(PrintRequestLogFilter.class);
@Override
public String filterType() {
return FilterConstants.POST_TYPE;//要打印返回信息,必须得用"post"
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
try {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
InputStream in = request.getInputStream();
String reqBbody = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
// 打印userId,获取其他用户信息
if (reqBbody != null) {
JSONObject json = JSONObject.fromObject(reqBbody);
Object userId = json.get("userId");
if (userId != null) {
PrintRequestLogFilter.LOGGER.info("request userId:\t" + userId);
}
}
// 打印请求方法,路径
PrintRequestLogFilter.LOGGER
.info("request url:\t" + request.getMethod() + "\t" + request.getRequestURL().toString());
Map<String, String[]> map = request.getParameterMap();
// 打印请求url参数
if (map != null) {
StringBuilder sb = new StringBuilder();
sb.append("request parameters:\t");
for (Map.Entry<String, String[]> entry : map.entrySet()) {
sb.append("[" + entry.getKey() + "=" + printArray(entry.getValue()) + "]");
}
PrintRequestLogFilter.LOGGER.info(sb.toString());
}
// 打印请求json参数
if (reqBbody != null) {
PrintRequestLogFilter.LOGGER.info("request body:\t" + reqBbody);
}
// 打印response
InputStream out = ctx.getResponseDataStream();
String outBody = StreamUtils.copyToString(out, Charset.forName("UTF-8"));
if (outBody != null) {
PrintRequestLogFilter.LOGGER.info("response body:\t" + outBody);
}
ctx.setResponseBody(outBody);//重要!!!
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
String printArray(String[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]);
if (i < arr.length - 1) {
sb.append(",");
}
}
return sb.toString();
}
}
以上代码可以打印出所有请求的相关重要信息,包含返回。但要注意一点,就是
ctx.setResponseBody(outBody);
在zuulFilter中,用 InputStream out = ctx.getResponseDataStream(); 取出response 信息后,如果不把信息 set 回去,会导致返回信息为空.
日志打印如下:
request userId: 1
request url: POST http://127.0.0.1:8000/web/pro/get
request parameters: [uuid=001]
request body: {"userId":1,"pageNum":0,"pageSize":10}
response body: {"message":"msg-null","msgCode":"abcd","statusCode":200,"success":true,"data":[],"totalPage":