From d79141c7a48859ef0a41d87e1c773163b1582ecc Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Tue, 30 Mar 2021 22:32:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E5=9F=8B?= =?UTF-8?q?=E7=82=B9=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zlt-log-spring-boot-starter/pom.xml | 5 ++ .../com/central/log/monitor/PointUtil.java | 87 ++++++++++++++++++- .../filter/pre/UserInfoHeaderFilter.java | 2 +- 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/zlt-commons/zlt-log-spring-boot-starter/pom.xml b/zlt-commons/zlt-log-spring-boot-starter/pom.xml index b256502..002fd93 100644 --- a/zlt-commons/zlt-log-spring-boot-starter/pom.xml +++ b/zlt-commons/zlt-log-spring-boot-starter/pom.xml @@ -42,6 +42,11 @@ org.springframework.boot spring-boot-starter-aop + + cn.hutool + hutool-all + + org.springframework spring-web diff --git a/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/monitor/PointUtil.java b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/monitor/PointUtil.java index 804a10c..d7a38fa 100644 --- a/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/monitor/PointUtil.java +++ b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/monitor/PointUtil.java @@ -1,18 +1,41 @@ package com.central.log.monitor; +import cn.hutool.core.util.ReflectUtil; +import lombok.Getter; +import lombok.Setter; 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 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Slf4j public class PointUtil { 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() { - 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) { 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 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()); + } } diff --git a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java index 731a907..ae832a0 100644 --- a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java +++ b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java @@ -45,7 +45,7 @@ public class UserInfoHeaderFilter extends ZuulFilter { RequestContext ctx = RequestContext.getCurrentContext(); //客户端模式只返回一个clientId 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_HEADER, user.getUsername()); } -- GitLab