mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-09 20:57:21 +08:00
refactor: 💥 适配 ContiNew Starter Log(日志模块)
1.continew-starter 1.0.1-SNAPSHOT => 1.1.0-SNAPSHOT 2.日志表结构及相关管理 UI 变更
This commit is contained in:
@@ -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
|
||||
|
@@ -41,7 +41,6 @@ import cn.hutool.core.collection.CollUtil;
|
||||
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;
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -77,7 +75,6 @@ public class GlobalExceptionHandler {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -89,7 +86,6 @@ public class GlobalExceptionHandler {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -101,7 +97,6 @@ public class GlobalExceptionHandler {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -113,7 +108,6 @@ public class GlobalExceptionHandler {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -125,7 +119,6 @@ public class GlobalExceptionHandler {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -140,7 +133,6 @@ public class GlobalExceptionHandler {
|
||||
case NotLoginException.BE_REPLACED_MESSAGE -> "您已被顶下线。";
|
||||
default -> "您的登录状态已过期,请重新登录。";
|
||||
};
|
||||
LogContextHolder.setErrorMsg(errorMsg);
|
||||
return R.fail(HttpStatus.UNAUTHORIZED.value(), errorMsg);
|
||||
}
|
||||
|
||||
@@ -167,7 +159,6 @@ public class GlobalExceptionHandler {
|
||||
*/
|
||||
@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());
|
||||
}
|
||||
@@ -178,7 +169,6 @@ public class GlobalExceptionHandler {
|
||||
@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());
|
||||
}
|
||||
|
||||
@@ -188,7 +178,6 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
|
||||
log.error("请求地址 [{}],发生系统异常。", request.getRequestURI(), e);
|
||||
LogContextHolder.setException(e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
|
||||
@@ -198,7 +187,6 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(Throwable.class)
|
||||
public R handleException(Throwable e, HttpServletRequest request) {
|
||||
log.error("请求地址 [{}],发生未知异常。", request.getRequestURI(), e);
|
||||
LogContextHolder.setException(e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
}
|
@@ -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;
|
||||
|
||||
/**
|
||||
* 登录时间
|
||||
*/
|
||||
|
@@ -26,13 +26,12 @@ import lombok.NoArgsConstructor;
|
||||
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;
|
||||
@@ -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);
|
||||
|
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.continew.admin.common.util.holder;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import top.charles7c.continew.admin.common.model.dto.LogContext;
|
||||
|
||||
/**
|
||||
* 系统日志上下文持有者
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/25 8:55
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class LogContextHolder {
|
||||
|
||||
private static final ThreadLocal<LogContext> LOG_THREAD_LOCAL = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 存储系统日志上下文
|
||||
*
|
||||
* @param logContext
|
||||
* 系统日志上下文信息
|
||||
*/
|
||||
public static void set(LogContext logContext) {
|
||||
LOG_THREAD_LOCAL.set(logContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统日志上下文
|
||||
*
|
||||
* @return 系统日志上下文信息
|
||||
*/
|
||||
public static LogContext get() {
|
||||
return LOG_THREAD_LOCAL.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除系统日志上下文
|
||||
*/
|
||||
public static void remove() {
|
||||
LOG_THREAD_LOCAL.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 在系统日志上下文中保存异常信息
|
||||
*
|
||||
* @param e
|
||||
* 异常信息
|
||||
*/
|
||||
public static void setException(Throwable e) {
|
||||
LogContext logContext = get();
|
||||
if (null != logContext) {
|
||||
logContext.setErrorMsg(e.getMessage());
|
||||
logContext.setException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在系统日志上下文中保存错误信息(非未知异常不记录异常信息,只记录错误信息)
|
||||
*
|
||||
* @param errorMsg
|
||||
* 错误信息
|
||||
*/
|
||||
public static void setErrorMsg(String errorMsg) {
|
||||
LogContext logContext = get();
|
||||
if (null != logContext) {
|
||||
logContext.setErrorMsg(errorMsg);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user