mirror of
https://github.com/gotoeasy/glogcenter.git
synced 2025-09-15 12:58:34 +08:00
0.12.0
This commit is contained in:
parent
4b7a460682
commit
5e63bd9424
@ -5,7 +5,7 @@
|
||||
|
||||
<groupId>top.gotoeasy</groupId>
|
||||
<artifactId>glc-logback-appender</artifactId>
|
||||
<version>0.11.1</version>
|
||||
<version>0.12.0</version>
|
||||
<description>logback appender for glogcenter</description>
|
||||
|
||||
<repositories>
|
||||
@ -74,6 +74,13 @@
|
||||
<version>5.15.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
|
||||
@ -0,0 +1,57 @@
|
||||
package top.gotoeasy.framework.glc.filter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import top.gotoeasy.framework.glc.logback.appender.MdcUtil;
|
||||
|
||||
/**
|
||||
* 过滤器,自动设定GLC所需的traceid、clientip,优先从请求头中获取
|
||||
*/
|
||||
public class GlcFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
|
||||
// 设定日志中心相关的的traceid、clientip
|
||||
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
|
||||
String traceid = httpServletRequest.getHeader(MdcUtil.TRACE_ID);
|
||||
MdcUtil.setTraceId((traceid == null || traceid.length() == 0) ? MdcUtil.generateTraceId() : traceid);
|
||||
MdcUtil.setClientIp(getIpAddr(httpServletRequest));
|
||||
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
private static String getIpAddr(HttpServletRequest request) {
|
||||
String[] headerNames = { "X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP", "WL-Proxy-Client-IP",
|
||||
"HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR" };
|
||||
for (String headerName : headerNames) {
|
||||
String ip = request.getHeader(headerName);
|
||||
if (ip != null && ip.length() > 0 && !"unknown".equalsIgnoreCase(ip)) {
|
||||
int index = ip.indexOf(',');
|
||||
if (index > 0) {
|
||||
ip = ip.substring(0, index);
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
return request.getRemoteAddr();
|
||||
}
|
||||
|
||||
}
|
||||
@ -30,7 +30,7 @@ public class MdcUtil {
|
||||
}
|
||||
|
||||
public static String generateTraceId() {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||
return Util.hash(UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package top.gotoeasy.framework.glc.logback.appender;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.net.InetAddress;
|
||||
import java.text.CharacterIterator;
|
||||
import java.text.SimpleDateFormat;
|
||||
@ -99,4 +100,13 @@ public class Util {
|
||||
}
|
||||
}
|
||||
|
||||
public static String hash(String str) {
|
||||
int rs = 53653;
|
||||
int i = (str == null ? 0 : str.length());
|
||||
while (i > 0) {
|
||||
rs = (rs * 33) ^ str.charAt(--i);
|
||||
}
|
||||
return new BigDecimal(Long.valueOf(rs & 0x0FFFFFFFFL)).toPlainString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user