logging模块完成改造
This commit is contained in:
@ -1,5 +1,9 @@
|
||||
package co.yixiang.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;
|
||||
@ -8,17 +12,15 @@ import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @author hupeng
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name = "log")
|
||||
@TableName("log")
|
||||
@NoArgsConstructor
|
||||
public class Log implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@TableId
|
||||
private Long id;
|
||||
|
||||
/** 操作用户 */
|
||||
@ -35,19 +37,15 @@ public class Log implements Serializable {
|
||||
private Integer type;
|
||||
|
||||
/** 参数 */
|
||||
@Column(columnDefinition = "text")
|
||||
private String params;
|
||||
|
||||
/** 日志类型 */
|
||||
@Column(name = "log_type")
|
||||
private String logType;
|
||||
|
||||
/** 请求ip */
|
||||
@Column(name = "request_ip")
|
||||
private String requestIp;
|
||||
|
||||
/** 地址 */
|
||||
@Column(name = "address")
|
||||
private String address;
|
||||
|
||||
/** 浏览器 */
|
||||
@ -57,12 +55,10 @@ public class Log implements Serializable {
|
||||
private Long time;
|
||||
|
||||
/** 异常详细 */
|
||||
@Column(name = "exception_detail", columnDefinition = "text")
|
||||
private byte[] exceptionDetail;
|
||||
|
||||
/** 创建日期 */
|
||||
@CreationTimestamp
|
||||
@Column(name = "create_time")
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Timestamp createTime;
|
||||
|
||||
public Log(String logType, Long time) {
|
||||
|
@ -1,50 +0,0 @@
|
||||
package co.yixiang.repository;
|
||||
|
||||
import co.yixiang.domain.Log;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Repository
|
||||
public interface LogRepository extends JpaRepository<Log,Long>, JpaSpecificationExecutor<Log> {
|
||||
|
||||
|
||||
|
||||
@Query(nativeQuery = true,
|
||||
value = "select l.id,l.create_time as createTime,l.description," +
|
||||
"l.request_ip as requestIp,l.address," +
|
||||
"u.nickname from log l left join yx_user u on u.uid=l.uid " +
|
||||
" where l.type=1" +
|
||||
" and if(?1 !='',u.nickname LIKE CONCAT('%',?1,'%'),1=1) order by l.id desc",
|
||||
countQuery = "select count(*) from log l left join yx_user u on u.uid=l.uid" +
|
||||
" where l.type=1 " +
|
||||
"and if(?1 !='',u.nickname LIKE CONCAT('%',?1,'%'),1=1)")
|
||||
Page<Map> findAllByPageable(String nickname,
|
||||
Pageable pageable);
|
||||
/**
|
||||
* 获取一个时间段的IP记录
|
||||
* @param date1 startTime
|
||||
* @param date2 entTime
|
||||
* @return IP数目
|
||||
*/
|
||||
@Query(value = "select count(*) FROM (select request_ip FROM log where create_time between ?1 and ?2 GROUP BY request_ip) as s",nativeQuery = true)
|
||||
Long findIp(String date1, String date2);
|
||||
|
||||
/**
|
||||
* 根据日志类型删除信息
|
||||
* @param logType 日志类型
|
||||
*/
|
||||
@Query(nativeQuery = true,value = "delete from log where log_type = ?1")
|
||||
@Modifying
|
||||
void deleteByLogType(String logType);
|
||||
}
|
@ -15,7 +15,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @author hupeng
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@RestController
|
||||
|
@ -1,5 +1,6 @@
|
||||
package co.yixiang.service;
|
||||
|
||||
import co.yixiang.common.service.BaseService;
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.service.dto.LogQueryCriteria;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
@ -14,7 +15,7 @@ import java.util.List;
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
public interface LogService {
|
||||
public interface LogService extends BaseService<Log> {
|
||||
|
||||
|
||||
|
||||
|
@ -5,7 +5,7 @@ import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @author hupeng
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Data
|
||||
@ -28,4 +28,4 @@ public class LogErrorDTO implements Serializable {
|
||||
private String address;
|
||||
|
||||
private Timestamp createTime;
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* 日志查询类
|
||||
* @author Zheng Jie
|
||||
* @author hupeng
|
||||
* @date 2019-6-4 09:23:07
|
||||
*/
|
||||
@Data
|
||||
|
@ -5,7 +5,7 @@ import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @author hupeng
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Data
|
||||
|
@ -3,13 +3,19 @@ package co.yixiang.service.impl;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import co.yixiang.common.service.impl.BaseServiceImpl;
|
||||
import co.yixiang.common.utils.QueryHelpPlus;
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.repository.LogRepository;
|
||||
import co.yixiang.dozer.service.IGenerator;
|
||||
import co.yixiang.service.LogService;
|
||||
import co.yixiang.service.dto.LogErrorDTO;
|
||||
import co.yixiang.service.dto.LogQueryCriteria;
|
||||
import co.yixiang.service.mapper.LogErrorMapper;
|
||||
import co.yixiang.service.mapper.LogSmallMapper;
|
||||
import co.yixiang.service.dto.LogSmallDTO;
|
||||
import co.yixiang.service.mapper.LogMapper;
|
||||
import co.yixiang.utils.*;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.Query;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.data.domain.Page;
|
||||
@ -32,49 +38,61 @@ import java.util.Map;
|
||||
*/
|
||||
@Service
|
||||
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
|
||||
public class LogServiceImpl implements LogService {
|
||||
public class LogServiceImpl extends BaseServiceImpl<LogMapper, Log> implements LogService {
|
||||
|
||||
private final LogRepository logRepository;
|
||||
|
||||
private final LogErrorMapper logErrorMapper;
|
||||
private final LogMapper logMapper;
|
||||
|
||||
private final LogSmallMapper logSmallMapper;
|
||||
private final IGenerator generator;
|
||||
|
||||
public LogServiceImpl(LogMapper logMapper, IGenerator generator) {
|
||||
this.logMapper = logMapper;
|
||||
this.generator = generator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object findAllByPageable(String nickname, Pageable pageable) {
|
||||
Page<Map> page = logRepository.findAllByPageable(nickname,pageable);
|
||||
getPage(pageable);
|
||||
List<Log> list = logMapper.findAllByPageable(nickname);
|
||||
PageInfo<Log> page = new PageInfo<>(list);
|
||||
|
||||
Map<String,Object> map = new LinkedHashMap<>(2);
|
||||
map.put("content",page.getContent());
|
||||
map.put("totalElements",page.getTotalElements());
|
||||
map.put("content",page.getList());
|
||||
map.put("totalElements",page.getTotal());
|
||||
return map;
|
||||
}
|
||||
|
||||
public LogServiceImpl(LogRepository logRepository, LogErrorMapper logErrorMapper, LogSmallMapper logSmallMapper) {
|
||||
this.logRepository = logRepository;
|
||||
this.logErrorMapper = logErrorMapper;
|
||||
this.logSmallMapper = logSmallMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object queryAll(LogQueryCriteria criteria, Pageable pageable){
|
||||
Page<Log> page = logRepository.findAll(((root, criteriaQuery, cb) -> QueryHelp.getPredicate(root, criteria, cb)),pageable);
|
||||
|
||||
getPage(pageable);
|
||||
PageInfo<Log> page = new PageInfo<>(queryAll(criteria));
|
||||
Map<String, Object> map = new LinkedHashMap<>(2);
|
||||
String status = "ERROR";
|
||||
if (status.equals(criteria.getLogType())) {
|
||||
return PageUtil.toPage(page.map(logErrorMapper::toDto));
|
||||
if(status.equals(criteria.getLogType())){
|
||||
map.put("content", generator.convert(page.getList(), LogErrorDTO.class));
|
||||
map.put("totalElements", page.getTotal());
|
||||
}
|
||||
return page;
|
||||
map.put("content", page.getList());
|
||||
map.put("totalElements", page.getTotal());
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Log> queryAll(LogQueryCriteria criteria) {
|
||||
return logRepository.findAll(((root, criteriaQuery, cb) -> QueryHelp.getPredicate(root, criteria, cb)));
|
||||
return baseMapper.selectList(QueryHelpPlus.getPredicate(Log.class, criteria));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object queryAllByUser(LogQueryCriteria criteria, Pageable pageable) {
|
||||
Page<Log> page = logRepository.findAll(((root, criteriaQuery, cb) -> QueryHelp.getPredicate(root, criteria, cb)),pageable);
|
||||
return PageUtil.toPage(page.map(logSmallMapper::toDto));
|
||||
getPage(pageable);
|
||||
PageInfo<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
|
||||
@ -124,12 +142,12 @@ public class LogServiceImpl implements LogService {
|
||||
log.setMethod(methodName);
|
||||
log.setUsername(username);
|
||||
log.setParams(params.toString() + " }");
|
||||
logRepository.save(log);
|
||||
this.save(log);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object findByErrDetail(Long id) {
|
||||
Log log = logRepository.findById(id).orElseGet(Log::new);
|
||||
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()));
|
||||
@ -156,12 +174,12 @@ public class LogServiceImpl implements LogService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delAllByError() {
|
||||
logRepository.deleteByLogType("ERROR");
|
||||
logMapper.deleteByLogType("ERROR");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delAllByInfo() {
|
||||
logRepository.deleteByLogType("INFO");
|
||||
logMapper.deleteByLogType("INFO");
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +0,0 @@
|
||||
package co.yixiang.service.mapper;
|
||||
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.service.dto.LogErrorDTO;
|
||||
import co.yixiang.base.BaseMapper;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
public interface LogErrorMapper extends BaseMapper<LogErrorDTO, Log> {
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package co.yixiang.service.mapper;
|
||||
|
||||
import co.yixiang.common.mapper.CoreMapper;
|
||||
import co.yixiang.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 Zheng Jie
|
||||
* @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);
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package co.yixiang.service.mapper;
|
||||
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.base.BaseMapper;
|
||||
import co.yixiang.service.dto.LogSmallDTO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
public interface LogSmallMapper extends BaseMapper<LogSmallDTO, Log> {
|
||||
|
||||
}
|
Reference in New Issue
Block a user