日志调整

This commit is contained in:
dengwc 2024-03-14 09:42:43 +08:00
parent 8e75f71dad
commit 3990be643d
6 changed files with 119 additions and 29 deletions

View File

@ -0,0 +1,12 @@
package com.chint.infrastructure.config.LogConfig.Dto;
import lombok.Data;
@Data
public class LogPageDto {
private Integer pageNum = 1;
private Integer pageSize = 10;
private String employeeNo;
private String url;
}

View File

@ -1,16 +1,16 @@
package com.chint.infrastructure.config.LogConfig;
import com.chint.application.dtos.LocationParam;
import com.chint.domain.aggregates.standards.TravelStandards;
import com.chint.infrastructure.config.LogConfig.Dto.LogPageDto;
import com.chint.infrastructure.constant.CommonMessageConstant;
import com.chint.infrastructure.util.PageResult;
import com.chint.infrastructure.util.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
/**
* 日志接口
@ -22,6 +22,25 @@ public class LogController {
@Autowired
private LogService logService;
/**
* 同步redis中的日志
*/
@PostMapping("/save")
@ApiOperation("保存日志")
public Result<Integer> saveLog() {
logService.saveLogs();
return Result.Success(CommonMessageConstant.SUCCESS);
}
/**
* 查看日志
*/
@PostMapping("/pageQuery")
@ApiOperation("分页查看日志信息")
public Result<PageResult<SystemLog>> queryLog(@RequestBody LogPageDto logPageDto) {
PageResult<SystemLog> systemLogPageResult = logService.pageQueryLog(logPageDto);
return Result.Success(CommonMessageConstant.SUCCESS, systemLogPageResult);
}
/**
* 清空日志

View File

@ -1,9 +1,15 @@
package com.chint.infrastructure.config.LogConfig;
import com.chint.domain.aggregates.order.Location;
import com.chint.infrastructure.config.LogConfig.Dto.LogPageDto;
import com.chint.infrastructure.repository.jdbc.JdbcSystemLogRepository;
import com.chint.infrastructure.util.PageResult;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
@ -68,27 +74,7 @@ public class LogService {
@Async
@Scheduled(cron = "0 0 3 * * *") // 每天凌晨3点执行一次
public void saveLog() {
int count = 0; // 初始化计数器
while (count <= 10000) { //当计数器小于10000时执行循环
long logNums = redisCache.getHashSize("SystemLog");
if (logNums <= 0L) {
break;//没有数据结束循环
}
//获取日志信息
Map<String, Object> systemLogMap = redisCache.getCacheHashValues("SystemLog", 100);
if (systemLogMap.isEmpty()) {
break; // 如果没有数据可取跳出循环
}
List<Object> logData = new ArrayList<>(systemLogMap.values());
Gson gson = new Gson();
List<SystemLog> systemLogs = logData.stream().map(log ->
gson.fromJson((String) log, SystemLog.class)
).toList();
jdbcSystemLogRepository.saveAll(systemLogs);
// 删除已处理的日志数据
redisCache.delCacheMapValue("SystemLog", systemLogMap.keySet());
count++;
}
saveLogs();
log.info("日志保存成功");
}
@ -116,6 +102,35 @@ public class LogService {
}
}
/**
* 保存redis可能剩余的日志信息
*/
@Transactional
public void saveLogs() {
int count = 0; // 初始化计数器
while (count <= 10000) { //当计数器小于10000时执行循环
long logNums = redisCache.getHashSize("SystemLog");
if (logNums <= 0L) {
break;//没有数据结束循环
}
//获取日志信息
Map<String, Object> systemLogMap = redisCache.getCacheHashValues("SystemLog", 100);
if (systemLogMap.isEmpty()) {
break; // 如果没有数据可取跳出循环
}
List<Object> logData = new ArrayList<>(systemLogMap.values());
Gson gson = new Gson();
List<SystemLog> systemLogs = logData.stream().map(log ->
gson.fromJson((String) log, SystemLog.class)
).toList();
jdbcSystemLogRepository.saveAll(systemLogs);
// 删除已处理的日志数据
redisCache.delCacheMapValue("SystemLog", systemLogMap.keySet());
count++;
}
}
/**
* 清空天日志
*/
@ -132,6 +147,35 @@ public class LogService {
return jdbcSystemLogRepository.deleteBatch((long) rows);
}
/**
* 根据员工分页查询日志
*/
public PageResult<SystemLog> pageQueryLog(LogPageDto logPageDto) {
PageRequest pageResult = PageRequest.of(
logPageDto.getPageNum() - 1,
logPageDto.getPageSize()
);
Page<SystemLog> res;
if (StringUtils.isNotBlank(logPageDto.getEmployeeNo()) && StringUtils.isNotBlank(logPageDto.getUrl())) {
String url = "%" + logPageDto.getUrl() + "%";
res = jdbcSystemLogRepository.findAllByEmployeeNoAndUrlLikeOrderByAccessTimeDesc(logPageDto.getEmployeeNo(), url, pageResult);
return PageResult.totalPageNum(res.getTotalElements(), res.toList());
}
if (StringUtils.isNotBlank(logPageDto.getEmployeeNo())) {
res = jdbcSystemLogRepository.findAllByEmployeeNoOrderByAccessTimeDesc(logPageDto.getEmployeeNo(), pageResult);
return PageResult.totalPageNum(res.getTotalElements(), res.toList());
}
if (StringUtils.isNotBlank(logPageDto.getUrl())) {
String url = "%" + logPageDto.getUrl() + "%";
res = jdbcSystemLogRepository.findAllByUrlLikeOrderByAccessTimeDesc(url, pageResult);
} else {
res = jdbcSystemLogRepository.findAllByOrderByAccessTimeDesc(pageResult);
}
return PageResult.totalPageNum(res.getTotalElements(), res.toList());
}
}

