规范包名
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.aop.log;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @author hupeng
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Log {
|
||||
String value() default "";
|
||||
int type() default 0;
|
||||
}
|
||||
@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.aspect;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import co.yixiang.logging.domain.Log;
|
||||
import co.yixiang.logging.service.LogService;
|
||||
import co.yixiang.utils.RequestHolder;
|
||||
import co.yixiang.utils.SecurityUtils;
|
||||
import co.yixiang.utils.StringUtils;
|
||||
import co.yixiang.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 hupeng
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Component
|
||||
@Aspect
|
||||
@Slf4j
|
||||
public class LogAspect {
|
||||
|
||||
private final LogService logService;
|
||||
|
||||
ThreadLocal<Long> currentTime = new ThreadLocal<>();
|
||||
|
||||
public LogAspect(LogService logService) {
|
||||
this.logService = logService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置切入点
|
||||
*/
|
||||
@Pointcut("@annotation(co.yixiang.logging.aop.log.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();
|
||||
Log log = new Log("INFO",System.currentTimeMillis() - currentTime.get());
|
||||
currentTime.remove();
|
||||
HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
||||
logService.save(getUsername(),
|
||||
StringUtils.getIp(RequestHolder.getHttpServletRequest()),joinPoint,
|
||||
log,getUid());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置异常通知
|
||||
*
|
||||
* @param joinPoint join point for advice
|
||||
* @param e exception
|
||||
*/
|
||||
@AfterThrowing(pointcut = "logPointcut()", throwing = "e")
|
||||
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
|
||||
Log log = new Log("ERROR",System.currentTimeMillis() - currentTime.get());
|
||||
currentTime.remove();
|
||||
log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes());
|
||||
HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
||||
logService.save(getUsername(),
|
||||
StringUtils.getIp(RequestHolder.getHttpServletRequest()),
|
||||
(ProceedingJoinPoint)joinPoint, log,getUid());
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
try {
|
||||
return SecurityUtils.getUsername();
|
||||
}catch (Exception e){
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public Long getUid(){
|
||||
try {
|
||||
return SecurityUtils.getUserId();
|
||||
}catch (Exception e){
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author hupeng
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Data
|
||||
@TableName("log")
|
||||
@NoArgsConstructor
|
||||
public class Log implements Serializable {
|
||||
|
||||
@TableId
|
||||
private Long id;
|
||||
|
||||
/** 操作用户 */
|
||||
private String username;
|
||||
|
||||
/** 描述 */
|
||||
private String description;
|
||||
|
||||
/** 方法名 */
|
||||
private String method;
|
||||
|
||||
private Long uid;
|
||||
|
||||
private Integer type;
|
||||
|
||||
/** 参数 */
|
||||
private String params;
|
||||
|
||||
/** 日志类型 */
|
||||
private String logType;
|
||||
|
||||
/** 请求ip */
|
||||
private String requestIp;
|
||||
|
||||
/** 地址 */
|
||||
private String address;
|
||||
|
||||
/** 浏览器 */
|
||||
private String browser;
|
||||
|
||||
/** 请求耗时 */
|
||||
private Long time;
|
||||
|
||||
/** 异常详细 */
|
||||
private byte[] exceptionDetail;
|
||||
|
||||
/** 创建日期 */
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Timestamp createTime;
|
||||
|
||||
public Log(String logType, Long time) {
|
||||
this.logType = logType;
|
||||
this.time = time;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,112 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.rest;
|
||||
|
||||
import co.yixiang.logging.aop.log.Log;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import co.yixiang.logging.service.LogService;
|
||||
import co.yixiang.logging.service.dto.LogQueryCriteria;
|
||||
import co.yixiang.utils.SecurityUtils;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
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 hupeng
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/logs")
|
||||
@Api(tags = "监控:日志管理")
|
||||
@SuppressWarnings("unchecked")
|
||||
public class LogController {
|
||||
|
||||
private final LogService logService;
|
||||
|
||||
public LogController(LogService logService) {
|
||||
this.logService = logService;
|
||||
}
|
||||
|
||||
@Log("导出数据")
|
||||
@ApiOperation("导出数据")
|
||||
@GetMapping(value = "/download")
|
||||
@PreAuthorize("@el.check('admin','log:list')")
|
||||
public void download(HttpServletResponse response, LogQueryCriteria criteria) throws IOException {
|
||||
criteria.setLogType("INFO");
|
||||
logService.download(logService.queryAll(criteria), response);
|
||||
}
|
||||
|
||||
@Log("导出错误数据")
|
||||
@ApiOperation("导出错误数据")
|
||||
@GetMapping(value = "/error/download")
|
||||
@PreAuthorize("@el.check('admin','log:list')")
|
||||
public void errorDownload(HttpServletResponse response, LogQueryCriteria criteria) throws IOException {
|
||||
criteria.setLogType("ERROR");
|
||||
logService.download(logService.queryAll(criteria), response);
|
||||
}
|
||||
@GetMapping
|
||||
@ApiOperation("日志查询")
|
||||
@PreAuthorize("@el.check('admin','log:list')")
|
||||
public ResponseEntity<Object> getLogs(LogQueryCriteria criteria, Pageable pageable){
|
||||
criteria.setLogType("INFO");
|
||||
criteria.setType(0);
|
||||
return new ResponseEntity<>(logService.queryAll(criteria,pageable), HttpStatus.OK);
|
||||
}
|
||||
@GetMapping(value = "/mlogs")
|
||||
@PreAuthorize("@el.check('admin','log:list')")
|
||||
public ResponseEntity getApiLogs(LogQueryCriteria criteria, Pageable pageable){
|
||||
criteria.setLogType("INFO");
|
||||
criteria.setType(1);
|
||||
return new ResponseEntity(logService.findAllByPageable(criteria.getBlurry(),pageable), HttpStatus.OK);
|
||||
}
|
||||
@GetMapping(value = "/user")
|
||||
@ApiOperation("用户日志查询")
|
||||
public ResponseEntity<Object> getUserLogs(LogQueryCriteria criteria, Pageable pageable){
|
||||
criteria.setLogType("INFO");
|
||||
criteria.setBlurry(SecurityUtils.getUsername());
|
||||
return new ResponseEntity<>(logService.queryAllByUser(criteria,pageable), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/error")
|
||||
@ApiOperation("错误日志查询")
|
||||
@PreAuthorize("@el.check('admin','logError:list')")
|
||||
public ResponseEntity<Object> getErrorLogs(LogQueryCriteria criteria, Pageable pageable){
|
||||
criteria.setLogType("ERROR");
|
||||
return new ResponseEntity<>(logService.queryAll(criteria,pageable), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/error/{id}")
|
||||
@ApiOperation("日志异常详情查询")
|
||||
@PreAuthorize("@el.check('admin','logError:detail')")
|
||||
public ResponseEntity<Object> getErrorLogs(@PathVariable Long id){
|
||||
return new ResponseEntity<>(logService.findByErrDetail(id), HttpStatus.OK);
|
||||
}
|
||||
@DeleteMapping(value = "/del/error")
|
||||
@Log("删除所有ERROR日志")
|
||||
@ApiOperation("删除所有ERROR日志")
|
||||
@PreAuthorize("@el.check('admin','logError:remove')")
|
||||
public ResponseEntity<Object> delAllByError(){
|
||||
logService.delAllByError();
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/del/info")
|
||||
@Log("删除所有INFO日志")
|
||||
@ApiOperation("删除所有INFO日志")
|
||||
@PreAuthorize("@el.check('admin','logInfo:remove')")
|
||||
public ResponseEntity<Object> delAllByInfo(){
|
||||
logService.delAllByInfo();
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.service;
|
||||
|
||||
import co.yixiang.logging.domain.Log;
|
||||
import co.yixiang.common.service.BaseService;
|
||||
import co.yixiang.logging.service.dto.LogQueryCriteria;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hupeng
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
public interface LogService extends BaseService<Log> {
|
||||
|
||||
|
||||
|
||||
Object findAllByPageable(String nickname, Pageable pageable);
|
||||
/**
|
||||
* 分页查询
|
||||
* @param criteria 查询条件
|
||||
* @param pageable 分页参数
|
||||
* @return /
|
||||
*/
|
||||
Object queryAll(LogQueryCriteria criteria, Pageable pageable);
|
||||
|
||||
/**
|
||||
* 查询全部数据
|
||||
* @param criteria 查询条件
|
||||
* @return /
|
||||
*/
|
||||
List<Log> queryAll(LogQueryCriteria criteria);
|
||||
|
||||
/**
|
||||
* 查询用户日志
|
||||
* @param criteria 查询条件
|
||||
* @param pageable 分页参数
|
||||
* @return -
|
||||
*/
|
||||
Object queryAllByUser(LogQueryCriteria criteria, Pageable pageable);
|
||||
|
||||
/**
|
||||
* 保存日志数据
|
||||
* @param username 用户
|
||||
* @param browser 浏览器
|
||||
* @param ip 请求IP
|
||||
* @param joinPoint /
|
||||
* @param log 日志实体
|
||||
*/
|
||||
@Async
|
||||
void save(String username, String ip, ProceedingJoinPoint joinPoint, Log log,Long uid);
|
||||
|
||||
/**
|
||||
* 查询异常详情
|
||||
* @param id 日志ID
|
||||
* @return Object
|
||||
*/
|
||||
Object findByErrDetail(Long id);
|
||||
|
||||
/**
|
||||
* 导出日志
|
||||
* @param logs 待导出的数据
|
||||
* @param response /
|
||||
* @throws IOException /
|
||||
*/
|
||||
void download(List<Log> logs, HttpServletResponse response) throws IOException;
|
||||
|
||||
/**
|
||||
* 删除所有错误日志
|
||||
*/
|
||||
void delAllByError();
|
||||
|
||||
/**
|
||||
* 删除所有INFO日志
|
||||
*/
|
||||
void delAllByInfo();
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author hupeng
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Data
|
||||
public class LogErrorDTO implements Serializable {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String description;
|
||||
|
||||
private String method;
|
||||
|
||||
private String params;
|
||||
|
||||
private String browser;
|
||||
|
||||
private String requestIp;
|
||||
|
||||
private String address;
|
||||
|
||||
private Timestamp createTime;
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.service.dto;
|
||||
|
||||
import co.yixiang.annotation.Query;
|
||||
import lombok.Data;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 日志查询类
|
||||
* @author hupeng
|
||||
* @date 2019-6-4 09:23:07
|
||||
*/
|
||||
@Data
|
||||
public class LogQueryCriteria {
|
||||
|
||||
@Query(blurry = "username,description,address,requestIp,method,params")
|
||||
private String blurry;
|
||||
|
||||
@Query
|
||||
private String logType;
|
||||
|
||||
@Query(type = Query.Type.BETWEEN)
|
||||
private List<Timestamp> createTime;
|
||||
|
||||
|
||||
@Query
|
||||
private Integer type;
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author hupeng
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Data
|
||||
public class LogSmallDTO implements Serializable {
|
||||
|
||||
private String description;
|
||||
|
||||
private String requestIp;
|
||||
|
||||
private Long time;
|
||||
|
||||
private String address;
|
||||
|
||||
private String browser;
|
||||
|
||||
private Timestamp createTime;
|
||||
}
|
||||
@ -0,0 +1,190 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.service.impl;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import co.yixiang.logging.aop.log.Log;
|
||||
import co.yixiang.logging.service.mapper.LogMapper;
|
||||
import co.yixiang.common.service.impl.BaseServiceImpl;
|
||||
import co.yixiang.common.utils.QueryHelpPlus;
|
||||
import co.yixiang.dozer.service.IGenerator;
|
||||
import co.yixiang.logging.service.LogService;
|
||||
import co.yixiang.logging.service.dto.LogErrorDTO;
|
||||
import co.yixiang.logging.service.dto.LogQueryCriteria;
|
||||
import co.yixiang.logging.service.dto.LogSmallDTO;
|
||||
import co.yixiang.utils.*;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author hupeng
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Service
|
||||
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
|
||||
public class LogServiceImpl extends BaseServiceImpl<LogMapper, co.yixiang.logging.domain.Log> implements LogService {
|
||||
|
||||
|
||||
private final LogMapper logMapper;
|
||||
|
||||
private final IGenerator generator;
|
||||
|
||||
public LogServiceImpl(LogMapper logMapper, IGenerator generator) {
|
||||
this.logMapper = logMapper;
|
||||
this.generator = generator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object findAllByPageable(String nickname, Pageable pageable) {
|
||||
getPage(pageable);
|
||||
List<co.yixiang.logging.domain.Log> list = logMapper.findAllByPageable(nickname);
|
||||
PageInfo<co.yixiang.logging.domain.Log> page = new PageInfo<>(list);
|
||||
|
||||
Map<String,Object> map = new LinkedHashMap<>(2);
|
||||
map.put("content",page.getList());
|
||||
map.put("totalElements",page.getTotal());
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object queryAll(LogQueryCriteria criteria, Pageable pageable){
|
||||
|
||||
getPage(pageable);
|
||||
PageInfo<co.yixiang.logging.domain.Log> page = new PageInfo<>(queryAll(criteria));
|
||||
Map<String, Object> map = new LinkedHashMap<>(2);
|
||||
String status = "ERROR";
|
||||
if(status.equals(criteria.getLogType())){
|
||||
map.put("content", generator.convert(page.getList(), LogErrorDTO.class));
|
||||
map.put("totalElements", page.getTotal());
|
||||
}
|
||||
map.put("content", page.getList());
|
||||
map.put("totalElements", page.getTotal());
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<co.yixiang.logging.domain.Log> queryAll(LogQueryCriteria criteria) {
|
||||
return baseMapper.selectList(QueryHelpPlus.getPredicate(co.yixiang.logging.domain.Log.class, criteria));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object queryAllByUser(LogQueryCriteria criteria, Pageable pageable) {
|
||||
getPage(pageable);
|
||||
PageInfo<co.yixiang.logging.domain.Log> page = new PageInfo<>(queryAll(criteria));
|
||||
Map<String, Object> map = new LinkedHashMap<>(2);
|
||||
map.put("content", generator.convert(page.getList(), LogSmallDTO.class));
|
||||
map.put("totalElements", page.getTotal());
|
||||
return map;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void save(String username, String ip, ProceedingJoinPoint joinPoint,
|
||||
co.yixiang.logging.domain.Log log, Long uid){
|
||||
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
Log aopLog = method.getAnnotation(Log.class);
|
||||
|
||||
// 方法路径
|
||||
String methodName = joinPoint.getTarget().getClass().getName()+"."+signature.getName()+"()";
|
||||
|
||||
StringBuilder params = new StringBuilder("{");
|
||||
//参数值
|
||||
Object[] argValues = joinPoint.getArgs();
|
||||
//参数名称
|
||||
String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
|
||||
if(argValues != null){
|
||||
for (int i = 0; i < argValues.length; i++) {
|
||||
params.append(" ").append(argNames[i]).append(": ").append(argValues[i]);
|
||||
}
|
||||
}
|
||||
// 描述
|
||||
if (log != null) {
|
||||
log.setDescription(aopLog.value());
|
||||
}
|
||||
//类型 0-后台 1-前台
|
||||
log.setType(aopLog.type());
|
||||
if(uid != null) {
|
||||
log.setUid(uid);
|
||||
}
|
||||
assert log != null;
|
||||
log.setRequestIp(ip);
|
||||
|
||||
String loginPath = "login";
|
||||
if(loginPath.equals(signature.getName())){
|
||||
try {
|
||||
assert argValues != null;
|
||||
username = new JSONObject(argValues[0]).get("username").toString();
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
log.setAddress(StringUtils.getCityInfo(log.getRequestIp()));
|
||||
log.setMethod(methodName);
|
||||
log.setUsername(username);
|
||||
log.setParams(params.toString() + " }");
|
||||
this.save(log);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object findByErrDetail(Long id) {
|
||||
co.yixiang.logging.domain.Log log = this.getById(id);
|
||||
ValidationUtil.isNull( log.getId(),"Log","id", id);
|
||||
byte[] details = log.getExceptionDetail();
|
||||
return Dict.create().set("exception",new String(ObjectUtil.isNotNull(details) ? details : "".getBytes()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void download(List<co.yixiang.logging.domain.Log> logs, HttpServletResponse response) throws IOException {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
for (co.yixiang.logging.domain.Log log : logs) {
|
||||
Map<String,Object> map = new LinkedHashMap<>();
|
||||
map.put("用户名", log.getUsername());
|
||||
map.put("IP", log.getRequestIp());
|
||||
map.put("IP来源", log.getAddress());
|
||||
map.put("描述", log.getDescription());
|
||||
map.put("浏览器", log.getBrowser());
|
||||
map.put("请求耗时/毫秒", log.getTime());
|
||||
map.put("异常详情", new String(ObjectUtil.isNotNull(log.getExceptionDetail()) ? log.getExceptionDetail() : "".getBytes()));
|
||||
map.put("创建日期", log.getCreateTime());
|
||||
list.add(map);
|
||||
}
|
||||
FileUtil.downloadExcel(list, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delAllByError() {
|
||||
logMapper.deleteByLogType("ERROR");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delAllByInfo() {
|
||||
logMapper.deleteByLogType("INFO");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2019
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
* 注意:
|
||||
* 本软件为www.yixiang.co开发研制,未经购买不得使用
|
||||
* 购买后可获得全部源代码(禁止转卖、分享、上传到码云、github等开源平台)
|
||||
* 一经发现盗用、分享等行为,将追究法律责任,后果自负
|
||||
*/
|
||||
package co.yixiang.logging.service.mapper;
|
||||
|
||||
import co.yixiang.common.mapper.CoreMapper;
|
||||
import co.yixiang.logging.domain.Log;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hupeng
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Repository
|
||||
@Mapper
|
||||
public interface LogMapper extends CoreMapper<Log> {
|
||||
|
||||
@Delete("delete from log where log_type = #{logType}")
|
||||
void deleteByLogType(@Param("logType") String logType);
|
||||
@Select("\"select l.id,l.create_time as createTime,l.description,\" +\n" +
|
||||
" \"l.request_ip as requestIp,l.address,\" +\n" +
|
||||
" \"u.nickname from log l left join yx_user u on u.uid=l.uid \" +\n" +
|
||||
" \" where l.type=1\" +\n" +
|
||||
" \" and if(#{nickname} !='',u.nickname LIKE CONCAT('%',#{nickname},'%'),1=1) order by l.id desc\"")
|
||||
List<Log> findAllByPageable(@Param("nickname") String nickname);
|
||||
@Select( "select count(*) FROM (select request_ip FROM log where create_time between #{date1} and #{date2} GROUP BY request_ip) as s")
|
||||
long findIp(@Param("date1") String date1, @Param("date2")String date2);
|
||||
}
|
||||
Reference in New Issue
Block a user