mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-10 20:57:14 +08:00
新增:新增系统监控/登录日志功能,优化日志表结构,并新增记录错误信息(非未知异常不记录异常详情,只记录错误信息)
This commit is contained in:
@@ -110,7 +110,7 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录请求耗时及异常信息
|
||||
* 记录请求耗时及异常详情
|
||||
*
|
||||
* @return 系统日志信息
|
||||
*/
|
||||
@@ -123,11 +123,17 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
sysLog.setElapsedTime(System.currentTimeMillis() - LocalDateTimeUtil.toEpochMilli(sysLog.getCreateTime()));
|
||||
sysLog.setStatus(LogStatusEnum.SUCCESS);
|
||||
|
||||
// 记录异常信息
|
||||
// 记录错误信息(非未知异常不记录异常详情,只记录错误信息)
|
||||
String errorMsg = logContext.getErrorMsg();
|
||||
if (StrUtil.isNotBlank(errorMsg)) {
|
||||
sysLog.setStatus(LogStatusEnum.FAILURE);
|
||||
sysLog.setErrorMsg(errorMsg);
|
||||
}
|
||||
// 记录异常详情
|
||||
Exception exception = logContext.getException();
|
||||
if (exception != null) {
|
||||
sysLog.setStatus(LogStatusEnum.FAILURE);
|
||||
sysLog.setException(ExceptionUtil.stacktraceToString(exception, -1));
|
||||
sysLog.setExceptionDetail(ExceptionUtil.stacktraceToString(exception, -1));
|
||||
}
|
||||
return sysLog;
|
||||
}
|
||||
|
@@ -110,9 +110,14 @@ public class SysLog implements Serializable {
|
||||
private String browser;
|
||||
|
||||
/**
|
||||
* 异常
|
||||
* 错误信息
|
||||
*/
|
||||
private String exception;
|
||||
private String errorMsg;
|
||||
|
||||
/**
|
||||
* 异常详情
|
||||
*/
|
||||
private String exceptionDetail;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
|
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.cnadmin.monitor.model.query;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import org.springdoc.api.annotations.ParameterObject;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import top.charles7c.cnadmin.common.annotation.Query;
|
||||
|
||||
/**
|
||||
* 登录日志查询条件
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/16 23:25
|
||||
*/
|
||||
@Data
|
||||
@ParameterObject
|
||||
@Schema(description = "登录日志查询条件")
|
||||
public class LoginLogQuery implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 登录状态(1成功 2失败)
|
||||
*/
|
||||
@Schema(description = "登录状态(1成功 2失败)")
|
||||
@Query(type = Query.Type.EQUAL)
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 登录时间
|
||||
*/
|
||||
@Schema(description = "登录时间")
|
||||
@Query(type = Query.Type.BETWEEN)
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private List<Date> createTime;
|
||||
}
|
@@ -14,29 +14,32 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.monitor.service;
|
||||
package top.charles7c.cnadmin.monitor.model.vo;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.monitor.model.query.OperationLogQuery;
|
||||
import top.charles7c.cnadmin.monitor.model.vo.OperationLogVO;
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
/**
|
||||
* 操作日志业务接口
|
||||
* 基础日志信息
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/15 21:05
|
||||
* @since 2023/1/17 21:43
|
||||
*/
|
||||
public interface OperationLogService {
|
||||
@Data
|
||||
public class LogVO {
|
||||
|
||||
/**
|
||||
* 分页查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 分页信息
|
||||
* 创建人
|
||||
*/
|
||||
PageInfo<OperationLogVO> list(OperationLogQuery query, PageQuery pageQuery);
|
||||
@JsonIgnore
|
||||
private Long createUser;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@Schema(description = "创建人")
|
||||
private String createUserString;
|
||||
}
|
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.cnadmin.monitor.model.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import top.charles7c.cnadmin.monitor.enums.LogStatusEnum;
|
||||
|
||||
/**
|
||||
* 登录日志信息
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/16 23:19
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "登录日志信息")
|
||||
public class LoginLogVO extends LogVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 日志ID
|
||||
*/
|
||||
@Schema(description = "日志ID")
|
||||
private Long logId;
|
||||
|
||||
/**
|
||||
* 日志描述
|
||||
*/
|
||||
@Schema(description = "日志描述")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 操作状态(1成功 2失败)
|
||||
*/
|
||||
@Schema(description = "操作状态(1成功 2失败)", type = "Integer", allowableValues = {"1", "2"})
|
||||
private LogStatusEnum status;
|
||||
|
||||
/**
|
||||
* 登录IP
|
||||
*/
|
||||
@Schema(description = "登录IP")
|
||||
private String clientIp;
|
||||
|
||||
/**
|
||||
* 登录地点
|
||||
*/
|
||||
@Schema(description = "登录地点")
|
||||
private String location;
|
||||
|
||||
/**
|
||||
* 浏览器
|
||||
*/
|
||||
@Schema(description = "浏览器")
|
||||
private String browser;
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
@Schema(description = "错误信息")
|
||||
private String errorMsg;
|
||||
|
||||
/**
|
||||
* 登录时间
|
||||
*/
|
||||
@Schema(description = "登录时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
@@ -23,8 +23,6 @@ import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import top.charles7c.cnadmin.monitor.enums.LogStatusEnum;
|
||||
|
||||
/**
|
||||
@@ -35,7 +33,7 @@ import top.charles7c.cnadmin.monitor.enums.LogStatusEnum;
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "操作日志信息")
|
||||
public class OperationLogVO implements Serializable {
|
||||
public class OperationLogVO extends LogVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -76,16 +74,10 @@ public class OperationLogVO implements Serializable {
|
||||
private String browser;
|
||||
|
||||
/**
|
||||
* 操作人
|
||||
* 错误信息
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Long createUser;
|
||||
|
||||
/**
|
||||
* 操作人
|
||||
*/
|
||||
@Schema(description = "操作人")
|
||||
private String createUserString;
|
||||
@Schema(description = "错误信息")
|
||||
private String errorMsg;
|
||||
|
||||
/**
|
||||
* 操作时间
|
||||
|
@@ -16,6 +16,13 @@
|
||||
|
||||
package top.charles7c.cnadmin.monitor.service;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.monitor.model.query.LoginLogQuery;
|
||||
import top.charles7c.cnadmin.monitor.model.query.OperationLogQuery;
|
||||
import top.charles7c.cnadmin.monitor.model.vo.LoginLogVO;
|
||||
import top.charles7c.cnadmin.monitor.model.vo.OperationLogVO;
|
||||
|
||||
/**
|
||||
* 系统日志业务接口
|
||||
*
|
||||
@@ -24,4 +31,25 @@ package top.charles7c.cnadmin.monitor.service;
|
||||
*/
|
||||
public interface LogService {
|
||||
|
||||
/**
|
||||
* 分页查询操作日志列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 操作日志分页信息
|
||||
*/
|
||||
PageInfo<OperationLogVO> list(OperationLogQuery query, PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 分页查询登录日志列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 登录日志分页信息
|
||||
*/
|
||||
PageInfo<LoginLogVO> list(LoginLogQuery query, PageQuery pageQuery);
|
||||
}
|
||||
|
@@ -16,6 +16,10 @@
|
||||
|
||||
package top.charles7c.cnadmin.monitor.service.impl;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -23,9 +27,25 @@ import org.springframework.context.event.EventListener;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
import top.charles7c.cnadmin.common.util.ReflectUtils;
|
||||
import top.charles7c.cnadmin.common.util.helper.QueryHelper;
|
||||
import top.charles7c.cnadmin.monitor.mapper.LogMapper;
|
||||
import top.charles7c.cnadmin.monitor.model.entity.SysLog;
|
||||
import top.charles7c.cnadmin.monitor.model.query.LoginLogQuery;
|
||||
import top.charles7c.cnadmin.monitor.model.query.OperationLogQuery;
|
||||
import top.charles7c.cnadmin.monitor.model.vo.LogVO;
|
||||
import top.charles7c.cnadmin.monitor.model.vo.LoginLogVO;
|
||||
import top.charles7c.cnadmin.monitor.model.vo.OperationLogVO;
|
||||
import top.charles7c.cnadmin.monitor.service.LogService;
|
||||
import top.charles7c.cnadmin.system.service.UserService;
|
||||
|
||||
/**
|
||||
* 系统日志业务实现类
|
||||
@@ -39,10 +59,69 @@ import top.charles7c.cnadmin.monitor.service.LogService;
|
||||
public class LogServiceImpl implements LogService {
|
||||
|
||||
private final LogMapper logMapper;
|
||||
private final UserService userService;
|
||||
|
||||
@Async
|
||||
@EventListener
|
||||
public void save(SysLog sysLog) {
|
||||
logMapper.insert(sysLog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<OperationLogVO> list(OperationLogQuery query, PageQuery pageQuery) {
|
||||
QueryWrapper<SysLog> queryWrapper = QueryHelper.build(query);
|
||||
|
||||
// 限定查询信息
|
||||
String[] fieldsName = ReflectUtils.getNonStaticFieldsName(OperationLogVO.class);
|
||||
List<String> columns = Arrays.stream(fieldsName).map(StrUtil::toUnderlineCase)
|
||||
.filter(n -> !n.endsWith("string")).collect(Collectors.toList());
|
||||
queryWrapper.select(columns);
|
||||
|
||||
// 分页查询
|
||||
IPage<SysLog> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper);
|
||||
PageInfo<OperationLogVO> pageInfo = PageInfo.build(page, OperationLogVO.class);
|
||||
|
||||
// 填充数据(如果是查询个人操作日志,只查询一次用户信息即可)
|
||||
if (query.getUid() != null) {
|
||||
String nickname = ExceptionUtils.exToNull(() -> userService.getById(query.getUid()).getNickname());
|
||||
pageInfo.getList().forEach(o -> o.setCreateUserString(nickname));
|
||||
} else {
|
||||
pageInfo.getList().forEach(this::fill);
|
||||
}
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<LoginLogVO> list(LoginLogQuery query, PageQuery pageQuery) {
|
||||
QueryWrapper<SysLog> queryWrapper = QueryHelper.build(query);
|
||||
queryWrapper.and(qw -> qw.like("request_url", "/auth/login").or().like("request_url", "/auth/logout"));
|
||||
|
||||
// 限定查询信息
|
||||
String[] fieldsName = ReflectUtils.getNonStaticFieldsName(LoginLogVO.class);
|
||||
List<String> columns = Arrays.stream(fieldsName).map(StrUtil::toUnderlineCase)
|
||||
.filter(n -> !n.endsWith("string")).collect(Collectors.toList());
|
||||
queryWrapper.select(columns);
|
||||
|
||||
// 分页查询
|
||||
IPage<SysLog> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper);
|
||||
PageInfo<LoginLogVO> pageInfo = PageInfo.build(page, LoginLogVO.class);
|
||||
|
||||
// 填充数据
|
||||
pageInfo.getList().forEach(this::fill);
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充数据
|
||||
*
|
||||
* @param vo
|
||||
* VO
|
||||
*/
|
||||
private void fill(LogVO vo) {
|
||||
Long createUser = vo.getCreateUser();
|
||||
if (createUser == null) {
|
||||
return;
|
||||
}
|
||||
vo.setCreateUserString(ExceptionUtils.exToNull(() -> userService.getById(vo.getCreateUser())).getNickname());
|
||||
}
|
||||
}
|
||||
|
@@ -1,97 +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.cnadmin.monitor.service.impl;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.common.util.ReflectUtils;
|
||||
import top.charles7c.cnadmin.common.util.helper.QueryHelper;
|
||||
import top.charles7c.cnadmin.monitor.mapper.LogMapper;
|
||||
import top.charles7c.cnadmin.monitor.model.entity.SysLog;
|
||||
import top.charles7c.cnadmin.monitor.model.query.OperationLogQuery;
|
||||
import top.charles7c.cnadmin.monitor.model.vo.OperationLogVO;
|
||||
import top.charles7c.cnadmin.monitor.service.OperationLogService;
|
||||
import top.charles7c.cnadmin.system.mapper.UserMapper;
|
||||
import top.charles7c.cnadmin.system.model.entity.SysUser;
|
||||
|
||||
/**
|
||||
* 操作日志业务实现类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/15 21:05
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class OperationLogServiceImpl implements OperationLogService {
|
||||
|
||||
private final LogMapper logMapper;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public PageInfo<OperationLogVO> list(OperationLogQuery query, PageQuery pageQuery) {
|
||||
QueryWrapper<SysLog> queryWrapper = QueryHelper.build(query);
|
||||
|
||||
// 限定查询信息
|
||||
String[] fieldsName = ReflectUtils.getNonStaticFieldsName(OperationLogVO.class);
|
||||
List<String> columns = Arrays.stream(fieldsName).map(StrUtil::toUnderlineCase)
|
||||
.filter(n -> !n.endsWith("string")).collect(Collectors.toList());
|
||||
queryWrapper.select(columns);
|
||||
|
||||
// 分页查询
|
||||
IPage<SysLog> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper);
|
||||
PageInfo<OperationLogVO> pageInfo = PageInfo.build(page, OperationLogVO.class);
|
||||
|
||||
// 填充数据(如果是查询个人操作日志,只查询一次用户信息即可)
|
||||
if (query.getUid() != null) {
|
||||
SysUser sysUser = userMapper.selectById(query.getUid());
|
||||
pageInfo.getList().forEach(o -> o.setCreateUserString(sysUser.getUsername()));
|
||||
} else {
|
||||
pageInfo.getList().forEach(this::fill);
|
||||
}
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充数据
|
||||
*
|
||||
* @param vo
|
||||
* VO
|
||||
*/
|
||||
private void fill(OperationLogVO vo) {
|
||||
Long createUser = vo.getCreateUser();
|
||||
if (createUser == null) {
|
||||
return;
|
||||
}
|
||||
SysUser sysUser = userMapper.selectById(createUser);
|
||||
vo.setCreateUserString(sysUser.getUsername());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user