mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-13 22:09:00 +08:00
✨ Introducing new features.#1908 日志功能新增时间过滤条件 区分租户隔离
✨ Introducing new features.#1908 日志功能新增时间过滤条件 区分租户隔离
This commit is contained in:
1 parent
cf1d4c8f06
commit
7dbe446bef
14 files changed
+159
-32
No files matched your search
+8
-1
@@ -10,10 +10,17 @@ public interface KeyStrResolver {
|
||||
|
||||
/**
|
||||
* 字符串加工
|
||||
* @param in 输入字符串
|
||||
*
|
||||
* @param in 输入字符串
|
||||
* @param split 分割符
|
||||
* @return 输出字符串
|
||||
*/
|
||||
String extract(String in, String split);
|
||||
|
||||
/**
|
||||
* 字符串获取
|
||||
*
|
||||
* @return 模块返回字符串
|
||||
*/
|
||||
String key();
|
||||
}
|
||||
+12
-1
@@ -13,7 +13,8 @@ public class TenantKeyStrResolver implements KeyStrResolver {
|
||||
|
||||
/**
|
||||
* 传入字符串增加 租户编号:in
|
||||
* @param in 输入字符串
|
||||
*
|
||||
* @param in 输入字符串
|
||||
* @param split 分割符
|
||||
* @return
|
||||
*/
|
||||
@@ -22,4 +23,14 @@ public class TenantKeyStrResolver implements KeyStrResolver {
|
||||
return TenantContextHolder.getTenantId() + split + in;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回当前租户ID
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String key() {
|
||||
return String.valueOf(TenantContextHolder.getTenantId());
|
||||
}
|
||||
|
||||
}
|
||||
+3
-2
@@ -20,6 +20,7 @@
|
||||
package com.pig4cloud.pigx.common.log;
|
||||
|
||||
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
|
||||
import com.pig4cloud.pigx.common.core.util.KeyStrResolver;
|
||||
import com.pig4cloud.pigx.common.log.aspect.SysLogAspect;
|
||||
import com.pig4cloud.pigx.common.log.event.SysLogListener;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -49,8 +50,8 @@ public class LogAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SysLogAspect sysLogAspect(ApplicationEventPublisher publisher) {
|
||||
return new SysLogAspect(publisher);
|
||||
public SysLogAspect sysLogAspect(ApplicationEventPublisher publisher, KeyStrResolver resolver) {
|
||||
return new SysLogAspect(publisher,resolver);
|
||||
}
|
||||
|
||||
}
|
||||
+12
-6
@@ -19,10 +19,12 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.log.aspect;
|
||||
|
||||
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
|
||||
import com.pig4cloud.pigx.common.core.util.KeyStrResolver;
|
||||
import com.pig4cloud.pigx.common.log.annotation.SysLog;
|
||||
import com.pig4cloud.pigx.common.log.event.SysLogEvent;
|
||||
import com.pig4cloud.pigx.common.log.util.SysLogUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
@@ -37,11 +39,14 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||
*/
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class SysLogAspect {
|
||||
|
||||
private final ApplicationEventPublisher publisher;
|
||||
|
||||
private final KeyStrResolver tenantKeyStrResolver;
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
@Around("@annotation(sysLog)")
|
||||
public Object around(ProceedingJoinPoint point, SysLog sysLog) {
|
||||
@@ -49,14 +54,15 @@ public class SysLogAspect {
|
||||
String strMethodName = point.getSignature().getName();
|
||||
log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);
|
||||
|
||||
com.pig4cloud.pigx.admin.api.entity.SysLog logVo = SysLogUtils.getSysLog();
|
||||
logVo.setTitle(sysLog.value());
|
||||
SysLogDTO logDTO = SysLogUtils.getSysLog();
|
||||
logDTO.setTitle(sysLog.value());
|
||||
// 发送异步日志事件
|
||||
Long startTime = System.currentTimeMillis();
|
||||
Object obj = point.proceed();
|
||||
Long endTime = System.currentTimeMillis();
|
||||
logVo.setTime(endTime - startTime);
|
||||
publisher.publishEvent(new SysLogEvent(logVo));
|
||||
logDTO.setTime(endTime - startTime);
|
||||
logDTO.setTenantId(Integer.parseInt(tenantKeyStrResolver.key()));
|
||||
publisher.publishEvent(new SysLogEvent(logDTO));
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -19,7 +19,7 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.log.event;
|
||||
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysLog;
|
||||
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@@ -30,6 +30,6 @@ import lombok.Getter;
|
||||
@AllArgsConstructor
|
||||
public class SysLogEvent {
|
||||
|
||||
private final SysLog sysLog;
|
||||
private final SysLogDTO sysLog;
|
||||
|
||||
}
|
||||
+2
-2
@@ -19,7 +19,7 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.log.event;
|
||||
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysLog;
|
||||
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
|
||||
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -41,7 +41,7 @@ public class SysLogListener {
|
||||
@Order
|
||||
@EventListener(SysLogEvent.class)
|
||||
public void saveSysLog(SysLogEvent event) {
|
||||
SysLog sysLog = event.getSysLog();
|
||||
SysLogDTO sysLog = event.getSysLog();
|
||||
remoteLogService.saveLog(sysLog, SecurityConstants.FROM_IN);
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -22,7 +22,7 @@ package com.pig4cloud.pigx.common.log.util;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysLog;
|
||||
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -42,10 +42,10 @@ import java.util.Objects;
|
||||
@UtilityClass
|
||||
public class SysLogUtils {
|
||||
|
||||
public SysLog getSysLog() {
|
||||
public SysLogDTO getSysLog() {
|
||||
HttpServletRequest request = ((ServletRequestAttributes) Objects
|
||||
.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
||||
SysLog sysLog = new SysLog();
|
||||
SysLogDTO sysLog = new SysLogDTO();
|
||||
sysLog.setCreateBy(Objects.requireNonNull(getUsername()));
|
||||
sysLog.setType(CommonConstants.STATUS_NORMAL);
|
||||
sysLog.setRemoteAddr(ServletUtil.getClientIP(request));
|
||||
@@ -57,8 +57,8 @@ public class SysLogUtils {
|
||||
return sysLog;
|
||||
}
|
||||
|
||||
public SysLog getSysLog(HttpServletRequest request, String username) {
|
||||
SysLog sysLog = new SysLog();
|
||||
public SysLogDTO getSysLog(HttpServletRequest request, String username) {
|
||||
SysLogDTO sysLog = new SysLogDTO();
|
||||
sysLog.setCreateBy(username);
|
||||
sysLog.setType(CommonConstants.STATUS_NORMAL);
|
||||
sysLog.setRemoteAddr(ServletUtil.getClientIP(request));
|
||||
|
||||
Reference in new issue
Block a user