mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 04:39: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
+2
-2
@@ -17,7 +17,7 @@
|
||||
|
||||
package com.pig4cloud.pigx.auth.handler;
|
||||
|
||||
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.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
@@ -61,7 +61,7 @@ public class PigxAuthenticationFailureEventHandler implements AuthenticationFail
|
||||
public void handle(AuthenticationException authenticationException, Authentication authentication,
|
||||
HttpServletRequest request, HttpServletResponse response) {
|
||||
String username = authentication.getName();
|
||||
SysLog sysLog = SysLogUtils.getSysLog(request, username);
|
||||
SysLogDTO sysLog = SysLogUtils.getSysLog(request, username);
|
||||
sysLog.setTitle(username + "用户登录");
|
||||
sysLog.setType(CommonConstants.STATUS_LOCK);
|
||||
sysLog.setParams(username);
|
||||
|
||||
+2
-2
@@ -17,7 +17,7 @@
|
||||
|
||||
package com.pig4cloud.pigx.auth.handler;
|
||||
|
||||
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 com.pig4cloud.pigx.common.core.util.WebUtils;
|
||||
@@ -56,7 +56,7 @@ public class PigxAuthenticationSuccessEventHandler implements AuthenticationSucc
|
||||
@Override
|
||||
public void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
|
||||
String username = authentication.getName();
|
||||
SysLog sysLog = SysLogUtils.getSysLog(request, username);
|
||||
SysLogDTO sysLog = SysLogUtils.getSysLog(request, username);
|
||||
sysLog.setTitle(username + "用户登录");
|
||||
sysLog.setParams(username);
|
||||
String header = request.getHeader(HttpHeaders.AUTHORIZATION);
|
||||
|
||||
+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));
|
||||
|
||||
+70
-3
@@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
@@ -17,15 +18,81 @@ import java.time.LocalDateTime;
|
||||
public class SysLogDTO {
|
||||
|
||||
/**
|
||||
* 查询日志类型
|
||||
* 编号
|
||||
*/
|
||||
@ApiModelProperty(value = "日志类型")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 日志类型
|
||||
*/
|
||||
@NotBlank(message = "日志类型不能为空")
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 日志标题
|
||||
*/
|
||||
@NotBlank(message = "日志标题不能为空")
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 创建者
|
||||
*/
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
/**
|
||||
* 操作IP地址
|
||||
*/
|
||||
private String remoteAddr;
|
||||
|
||||
/**
|
||||
* 用户代理
|
||||
*/
|
||||
private String userAgent;
|
||||
|
||||
/**
|
||||
* 请求URI
|
||||
*/
|
||||
private String requestUri;
|
||||
|
||||
/**
|
||||
* 操作方式
|
||||
*/
|
||||
private String method;
|
||||
|
||||
/**
|
||||
* 操作提交的数据
|
||||
*/
|
||||
private String params;
|
||||
|
||||
/**
|
||||
* 执行时间
|
||||
*/
|
||||
private Long time;
|
||||
|
||||
/**
|
||||
* 异常信息
|
||||
*/
|
||||
private String exception;
|
||||
|
||||
/**
|
||||
* 服务ID
|
||||
*/
|
||||
private String serviceId;
|
||||
|
||||
|
||||
/**
|
||||
* 创建时间区间 [开始时间,结束时间]
|
||||
*/
|
||||
@ApiModelProperty(value = "创建时间区间")
|
||||
private LocalDateTime[] createTime;
|
||||
|
||||
/**
|
||||
* 租户编号
|
||||
*/
|
||||
private Integer tenantId;
|
||||
|
||||
}
|
||||
+2
-2
@@ -19,7 +19,7 @@
|
||||
|
||||
package com.pig4cloud.pigx.admin.api.feign;
|
||||
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysLog;
|
||||
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.constant.ServiceNameConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
@@ -42,6 +42,6 @@ public interface RemoteLogService {
|
||||
* @return succes、false
|
||||
*/
|
||||
@PostMapping("/log/save")
|
||||
R<Boolean> saveLog(@RequestBody SysLog sysLog, @RequestHeader(SecurityConstants.FROM) String from);
|
||||
R<Boolean> saveLog(@RequestBody SysLogDTO sysLog, @RequestHeader(SecurityConstants.FROM) String from);
|
||||
|
||||
}
|
||||
+7
-4
@@ -21,7 +21,6 @@ package com.pig4cloud.pigx.admin.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysLog;
|
||||
import com.pig4cloud.pigx.admin.api.vo.PreLogVO;
|
||||
import com.pig4cloud.pigx.admin.service.SysLogService;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
@@ -52,7 +51,8 @@ public class SysLogController {
|
||||
|
||||
/**
|
||||
* 简单分页查询
|
||||
* @param page 分页对象
|
||||
*
|
||||
* @param page 分页对象
|
||||
* @param sysLog 系统日志
|
||||
* @return
|
||||
*/
|
||||
@@ -63,6 +63,7 @@ public class SysLogController {
|
||||
|
||||
/**
|
||||
* 删除日志
|
||||
*
|
||||
* @param id ID
|
||||
* @return success/false
|
||||
*/
|
||||
@@ -74,17 +75,19 @@ public class SysLogController {
|
||||
|
||||
/**
|
||||
* 插入日志
|
||||
*
|
||||
* @param sysLog 日志实体
|
||||
* @return success/false
|
||||
*/
|
||||
@Inner
|
||||
@PostMapping("/save")
|
||||
public R save(@Valid @RequestBody SysLog sysLog) {
|
||||
return R.ok(sysLogService.save(sysLog));
|
||||
public R save(@Valid @RequestBody SysLogDTO sysLog) {
|
||||
return R.ok(sysLogService.saveLog(sysLog));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入前端异常日志
|
||||
*
|
||||
* @param preLogVoList 日志实体
|
||||
* @return success/false
|
||||
*/
|
||||
|
||||
+9
@@ -39,6 +39,7 @@ public interface SysLogService extends IService<SysLog> {
|
||||
|
||||
/**
|
||||
* 批量插入前端错误日志
|
||||
*
|
||||
* @param preLogVoList 日志信息
|
||||
* @return true/false
|
||||
*/
|
||||
@@ -46,10 +47,18 @@ public interface SysLogService extends IService<SysLog> {
|
||||
|
||||
/**
|
||||
* 分页查询日志
|
||||
*
|
||||
* @param page
|
||||
* @param sysLog
|
||||
* @return
|
||||
*/
|
||||
Page getLogByPage(Page page, SysLogDTO sysLog);
|
||||
|
||||
/**
|
||||
* 插入日志
|
||||
*
|
||||
* @param sysLog 日志对象
|
||||
* @return true/false
|
||||
*/
|
||||
Boolean saveLog(SysLogDTO sysLog);
|
||||
}
|
||||
+23
@@ -31,7 +31,11 @@ import com.pig4cloud.pigx.admin.api.vo.PreLogVO;
|
||||
import com.pig4cloud.pigx.admin.mapper.SysLogMapper;
|
||||
import com.pig4cloud.pigx.admin.service.SysLogService;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.data.tenant.TenantBroker;
|
||||
import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@@ -50,6 +54,7 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
|
||||
|
||||
/**
|
||||
* 批量插入前端错误日志
|
||||
*
|
||||
* @param preLogVoList 日志信息
|
||||
* @return true/false
|
||||
*/
|
||||
@@ -85,4 +90,22 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
|
||||
return baseMapper.selectPage(page, wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入日志
|
||||
*
|
||||
* @param sysLog 日志对象
|
||||
* @return true/false
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean saveLog(SysLogDTO sysLog) {
|
||||
TenantBroker.applyAs(sysLog::getTenantId, tenantId -> {
|
||||
TenantContextHolder.setTenantId(tenantId);
|
||||
SysLog log = new SysLog();
|
||||
BeanUtils.copyProperties(sysLog, log, "createTime");
|
||||
return baseMapper.insert(log);
|
||||
});
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user