Skip to content

Commit

Permalink
refactor: 💥 适配 ContiNew Starter Log(日志模块)
Browse files Browse the repository at this point in the history
1.continew-starter 1.0.1-SNAPSHOT => 1.1.0-SNAPSHOT
2.日志表结构及相关管理 UI 变更
  • Loading branch information
Charles7c committed Dec 17, 2023
1 parent 349899b commit 9bf0150
Show file tree
Hide file tree
Showing 35 changed files with 305 additions and 890 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public class SysConstants {
public static final String DEFAULT_PASSWORD = "123456";

/**
* 登录 URI
* 账号登录 URI
*/
public static final String LOGIN_URI = "/auth/login";
public static final String LOGIN_URI = "/auth/account";

/**
* 退出 URI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;

import top.charles7c.continew.admin.common.util.holder.LogContextHolder;
import top.charles7c.continew.starter.core.constant.StringConstants;
import top.charles7c.continew.starter.core.exception.BadRequestException;
import top.charles7c.continew.starter.core.exception.BusinessException;
Expand All @@ -65,7 +64,6 @@ public class GlobalExceptionHandler {
@ExceptionHandler(BadRequestException.class)
public R handleBadRequestException(BadRequestException e, HttpServletRequest request) {
log.warn("请求地址 [{}],自定义验证失败。", request.getRequestURI(), e);
LogContextHolder.setErrorMsg(e.getMessage());
return R.fail(HttpStatus.BAD_REQUEST.value(), e.getMessage());
}

Expand All @@ -77,7 +75,6 @@ public R constraintViolationException(ConstraintViolationException e, HttpServle
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
String errorMsg =
CollUtil.join(e.getConstraintViolations(), StringConstants.CHINESE_COMMA, ConstraintViolation::getMessage);
LogContextHolder.setErrorMsg(errorMsg);
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}

Expand All @@ -89,7 +86,6 @@ public R handleBindException(BindException e, HttpServletRequest request) {
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
String errorMsg = CollUtil.join(e.getAllErrors(), StringConstants.CHINESE_COMMA,
DefaultMessageSourceResolvable::getDefaultMessage);
LogContextHolder.setErrorMsg(errorMsg);
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}

Expand All @@ -101,7 +97,6 @@ public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
String errorMsg = ExceptionUtils
.exToNull(() -> Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
LogContextHolder.setErrorMsg(errorMsg);
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}

Expand All @@ -113,7 +108,6 @@ public R handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchExc
HttpServletRequest request) {
String errorMsg = StrUtil.format("参数名:[{}],期望参数类型:[{}]", e.getName(), e.getParameter().getParameterType());
log.warn("请求地址 [{}],参数转换失败,{}。", request.getRequestURI(), errorMsg, e);
LogContextHolder.setErrorMsg(errorMsg);
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}

Expand All @@ -125,7 +119,6 @@ public R handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e,
log.warn("请求地址 [{}],上传文件失败,文件大小超过限制。", request.getRequestURI(), e);
String sizeLimit = StrUtil.subBetween(e.getMessage(), "The maximum size ", " for");
String errorMsg = String.format("请上传小于 %sMB 的文件", NumberUtil.parseLong(sizeLimit) / 1024 / 1024);
LogContextHolder.setErrorMsg(errorMsg);
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}

Expand All @@ -140,7 +133,6 @@ public R handleNotLoginException(NotLoginException e, HttpServletRequest request
case NotLoginException.BE_REPLACED_MESSAGE -> "您已被顶下线。";
default -> "您的登录状态已过期,请重新登录。";
};
LogContextHolder.setErrorMsg(errorMsg);
return R.fail(HttpStatus.UNAUTHORIZED.value(), errorMsg);
}

Expand All @@ -167,7 +159,6 @@ public R handleNotRoleException(NotRoleException e, HttpServletRequest request)
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
LogContextHolder.setErrorMsg(e.getMessage());
log.error("请求地址 [{}],不支持 [{}] 请求。", request.getRequestURI(), e.getMethod());
return R.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage());
}
Expand All @@ -178,7 +169,6 @@ public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedExcept
@ExceptionHandler(BusinessException.class)
public R handleServiceException(BusinessException e, HttpServletRequest request) {
log.error("请求地址 [{}],发生业务异常。", request.getRequestURI(), e);
LogContextHolder.setErrorMsg(e.getMessage());
return R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
}

Expand All @@ -188,7 +178,6 @@ public R handleServiceException(BusinessException e, HttpServletRequest request)
@ExceptionHandler(RuntimeException.class)
public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
log.error("请求地址 [{}],发生系统异常。", request.getRequestURI(), e);
LogContextHolder.setException(e);
return R.fail(e.getMessage());
}

Expand All @@ -198,7 +187,6 @@ public R handleRuntimeException(RuntimeException e, HttpServletRequest request)
@ExceptionHandler(Throwable.class)
public R handleException(Throwable e, HttpServletRequest request) {
log.error("请求地址 [{}],发生未知异常。", request.getRequestURI(), e);
LogContextHolder.setException(e);
return R.fail(e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,25 @@ public class LoginUser implements Serializable {
private String token;

/**
* 登录 IP
* IP
*/
private String clientIp;
private String ip;

/**
* 登录地点
* IP 归属地
*/
private String location;
private String address;

/**
* 浏览器
*/
private String browser;

/**
* 操作系统
*/
private String os;

/**
* 登录时间
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.JakartaServletUtil;
import cn.hutool.extra.spring.SpringUtil;

import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.common.model.dto.LogContext;
import top.charles7c.continew.admin.common.model.dto.LoginUser;
import top.charles7c.continew.admin.common.util.holder.LogContextHolder;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
import top.charles7c.continew.starter.core.util.IpUtils;
import top.charles7c.continew.starter.core.util.ServletUtils;
Expand All @@ -58,11 +57,11 @@ public class LoginHelper {
public static String login(LoginUser loginUser) {
// 记录登录信息
HttpServletRequest request = ServletUtils.getRequest();
loginUser.setClientIp(JakartaServletUtil.getClientIP(request));
loginUser.setLocation(IpUtils.getCityInfo(loginUser.getClientIp()));
loginUser.setIp(JakartaServletUtil.getClientIP(request));
loginUser.setAddress(IpUtils.getAddress(loginUser.getIp()));
loginUser.setBrowser(ServletUtils.getBrowser(request));
LogContext logContext = LogContextHolder.get();
loginUser.setLoginTime(null != logContext ? logContext.getCreateTime() : LocalDateTime.now());
loginUser.setLoginTime(LocalDateTime.now());
loginUser.setOs(StrUtil.subBefore(ServletUtils.getOs(request), " or", false));
// 登录并缓存用户信息
StpUtil.login(loginUser.getId());
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_KEY, loginUser);
Expand Down

This file was deleted.

6 changes: 6 additions & 0 deletions continew-admin-monitor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
<description>系统监控模块(存放系统监控模块相关功能,例如:日志管理、服务监控等)</description>

<dependencies>
<!-- ContiNew Starter 日志模块 - HttpTracePro(Spring Boot Actuator HttpTrace 定制增强版) -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-log-httptrace-pro</artifactId>
</dependency>

<!-- 系统管理模块(存放系统管理模块相关功能,例如:部门管理、角色管理、用户管理等) -->
<dependency>
<groupId>top.charles7c.continew</groupId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,29 @@

package top.charles7c.continew.admin.monitor.config;

import lombok.RequiredArgsConstructor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import top.charles7c.continew.admin.monitor.interceptor.LogInterceptor;
import top.charles7c.continew.admin.monitor.mapper.LogMapper;
import top.charles7c.continew.admin.system.service.UserService;
import top.charles7c.continew.starter.log.common.dao.LogDao;
import top.charles7c.continew.starter.log.httptracepro.autoconfigure.ConditionalOnEnabledLog;

/**
* 监控模块 Web MVC 配置
* 日志配置
*
* @author Charles7c
* @since 2022/12/24 23:15
*/
@EnableWebMvc
@Configuration
@RequiredArgsConstructor
public class WebMvcMonitorConfiguration implements WebMvcConfigurer {

private final LogInterceptor logInterceptor;
@ConditionalOnEnabledLog
public class LogConfiguration {

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor);
/**
* 日志持久层接口本地实现类
*/
@Bean
public LogDao logDao(UserService userService, LogMapper logMapper) {
return new LogDaoLocalImpl(userService, logMapper);
}
}
Loading

0 comments on commit 9bf0150

Please sign in to comment.