first commit
This commit is contained in:
31
fys-logging/src/main/java/com/fuyuanshen/annotation/Log.java
Normal file
31
fys-logging/src/main/java/com/fuyuanshen/annotation/Log.java
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Zheng Jie
|
||||
*
|
||||
* 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 com.fuyuanshen.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Log {
|
||||
String value() default "";
|
||||
}
|
102
fys-logging/src/main/java/com/fuyuanshen/aspect/LogAspect.java
Normal file
102
fys-logging/src/main/java/com/fuyuanshen/aspect/LogAspect.java
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Zheng Jie
|
||||
*
|
||||
* 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 com.fuyuanshen.aspect;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.fuyuanshen.domain.SysLog;
|
||||
import com.fuyuanshen.service.SysLogService;
|
||||
import com.fuyuanshen.utils.RequestHolder;
|
||||
import com.fuyuanshen.utils.SecurityUtils;
|
||||
import com.fuyuanshen.utils.StringUtils;
|
||||
import com.fuyuanshen.utils.ThrowableUtil;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterThrowing;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Component
|
||||
@Aspect
|
||||
@Slf4j
|
||||
public class LogAspect {
|
||||
|
||||
private final SysLogService sysLogService;
|
||||
|
||||
ThreadLocal<Long> currentTime = new ThreadLocal<>();
|
||||
|
||||
public LogAspect(SysLogService sysLogService) {
|
||||
this.sysLogService = sysLogService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置切入点
|
||||
*/
|
||||
@Pointcut("@annotation(com.fuyuanshen.annotation.Log)")
|
||||
public void logPointcut() {
|
||||
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置环绕通知,使用在方法logPointcut()上注册的切入点
|
||||
*
|
||||
* @param joinPoint join point for advice
|
||||
*/
|
||||
@Around("logPointcut()")
|
||||
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
Object result;
|
||||
currentTime.set(System.currentTimeMillis());
|
||||
result = joinPoint.proceed();
|
||||
SysLog sysLog = new SysLog("INFO",System.currentTimeMillis() - currentTime.get());
|
||||
currentTime.remove();
|
||||
HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
||||
sysLogService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request),joinPoint, sysLog);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置异常通知
|
||||
*
|
||||
* @param joinPoint join point for advice
|
||||
* @param e exception
|
||||
*/
|
||||
@AfterThrowing(pointcut = "logPointcut()", throwing = "e")
|
||||
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
|
||||
SysLog sysLog = new SysLog("ERROR",System.currentTimeMillis() - currentTime.get());
|
||||
currentTime.remove();
|
||||
sysLog.setExceptionDetail(new String(ThrowableUtil.getStackTrace(e).getBytes()));
|
||||
HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
||||
sysLogService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint)joinPoint, sysLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户名
|
||||
* @return /
|
||||
*/
|
||||
public String getUsername() {
|
||||
try {
|
||||
return SecurityUtils.getCurrentUsername();
|
||||
}catch (Exception e){
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
81
fys-logging/src/main/java/com/fuyuanshen/domain/SysLog.java
Normal file
81
fys-logging/src/main/java/com/fuyuanshen/domain/SysLog.java
Normal file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Zheng Jie
|
||||
*
|
||||
* 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 com.fuyuanshen.domain;
|
||||
|
||||
import com.alibaba.fastjson2.annotation.JSONField;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@TableName("sys_log")
|
||||
public class SysLog implements Serializable {
|
||||
|
||||
@TableId(value = "log_id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@ApiModelProperty(value = "操作用户")
|
||||
private String username;
|
||||
|
||||
@ApiModelProperty(value = "描述")
|
||||
private String description;
|
||||
|
||||
@ApiModelProperty(value = "方法名")
|
||||
private String method;
|
||||
|
||||
@ApiModelProperty(value = "参数")
|
||||
private String params;
|
||||
|
||||
@ApiModelProperty(value = "日志类型")
|
||||
private String logType;
|
||||
|
||||
@ApiModelProperty(value = "请求ip")
|
||||
private String requestIp;
|
||||
|
||||
@ApiModelProperty(value = "地址")
|
||||
private String address;
|
||||
|
||||
@ApiModelProperty(value = "浏览器")
|
||||
private String browser;
|
||||
|
||||
@ApiModelProperty(value = "请求耗时")
|
||||
private Long time;
|
||||
|
||||
@ApiModelProperty(value = "异常详细")
|
||||
@JSONField(serialize = false)
|
||||
private String exceptionDetail;
|
||||
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
@ApiModelProperty(value = "创建日期:yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Timestamp createTime;
|
||||
|
||||
public SysLog(String logType, Long time) {
|
||||
this.logType = logType;
|
||||
this.time = time;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Zheng Jie
|
||||
*
|
||||
* 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 com.fuyuanshen.domain.dto;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 日志查询类
|
||||
* @author Zheng Jie
|
||||
* @date 2019-6-4 09:23:07
|
||||
*/
|
||||
@Data
|
||||
public class SysLogQueryCriteria {
|
||||
|
||||
//test1
|
||||
@ApiModelProperty(value = "模糊查询")
|
||||
private String blurry;
|
||||
|
||||
@ApiModelProperty(value = "用户名称")
|
||||
private String username;
|
||||
|
||||
@ApiModelProperty(value = "日志类型")
|
||||
private String logType;
|
||||
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
private List<Timestamp> createTime;
|
||||
|
||||
@ApiModelProperty(value = "页码", example = "1")
|
||||
private Integer page = 1;
|
||||
|
||||
@ApiModelProperty(value = "每页数据量", example = "10")
|
||||
private Integer size = 10;
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Zheng Jie
|
||||
*
|
||||
* 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 com.fuyuanshen.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.fuyuanshen.domain.SysLog;
|
||||
import com.fuyuanshen.domain.dto.SysLogQueryCriteria;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @description
|
||||
* @date 2023-06-12
|
||||
**/
|
||||
@Mapper
|
||||
public interface SysLogMapper extends BaseMapper<SysLog> {
|
||||
|
||||
List<SysLog> queryAll(@Param("criteria") SysLogQueryCriteria criteria);
|
||||
|
||||
IPage<SysLog> queryAll(@Param("criteria") SysLogQueryCriteria criteria, Page<SysLog> page);
|
||||
|
||||
IPage<SysLog> queryAllByUser(@Param("criteria") SysLogQueryCriteria criteria, Page<SysLog> page);
|
||||
|
||||
String getExceptionDetails(@Param("id") Long id);
|
||||
|
||||
void deleteByLevel(@Param("logType") String logType);
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Zheng Jie
|
||||
*
|
||||
* 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 com.fuyuanshen.rest;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import com.fuyuanshen.annotation.Log;
|
||||
import com.fuyuanshen.domain.SysLog;
|
||||
import com.fuyuanshen.service.SysLogService;
|
||||
import com.fuyuanshen.domain.dto.SysLogQueryCriteria;
|
||||
import com.fuyuanshen.utils.PageResult;
|
||||
import com.fuyuanshen.utils.SecurityUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/logs")
|
||||
@Api(tags = "系统:日志管理")
|
||||
public class SysLogController {
|
||||
|
||||
private final SysLogService sysLogService;
|
||||
|
||||
@Log("导出数据")
|
||||
@ApiOperation("导出数据")
|
||||
@GetMapping(value = "/download")
|
||||
@PreAuthorize("@el.check()")
|
||||
public void exportLog(HttpServletResponse response, SysLogQueryCriteria criteria) throws IOException {
|
||||
criteria.setLogType("INFO");
|
||||
sysLogService.download(sysLogService.queryAll(criteria), response);
|
||||
}
|
||||
|
||||
@Log("导出错误数据")
|
||||
@ApiOperation("导出错误数据")
|
||||
@GetMapping(value = "/error/download")
|
||||
@PreAuthorize("@el.check()")
|
||||
public void exportErrorLog(HttpServletResponse response, SysLogQueryCriteria criteria) throws IOException {
|
||||
criteria.setLogType("ERROR");
|
||||
sysLogService.download(sysLogService.queryAll(criteria), response);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ApiOperation("日志查询")
|
||||
@PreAuthorize("@el.check()")
|
||||
public ResponseEntity<PageResult<SysLog>> queryLog(SysLogQueryCriteria criteria){
|
||||
criteria.setLogType("INFO");
|
||||
Page<SysLog> page = new Page<>(criteria.getPage(), criteria.getSize());
|
||||
return new ResponseEntity<>(sysLogService.queryAll(criteria,page), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/user")
|
||||
@ApiOperation("用户日志查询")
|
||||
public ResponseEntity<PageResult<SysLog>> queryUserLog(SysLogQueryCriteria criteria){
|
||||
criteria.setLogType("INFO");
|
||||
criteria.setUsername(SecurityUtils.getCurrentUsername());
|
||||
Page<SysLog> page = new Page<>(criteria.getPage(), criteria.getSize());
|
||||
return new ResponseEntity<>(sysLogService.queryAllByUser(criteria,page), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/error")
|
||||
@ApiOperation("错误日志查询")
|
||||
@PreAuthorize("@el.check()")
|
||||
public ResponseEntity<PageResult<SysLog>> queryErrorLog(SysLogQueryCriteria criteria){
|
||||
criteria.setLogType("ERROR");
|
||||
Page<SysLog> page = new Page<>(criteria.getPage(), criteria.getSize());
|
||||
return new ResponseEntity<>(sysLogService.queryAll(criteria,page), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/error/{id}")
|
||||
@ApiOperation("日志异常详情查询")
|
||||
@PreAuthorize("@el.check()")
|
||||
public ResponseEntity<Object> queryErrorLogDetail(@PathVariable Long id){
|
||||
return new ResponseEntity<>(sysLogService.findByErrDetail(id), HttpStatus.OK);
|
||||
}
|
||||
@DeleteMapping(value = "/del/error")
|
||||
@Log("删除所有ERROR日志")
|
||||
@ApiOperation("删除所有ERROR日志")
|
||||
@PreAuthorize("@el.check()")
|
||||
public ResponseEntity<Object> delAllErrorLog(){
|
||||
sysLogService.delAllByError();
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/del/info")
|
||||
@Log("删除所有INFO日志")
|
||||
@ApiOperation("删除所有INFO日志")
|
||||
@PreAuthorize("@el.check()")
|
||||
public ResponseEntity<Object> delAllInfoLog(){
|
||||
sysLogService.delAllByInfo();
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Zheng Jie
|
||||
*
|
||||
* 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 com.fuyuanshen.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.fuyuanshen.domain.SysLog;
|
||||
import com.fuyuanshen.domain.dto.SysLogQueryCriteria;
|
||||
import com.fuyuanshen.utils.PageResult;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
public interface SysLogService extends IService<SysLog>{
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
*
|
||||
* @param criteria 查询条件
|
||||
* @param page 分页参数
|
||||
* @return /
|
||||
*/
|
||||
PageResult<SysLog> queryAll(SysLogQueryCriteria criteria, Page<SysLog> page);
|
||||
|
||||
/**
|
||||
* 查询全部数据
|
||||
* @param criteria 查询条件
|
||||
* @return /
|
||||
*/
|
||||
List<SysLog> queryAll(SysLogQueryCriteria criteria);
|
||||
|
||||
/**
|
||||
* 查询用户日志
|
||||
* @param criteria 查询条件
|
||||
* @param page 分页参数
|
||||
* @return -
|
||||
*/
|
||||
PageResult<SysLog> queryAllByUser(SysLogQueryCriteria criteria, Page<SysLog> page);
|
||||
|
||||
/**
|
||||
* 保存日志数据
|
||||
* @param username 用户
|
||||
* @param browser 浏览器
|
||||
* @param ip 请求IP
|
||||
* @param joinPoint /
|
||||
* @param sysLog 日志实体
|
||||
*/
|
||||
void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, SysLog sysLog);
|
||||
|
||||
/**
|
||||
* 查询异常详情
|
||||
* @param id 日志ID
|
||||
* @return Object
|
||||
*/
|
||||
Object findByErrDetail(Long id);
|
||||
|
||||
/**
|
||||
* 导出日志
|
||||
* @param sysLogs 待导出的数据
|
||||
* @param response /
|
||||
* @throws IOException /
|
||||
*/
|
||||
void download(List<SysLog> sysLogs, HttpServletResponse response) throws IOException;
|
||||
|
||||
/**
|
||||
* 删除所有错误日志
|
||||
*/
|
||||
void delAllByError();
|
||||
|
||||
/**
|
||||
* 删除所有INFO日志
|
||||
*/
|
||||
void delAllByInfo();
|
||||
}
|
@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Copyright 2019-2025 Zheng Jie
|
||||
*
|
||||
* 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 com.fuyuanshen.service.impl;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.fuyuanshen.domain.SysLog;
|
||||
import com.fuyuanshen.mapper.SysLogMapper;
|
||||
import com.fuyuanshen.service.SysLogService;
|
||||
import com.fuyuanshen.domain.dto.SysLogQueryCriteria;
|
||||
import com.fuyuanshen.utils.*;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {
|
||||
|
||||
private final SysLogMapper sysLogMapper;
|
||||
// 定义敏感字段常量数组
|
||||
private static final String[] SENSITIVE_KEYS = {"password"};
|
||||
|
||||
@Override
|
||||
public PageResult<SysLog> queryAll(SysLogQueryCriteria criteria, Page<SysLog> page) {
|
||||
return PageUtil.toPage(sysLogMapper.queryAll(criteria, page));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysLog> queryAll(SysLogQueryCriteria criteria) {
|
||||
return sysLogMapper.queryAll(criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<SysLog> queryAllByUser(SysLogQueryCriteria criteria, Page<SysLog> page) {
|
||||
return PageUtil.toPage(sysLogMapper.queryAllByUser(criteria, page));
|
||||
}
|
||||
|
||||
@Async
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, SysLog sysLog) {
|
||||
if (sysLog == null) {
|
||||
throw new IllegalArgumentException("Log 不能为 null!");
|
||||
}
|
||||
|
||||
// 获取方法签名
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
com.fuyuanshen.annotation.Log aopLog = method.getAnnotation(com.fuyuanshen.annotation.Log.class);
|
||||
|
||||
// 方法路径
|
||||
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
|
||||
|
||||
// 获取参数
|
||||
JSONObject params = getParameter(method, joinPoint.getArgs());
|
||||
|
||||
// 填充基本信息
|
||||
sysLog.setRequestIp(ip);
|
||||
sysLog.setAddress(StringUtils.getCityInfo(sysLog.getRequestIp()));
|
||||
sysLog.setMethod(methodName);
|
||||
sysLog.setUsername(username);
|
||||
sysLog.setParams(JSON.toJSONString(params));
|
||||
sysLog.setBrowser(browser);
|
||||
sysLog.setDescription(aopLog.value());
|
||||
|
||||
// 如果没有获取到用户名,尝试从参数中获取
|
||||
if(StringUtils.isBlank(sysLog.getUsername())){
|
||||
sysLog.setUsername(params.getString("username"));
|
||||
}
|
||||
|
||||
// 保存
|
||||
save(sysLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据方法和传入的参数获取请求参数
|
||||
*/
|
||||
private JSONObject getParameter(Method method, Object[] args) {
|
||||
JSONObject params = new JSONObject();
|
||||
Parameter[] parameters = method.getParameters();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
// 过滤掉 MultiPartFile
|
||||
if (args[i] instanceof MultipartFile) {
|
||||
continue;
|
||||
}
|
||||
// 过滤掉 HttpServletResponse
|
||||
if (args[i] instanceof HttpServletResponse) {
|
||||
continue;
|
||||
}
|
||||
// 过滤掉 HttpServletRequest
|
||||
if (args[i] instanceof HttpServletRequest) {
|
||||
continue;
|
||||
}
|
||||
// 将RequestBody注解修饰的参数作为请求参数
|
||||
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
|
||||
if (requestBody != null) {
|
||||
try {
|
||||
params.putAll((JSONObject) JSON.toJSON(args[i]));
|
||||
} catch (Exception e) {
|
||||
log.error("参数转换异常:" + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
String key = parameters[i].getName();
|
||||
params.put(key, args[i]);
|
||||
}
|
||||
}
|
||||
// 遍历敏感字段数组并替换值
|
||||
Set<String> keys = params.keySet();
|
||||
for (String key : SENSITIVE_KEYS) {
|
||||
if (keys.contains(key)) {
|
||||
params.put(key, "******");
|
||||
}
|
||||
}
|
||||
// 返回参数
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object findByErrDetail(Long id) {
|
||||
String details = sysLogMapper.getExceptionDetails(id);
|
||||
return Dict.create().set("exception", details);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void download(List<SysLog> sysLogs, HttpServletResponse response) throws IOException {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
for (SysLog sysLog : sysLogs) {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("用户名", sysLog.getUsername());
|
||||
map.put("IP", sysLog.getRequestIp());
|
||||
map.put("IP来源", sysLog.getAddress());
|
||||
map.put("描述", sysLog.getDescription());
|
||||
map.put("浏览器", sysLog.getBrowser());
|
||||
map.put("请求耗时/毫秒", sysLog.getTime());
|
||||
map.put("异常详情", sysLog.getExceptionDetail());
|
||||
map.put("创建日期", sysLog.getCreateTime());
|
||||
list.add(map);
|
||||
}
|
||||
FileUtil.downloadExcel(list, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delAllByError() {
|
||||
// 删除 ERROR 级别的日志
|
||||
sysLogMapper.deleteByLevel("ERROR");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delAllByInfo() {
|
||||
// 删除 INFO 级别的日志
|
||||
sysLogMapper.deleteByLevel("INFO");
|
||||
}
|
||||
}
|
69
fys-logging/src/main/resources/mapper/SysLogMapper.xml
Normal file
69
fys-logging/src/main/resources/mapper/SysLogMapper.xml
Normal file
@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.fuyuanshen.mapper.SysLogMapper">
|
||||
|
||||
<sql id="info_column">
|
||||
log_id id,description,method,params,request_ip,time,username,address,browser,exception_detail,create_time
|
||||
</sql>
|
||||
|
||||
<sql id="error_column">
|
||||
log_id id,description,method,params,request_ip,username,address,browser,exception_detail,create_time
|
||||
</sql>
|
||||
|
||||
<sql id="user_column">
|
||||
log_id id,description,request_ip,time,address,browser,create_time
|
||||
</sql>
|
||||
|
||||
<sql id="query">
|
||||
from sys_log
|
||||
<where>
|
||||
<if test="criteria.blurry != null and criteria.blurry != ''">
|
||||
and (
|
||||
username like concat('%',#{criteria.blurry},'%')
|
||||
or description like concat('%',#{criteria.blurry},'%')
|
||||
or address like concat('%',#{criteria.blurry},'%')
|
||||
or request_ip like concat('%',#{criteria.blurry},'%')
|
||||
or method like concat('%',#{criteria.blurry},'%')
|
||||
or params like concat('%',#{criteria.blurry},'%')
|
||||
)
|
||||
</if>
|
||||
<if test="criteria.username != null and criteria.username != ''">
|
||||
and username like concat('%',#{criteria.username},'%')
|
||||
</if>
|
||||
<if test="criteria.logType != null and criteria.logType != ''">
|
||||
and log_type = #{criteria.logType}
|
||||
</if>
|
||||
<if test="criteria.createTime != null and criteria.createTime.size() > 0">
|
||||
and create_time between #{criteria.createTime[0]} and #{criteria.createTime[1]}
|
||||
</if>
|
||||
</where>
|
||||
order by log_id desc
|
||||
</sql>
|
||||
|
||||
<select id="queryAll" resultType="com.fuyuanshen.domain.SysLog">
|
||||
select
|
||||
<choose>
|
||||
<when test="criteria.logType == 'ERROR'">
|
||||
<include refid="error_column"/>
|
||||
</when>
|
||||
<otherwise>
|
||||
<include refid="info_column"/>
|
||||
</otherwise>
|
||||
</choose>
|
||||
<include refid="query"/>
|
||||
</select>
|
||||
|
||||
<select id="queryAllByUser" resultType="com.fuyuanshen.domain.SysLog">
|
||||
select
|
||||
<include refid="user_column"/>
|
||||
<include refid="query"/>
|
||||
</select>
|
||||
|
||||
<delete id="deleteByLevel">
|
||||
delete from sys_log where log_type = #{logType}
|
||||
</delete>
|
||||
|
||||
<select id="getExceptionDetails" resultType="java.lang.String">
|
||||
select exception_detail from sys_log where log_id = #{id}
|
||||
</select>
|
||||
</mapper>
|
Reference in New Issue
Block a user