优化日志埋点工具类
This commit is contained in:
@@ -42,6 +42,11 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-aop</artifactId>
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-web</artifactId>
|
<artifactId>spring-web</artifactId>
|
||||||
|
|||||||
+86
-1
@@ -1,18 +1,41 @@
|
|||||||
package com.central.log.monitor;
|
package com.central.log.monitor;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日志埋点工具类
|
* 日志埋点工具类
|
||||||
*
|
*
|
||||||
* @author zlt
|
* @author zlt
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PointUtil {
|
public class PointUtil {
|
||||||
private static final String MSG_PATTERN = "{}|{}|{}";
|
private static final String MSG_PATTERN = "{}|{}|{}";
|
||||||
|
private static final String PROPERTIES_SPLIT = "&";
|
||||||
|
private static final String PROPERTIES_VALUE_SPLIT = "=";
|
||||||
|
|
||||||
|
private final PointEntry pointEntry;
|
||||||
|
|
||||||
private PointUtil() {
|
private PointUtil() {
|
||||||
throw new IllegalStateException("Utility class");
|
pointEntry = new PointEntry();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
|
private class PointEntry {
|
||||||
|
String id;
|
||||||
|
String type;
|
||||||
|
Object properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,4 +54,66 @@ public class PointUtil {
|
|||||||
public static void debug(String id, String type, String message) {
|
public static void debug(String id, String type, String message) {
|
||||||
log.debug(MSG_PATTERN, id, type, message);
|
log.debug(MSG_PATTERN, id, type, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static PointUtil builder() {
|
||||||
|
return new PointUtil();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param businessId 业务id/对象id
|
||||||
|
*/
|
||||||
|
public PointUtil id(Object businessId) {
|
||||||
|
this.pointEntry.setId(String.valueOf(businessId));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param type 类型
|
||||||
|
*/
|
||||||
|
public PointUtil type(String type) {
|
||||||
|
this.pointEntry.setType(type);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param properties 对象属性
|
||||||
|
*/
|
||||||
|
public PointUtil properties(Object properties) {
|
||||||
|
this.pointEntry.setProperties(properties);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getPropertiesStr() {
|
||||||
|
Object properties = this.pointEntry.getProperties();
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
if (!ObjectUtils.isEmpty(properties)) {
|
||||||
|
//解析map
|
||||||
|
if (properties instanceof Map) {
|
||||||
|
Map proMap = (Map)properties;
|
||||||
|
Iterator<Map.Entry> ite = proMap.entrySet().iterator();
|
||||||
|
while (ite.hasNext()) {
|
||||||
|
Map.Entry entry = ite.next();
|
||||||
|
if (result.length() > 0) {
|
||||||
|
result.append(PROPERTIES_SPLIT);
|
||||||
|
}
|
||||||
|
result.append(entry.getKey()).append(PROPERTIES_VALUE_SPLIT).append(entry.getValue());
|
||||||
|
}
|
||||||
|
} else {//解析对象
|
||||||
|
Field[] allFields = ReflectUtil.getFields(properties.getClass());
|
||||||
|
for (Field field : allFields) {
|
||||||
|
String fieldName = field.getName();
|
||||||
|
Object fieldValue = ReflectUtil.getFieldValue(properties, field);
|
||||||
|
if (result.length() > 0) {
|
||||||
|
result.append(PROPERTIES_SPLIT);
|
||||||
|
}
|
||||||
|
result.append(fieldName).append(PROPERTIES_VALUE_SPLIT).append(fieldValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void build() {
|
||||||
|
PointUtil.debug(this.pointEntry.getId(), this.pointEntry.getType(), this.getPropertiesStr());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -45,7 +45,7 @@ public class UserInfoHeaderFilter extends ZuulFilter {
|
|||||||
RequestContext ctx = RequestContext.getCurrentContext();
|
RequestContext ctx = RequestContext.getCurrentContext();
|
||||||
//客户端模式只返回一个clientId
|
//客户端模式只返回一个clientId
|
||||||
if (principal instanceof SysUser) {
|
if (principal instanceof SysUser) {
|
||||||
SysUser user = (SysUser)authentication.getPrincipal();
|
SysUser user = (SysUser)principal;
|
||||||
ctx.addZuulRequestHeader(SecurityConstants.USER_ID_HEADER, String.valueOf(user.getId()));
|
ctx.addZuulRequestHeader(SecurityConstants.USER_ID_HEADER, String.valueOf(user.getId()));
|
||||||
ctx.addZuulRequestHeader(SecurityConstants.USER_HEADER, user.getUsername());
|
ctx.addZuulRequestHeader(SecurityConstants.USER_HEADER, user.getUsername());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user