View File

@ -18,14 +18,15 @@ public class SystemLog {
@Id
private Integer id;
private String name;//员工姓名
private String employeeNo;//员工号
private String accessTime;//访问时间
private String clientIp;//IP
private String url;//请求路径
private String method;//请求方法
private String params;//url后面参数
private String requestBody;//请求体
private String responseBody;//响应数据
private String employeeNo;//员工号
private String name;//员工姓名
private String accessTime;//访问时间
}

View File

@ -31,7 +31,8 @@ public class WebConfig implements WebMvcConfigurer {
.excludePathPatterns("/public/**", "/login");
//调用bean
registry.addInterceptor(getMyRequestLoggingInterceptor())
.addPathPatterns("/**");
.addPathPatterns("/**")
.excludePathPatterns("/log/pageQuery", "/order/pageQuery", "/OrderDetail/query/page", "/order/query/**", "/location/**");
}
@Override

View File

@ -1,8 +1,12 @@
package com.chint.infrastructure.repository.jdbc;
import com.chint.domain.aggregates.location.CityEntity;
import com.chint.domain.aggregates.order.Location;
import com.chint.infrastructure.config.LogConfig.SystemLog;
import io.swagger.models.auth.In;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
@ -22,9 +26,18 @@ public interface JdbcSystemLogRepository extends CrudRepository<SystemLog, Long>
@Query(value = "DELETE sl FROM system_log sl JOIN (SELECT id FROM system_log ORDER BY access_time LIMIT :deleteCount) AS sub ON sl.id = sub.id")
Integer deleteBatch(@Param("deleteCount") long deleteCount);
//查看日志表大小
/*@Query(value = "SELECT round(((data_length + index_length) / 1024 / 1024), 2) AS size_in_mb FROM information_schema.TABLES WHERE table_schema = 'itinerary_booking' AND table_name = 'system_log'")
String getLogTableSizeInMB();*/
Page<SystemLog> findAllByEmployeeNoAndUrlLikeOrderByAccessTimeDesc(String employeeNo,String url, PageRequest pageResult);
Page<SystemLog> findAllByEmployeeNoOrderByAccessTimeDesc(String employeeNo, PageRequest pageResult);
Page<SystemLog> findAllByUrlLikeOrderByAccessTimeDesc(String url, PageRequest pageResult);
Page<SystemLog> findAllByOrderByAccessTimeDesc(PageRequest pageResult);
}