【8.0】【log】整理logManagere日志

pull/57/head
fengshuonan 2023-07-21 13:42:51 +08:00
parent a24cc84564
commit 83327552a5
7 changed files with 55 additions and 250 deletions

View File

@ -28,8 +28,6 @@ import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.log.api.pojo.manage.LogManagerRequest;
import cn.stylefeng.roses.kernel.log.api.pojo.record.LogRecordDTO;
import java.util.List;
/**
*
* <p>
@ -41,42 +39,11 @@ import java.util.List;
public interface LogManagerApi {
/**
*
* api
*
* @param logManagerRequest
* @return
* @author fengshuonan
* @since 2020/10/28 11:27
* @since 2023/7/21 13:26
*/
List<LogRecordDTO> findList(LogManagerRequest logManagerRequest);
/**
*
*
* @param logManagerRequest
* @return
* @author luojie
* @since 2020/11/3 10:40
*/
PageResult<LogRecordDTO> findPage(LogManagerRequest logManagerRequest);
/**
*
* <p>
*
*
* @param logManagerRequest
* @author fengshuonan
* @since 2020/10/28 11:47
*/
void del(LogManagerRequest logManagerRequest);
/**
*
*
* @author chenjinlong
* @since 2021/2/1 19:47
*/
LogRecordDTO detail(LogManagerRequest logManagerRequest);
PageResult<LogRecordDTO> apiLogPageQuery(LogManagerRequest logManagerRequest);
}

View File

@ -1,99 +0,0 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* 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.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.log.requestapi;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.log.api.LogManagerApi;
import cn.stylefeng.roses.kernel.log.api.pojo.manage.LogManagerRequest;
import cn.stylefeng.roses.kernel.log.api.pojo.record.LogRecordDTO;
import cn.stylefeng.roses.kernel.log.requestapi.entity.SysLog;
import cn.stylefeng.roses.kernel.log.requestapi.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author luojie
* @since 2020/11/2 17:40
*/
@Slf4j
public class DbLogManagerServiceImpl implements LogManagerApi {
@Resource
private SysLogService sysLogService;
@Override
public List<LogRecordDTO> findList(LogManagerRequest logManagerRequest) {
List<SysLog> sysLogList = this.sysLogService.findList(logManagerRequest);
List<LogRecordDTO> logRecordDTOList = CollUtil.newArrayList();
BeanUtil.copyProperties(sysLogList, logRecordDTOList);
return logRecordDTOList;
}
@Override
public PageResult<LogRecordDTO> findPage(LogManagerRequest logManagerRequest) {
PageResult<SysLog> sysLogPageResult = this.sysLogService.findPage(logManagerRequest);
// 分页类型转换
PageResult<LogRecordDTO> logRecordDTOPageResult = new PageResult<>();
BeanUtil.copyProperties(sysLogPageResult, logRecordDTOPageResult);
// 转化数组
List<SysLog> rows = sysLogPageResult.getRows();
ArrayList<LogRecordDTO> newRows = new ArrayList<>();
for (SysLog row : rows) {
LogRecordDTO logRecordDTO = new LogRecordDTO();
BeanUtil.copyProperties(row, logRecordDTO);
// 设置请求和响应为空,减少带宽开销
logRecordDTO.setRequestParams(null);
logRecordDTO.setRequestResult(null);
newRows.add(logRecordDTO);
}
logRecordDTOPageResult.setRows(newRows);
return logRecordDTOPageResult;
}
@Override
public void del(LogManagerRequest logManagerRequest) {
this.sysLogService.del(logManagerRequest);
}
@Override
public LogRecordDTO detail(LogManagerRequest logManagerRequest) {
SysLog detail = this.sysLogService.detail(logManagerRequest);
LogRecordDTO logRecordDTO = new LogRecordDTO();
BeanUtil.copyProperties(detail, logRecordDTO);
return logRecordDTO;
}
}

View File

@ -25,11 +25,10 @@
package cn.stylefeng.roses.kernel.log.requestapi.controller;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.log.api.LogManagerApi;
import cn.stylefeng.roses.kernel.log.api.pojo.manage.LogManagerRequest;
import cn.stylefeng.roses.kernel.log.api.pojo.record.LogRecordDTO;
import cn.stylefeng.roses.kernel.log.requestapi.entity.SysLog;
import cn.stylefeng.roses.kernel.log.requestapi.service.SysLogService;
import cn.stylefeng.roses.kernel.rule.annotation.BusinessLog;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
@ -40,16 +39,15 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
*
* API
*
* @author luojie
* @since 2020/11/3 12:44
* @author fengshuonan
* @since 2023/7/21 11:22
*/
@RestController
@ApiResource(name = "日志管理控制器", requiredPermission = true, requirePermissionCode = LogManagerController.OPERATE_LOG)
@ApiResource(name = "API日志管理控制器", requiredPermission = true, requirePermissionCode = LogManagerController.OPERATE_LOG)
public class LogManagerController {
/**
@ -57,38 +55,18 @@ public class LogManagerController {
*/
public static final String OPERATE_LOG = "OPERATE_LOG";
/**
* api
*/
@Resource
private LogManagerApi logManagerApi;
/**
* service
*/
@Resource
private SysLogService sysLogService;
/**
*
* API
*
* @author luojie
* @since 2020/11/3 12:58
* @author fengshuonan
* @since 2023/7/21 11:34
*/
@GetResource(name = "查询日志列表", path = "/logManager/list")
public ResponseData<List<LogRecordDTO>> list(@RequestBody LogManagerRequest logManagerRequest) {
return new SuccessResponseData<>(logManagerApi.findList(logManagerRequest));
}
/**
*
*
* @author tengshuqi
* @since 2021/1/8 17:36
*/
@GetResource(name = "查询日志列表", path = "/logManager/page")
@GetResource(name = "分页查询API日志列表", path = "/logManager/page")
public ResponseData<PageResult<LogRecordDTO>> page(LogManagerRequest logManagerRequest) {
return new SuccessResponseData<>(logManagerApi.findPage(logManagerRequest));
return new SuccessResponseData<>(sysLogService.apiLogPageQuery(logManagerRequest));
}
/**
@ -98,7 +76,6 @@ public class LogManagerController {
* @since 2020/11/3 13:47
*/
@PostResource(name = "删除日志", path = "/logManager/delete")
@BusinessLog
public ResponseData<?> delete(@RequestBody @Validated(LogManagerRequest.delete.class) LogManagerRequest logManagerRequest) {
sysLogService.delAll(logManagerRequest);
return new SuccessResponseData<>();
@ -111,8 +88,8 @@ public class LogManagerController {
* @since 2021/1/11 17:36
*/
@GetResource(name = "查看日志详情", path = "/logManager/detail")
public ResponseData<LogRecordDTO> detail(@Validated(LogManagerRequest.detail.class) LogManagerRequest logManagerRequest) {
return new SuccessResponseData<>(logManagerApi.detail(logManagerRequest));
public ResponseData<SysLog> detail(@Validated(LogManagerRequest.detail.class) LogManagerRequest logManagerRequest) {
return new SuccessResponseData<>(sysLogService.detail(logManagerRequest));
}
}

View File

@ -24,20 +24,18 @@
*/
package cn.stylefeng.roses.kernel.log.requestapi.service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.log.api.LogManagerApi;
import cn.stylefeng.roses.kernel.log.api.pojo.manage.LogManagerRequest;
import cn.stylefeng.roses.kernel.log.requestapi.entity.SysLog;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* service
*
* @author luojie
* @since 2020/11/2 17:44
*/
public interface SysLogService extends IService<SysLog> {
public interface SysLogService extends IService<SysLog>, LogManagerApi {
/**
*
@ -48,15 +46,6 @@ public interface SysLogService extends IService<SysLog> {
*/
void add(LogManagerRequest logManagerRequest);
/**
*
*
* @param logManagerRequest
* @author chenjinlong
* @since 2021/1/26 12:52
*/
void del(LogManagerRequest logManagerRequest);
/**
*
*
@ -74,22 +63,4 @@ public interface SysLogService extends IService<SysLog> {
*/
SysLog detail(LogManagerRequest logManagerParam);
/**
* --
*
* @param logManagerParam
* @author chenjinlong
* @since 2021/1/26 12:52
*/
List<SysLog> findList(LogManagerRequest logManagerParam);
/**
* ---
*
* @param logManagerParam
* @author chenjinlong
* @since 2021/1/26 12:52
*/
PageResult<SysLog> findPage(LogManagerRequest logManagerParam);
}

View File

@ -25,15 +25,19 @@
package cn.stylefeng.roses.kernel.log.requestapi.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.log.api.exception.LogException;
import cn.stylefeng.roses.kernel.log.api.exception.enums.LogExceptionEnum;
import cn.stylefeng.roses.kernel.log.api.pojo.manage.LogManagerRequest;
import cn.stylefeng.roses.kernel.log.api.pojo.record.LogRecordDTO;
import cn.stylefeng.roses.kernel.log.requestapi.entity.SysLog;
import cn.stylefeng.roses.kernel.log.requestapi.mapper.SysLogMapper;
import cn.stylefeng.roses.kernel.log.requestapi.service.SysLogService;
@ -56,7 +60,6 @@ import java.util.List;
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {
@Override
public void add(LogManagerRequest logManagerRequest) {
SysLog sysLog = new SysLog();
@ -64,17 +67,12 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
this.save(sysLog);
}
@Override
public void del(LogManagerRequest logManagerRequest) {
SysLog sysLog = this.querySysLogById(logManagerRequest);
this.removeById(sysLog.getLogId());
}
@Override
public void delAll(LogManagerRequest logManagerRequest) {
LambdaUpdateWrapper<SysLog> queryWrapper = new LambdaUpdateWrapper<>();
queryWrapper.between(SysLog::getCreateTime, logManagerRequest.getBeginDate() + " 00:00:00", logManagerRequest.getEndDate() + " 23:59:59");
queryWrapper.between(SysLog::getCreateTime, logManagerRequest.getBeginDate() + " 00:00:00",
logManagerRequest.getEndDate() + " 23:59:59");
queryWrapper.eq(SysLog::getAppName, logManagerRequest.getAppName());
this.remove(queryWrapper);
@ -82,21 +80,25 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
@Override
public SysLog detail(LogManagerRequest logManagerRequest) {
LambdaQueryWrapper<SysLog> queryWrapper = this.createWrapper(logManagerRequest);
return this.getOne(queryWrapper, false);
return this.querySysLogById(logManagerRequest);
}
@Override
public List<SysLog> findList(LogManagerRequest logManagerRequest) {
LambdaQueryWrapper<SysLog> wrapper = this.createWrapper(logManagerRequest);
return this.list(wrapper);
}
public PageResult<LogRecordDTO> apiLogPageQuery(LogManagerRequest logManagerRequest) {
@Override
public PageResult<SysLog> findPage(LogManagerRequest logManagerRequest) {
LambdaQueryWrapper<SysLog> wrapper = createWrapper(logManagerRequest);
// 只查询需要字段
wrapper.select(SysLog::getLogId, SysLog::getRequestUrl, SysLog::getLogContent, SysLog::getUserId, SysLog::getAppName,
BaseEntity::getCreateTime);
// 转化实体
Page<SysLog> page = this.page(PageFactory.defaultPage(), wrapper);
return PageResultFactory.createPageResult(page);
List<SysLog> records = page.getRecords();
List<LogRecordDTO> logRecordDTOS = BeanUtil.copyToList(records, LogRecordDTO.class, CopyOptions.create().ignoreError());
return PageResultFactory.createPageResult(logRecordDTOS, page.getTotal(), Convert.toInt(page.getSize()),
Convert.toInt(page.getCurrent()));
}
/**
@ -132,6 +134,7 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
String beginDateTime = logManagerRequest.getBeginDate();
String endDateTime = logManagerRequest.getEndDate();
// 根据时间段查询
Date beginDate = null;
Date endDate = null;
if (StrUtil.isNotBlank(beginDateTime)) {
@ -140,25 +143,26 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
if (StrUtil.isNotBlank(endDateTime)) {
endDate = DateUtil.parseDateTime(endDateTime + " 23:59:59").toJdkDate();
}
// SQL条件拼接
String appName = logManagerRequest.getAppName();
String serverIp = logManagerRequest.getServerIp();
Long userId = logManagerRequest.getUserId();
String clientIp = logManagerRequest.getClientIp();
String url = logManagerRequest.getRequestUrl();
Long logId = logManagerRequest.getLogId();
queryWrapper.eq(ObjectUtil.isNotEmpty(logId), SysLog::getLogId, logId);
queryWrapper.between(ObjectUtil.isAllNotEmpty(beginDate, endDate), SysLog::getCreateTime, beginDate, endDate);
// 根据应用名称查询
String appName = logManagerRequest.getAppName();
queryWrapper.like(StrUtil.isNotEmpty(appName), SysLog::getAppName, appName);
queryWrapper.like(StrUtil.isNotEmpty(serverIp), SysLog::getServerIp, serverIp);
queryWrapper.eq(ObjectUtil.isNotNull(userId), SysLog::getUserId, userId);
queryWrapper.eq(StrUtil.isNotEmpty(clientIp), SysLog::getClientIp, clientIp);
queryWrapper.eq(StrUtil.isNotEmpty(url), SysLog::getRequestUrl, url);
// 根据内容查询
String searchText = logManagerRequest.getSearchText();
if (StrUtil.isNotEmpty(searchText)) {
queryWrapper.nested(wrap -> {
queryWrapper.likeRight(SysLog::getRequestUrl, searchText).or().likeRight(SysLog::getLogContent, searchText);
});
}
// 根据用户id查询
Long userId = logManagerRequest.getUserId();
if (ObjectUtil.isNotEmpty(userId)) {
queryWrapper.eq(SysLog::getUserId, userId);
}
return queryWrapper;
}
}

View File

@ -24,15 +24,12 @@
*/
package cn.stylefeng.roses.kernel.log.starter;
import cn.stylefeng.roses.kernel.log.api.LogManagerApi;
import cn.stylefeng.roses.kernel.log.api.LogRecordApi;
import cn.stylefeng.roses.kernel.log.api.threadpool.LogManagerThreadPool;
import cn.stylefeng.roses.kernel.log.requestapi.DbLogManagerServiceImpl;
import cn.stylefeng.roses.kernel.log.requestapi.DbLogRecordServiceImpl;
import cn.stylefeng.roses.kernel.log.requestapi.aop.RequestApiLogRecordAop;
import cn.stylefeng.roses.kernel.log.requestapi.service.SysLogService;
import cn.stylefeng.roses.kernel.log.requestapi.service.impl.SysLogServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -52,7 +49,6 @@ public class ProjectLogAutoConfiguration {
* @since 2020/12/28 22:09
*/
@Bean
@ConditionalOnMissingBean(SysLogService.class)
public SysLogService sysLogService() {
return new SysLogServiceImpl();
}
@ -71,17 +67,6 @@ public class ProjectLogAutoConfiguration {
return new RequestApiLogRecordAop(new DbLogRecordServiceImpl(new LogManagerThreadPool(), sysLogService));
}
/**
*
*
* @author liuhanqing
* @since 2020/12/20 18:53
*/
@Bean
public LogManagerApi logManagerApi() {
return new DbLogManagerServiceImpl();
}
/**
* api
*

View File

@ -41,7 +41,7 @@ public class HomeLogController {
logManagerRequest.setUserId(LoginContext.me().getLoginUser().getUserId());
// 默认查询20条记录
PageResult<LogRecordDTO> page = logManagerApi.findPage(logManagerRequest);
PageResult<LogRecordDTO> page = logManagerApi.apiLogPageQuery(logManagerRequest);
return new SuccessResponseData<>(page.getRows());
}