SpringMVC - 使用 Filter 统一打请求日志

继续后端服务系列:

使用 Filter 统一打日志。Filter 其实来自 javax.servlet.Filter,是在 Servlet 规范中定义的,是 Servlet 容器支持的。

效果如下:

1
[23:42:05:223] [INFO] - com.tracenote.filters.RequestBodyReaderWrapper.writeLog(RequestBodyReaderWrapper.java:29) - {    "name": "zhang"}

实现:

web.xml 配置

1
2
3
4
5
6
7
8
<filter>
<filter-name>RequestFilter</filter-name>
<filter-class>com.tracenote.filters.RequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RequestFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
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
package com.tracenote.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;


public class RequestFilter implements Filter {
private static Logger logger = LoggerFactory.getLogger(RequestFilter.class);

@Override
public void destroy() {

}

public void init(FilterConfig filterConfig) {

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException {
HttpServletRequest req = (HttpServletRequest) request;
try {
RequestBodyReaderWrapper myRequest = new RequestBodyReaderWrapper(req);
chain.doFilter(myRequest, response);
} catch (Exception e) {
logger.error(e.toString());
}
}
}
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
package com.tracenote.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;

public class RequestBodyReaderWrapper extends HttpServletRequestWrapper {
private static Logger logger = LoggerFactory.getLogger(RequestBodyReaderWrapper.class);
private final byte[] body;

public RequestBodyReaderWrapper(HttpServletRequest request) throws Exception {
super(request);
body = writeLog(request).getBytes();
}

private static String writeLog(HttpServletRequest request) throws Exception {
InputStream inputStream = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String msg = "";
StringBuilder sb = new StringBuilder();
while ((msg = reader.readLine()) != null) {
sb.append(msg);
}
logger.info(sb.toString());
return sb.toString();
}

@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return byteArrayInputStream.read();
}

@Override
public boolean isReady() {
return true;
}

@Override
public void setReadListener(ReadListener readListener) {

}

public boolean isFinished() {
return byteArrayInputStream.available() == 0;
}
};
}

@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
}

参考: