添加日志开关,修改加载配置bug

This commit is contained in:
shiyidong 2021-05-11 02:49:23 +08:00
parent d0a7c0593f
commit 57bcaba7f0
13 changed files with 160 additions and 25 deletions

View File

@ -1,13 +1,14 @@
package cn.iocoder.yudao.adminserver.modules.infra.controller.config;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.adminserver.modules.infra.controller.config.vo.*;
import cn.iocoder.yudao.adminserver.modules.infra.convert.config.InfConfigConvert;
import cn.iocoder.yudao.adminserver.modules.infra.dal.dataobject.config.InfConfigDO;
import cn.iocoder.yudao.adminserver.modules.infra.service.config.InfConfigService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@ -18,13 +19,14 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;
import static cn.iocoder.yudao.adminserver.modules.infra.enums.InfErrorCodeConstants.CONFIG_GET_VALUE_ERROR_IF_SENSITIVE;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.adminserver.modules.infra.enums.InfErrorCodeConstants.CONFIG_GET_VALUE_ERROR_IF_SENSITIVE;
@Api(tags = "参数配置")
@RestController
@ -67,6 +69,16 @@ public class InfConfigController {
return success(InfConfigConvert.INSTANCE.convert(configService.getConfig(id)));
}
@PutMapping("/update-value-by-key")
@ApiOperation(value = "根据参数键名修改参数值")
@PreAuthorize("@ss.hasPermission('infra:config:updateValueByKey')")
public CommonResult<Boolean> updateValueByKey(@NotBlank(message = "key 不允许为空") String key, String value) {
configService.updateValueByKey(key,value);
return success(true);
}
@GetMapping(value = "/get-value-by-key")
@ApiOperation(value = "根据参数键名查询参数值", notes = "敏感配置,不允许返回给前端")
@ApiImplicitParam(name = "key", value = "参数键", required = true, example = "yunai.biz.username", dataTypeClass = String.class)

View File

@ -1,11 +1,11 @@
package cn.iocoder.yudao.adminserver.modules.infra.dal.mysql.config;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.adminserver.modules.infra.controller.config.vo.InfConfigExportReqVO;
import cn.iocoder.yudao.adminserver.modules.infra.controller.config.vo.InfConfigPageReqVO;
import cn.iocoder.yudao.adminserver.modules.infra.dal.dataobject.config.InfConfigDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;

View File

@ -32,6 +32,13 @@ public interface InfConfigService {
*/
void updateConfig(@Valid InfConfigUpdateReqVO reqVO);
/**
* 根据key修改value
* @param key
* @param value
*/
void updateValueByKey(String key,String value);
/**
* 删除参数配置
*
@ -72,4 +79,6 @@ public interface InfConfigService {
List<InfConfigDO> getConfigList(@Valid InfConfigExportReqVO reqVO);
}

View File

@ -20,6 +20,7 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import static cn.iocoder.yudao.adminserver.modules.infra.enums.InfErrorCodeConstants.*;
@ -61,6 +62,20 @@ public class InfConfigServiceImpl implements InfConfigService {
configProducer.sendConfigRefreshMessage();
}
@Override
public void updateValueByKey(String key, String value) {
InfConfigDO config = configMapper.selectByKey(key);
if (Objects.isNull(config)) {
throw ServiceExceptionUtil.exception(CONFIG_NOT_EXISTS);
}
InfConfigDO updateDo = new InfConfigDO();
updateDo.setId(config.getId());
updateDo.setValue(value);
configMapper.updateById(updateDo);
// 发送刷新消息
configProducer.sendConfigRefreshMessage();
}
@Override
public void deleteConfig(Long id) {
// 校验配置存在

View File

@ -44,20 +44,20 @@ spring:
datasource:
master:
name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
url: jdbc:mysql://192.168.1.111:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
password: 851511
slave: # 模拟从库,可根据自己需要修改
name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
url: jdbc:mysql://192.168.1.111:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
password: 851511
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
redis:
host: 127.0.0.1 # 地址
host: 192.168.1.111 # 地址
port: 6379 # 端口
database: 0 # 数据库索引

View File

@ -43,6 +43,15 @@ export function updateConfig(data) {
})
}
// 修改参数配置
export function updateValueByKey(data) {
return request({
url: '/infra/config/update-value-by-key',
method: 'put',
data: data
})
}
// 删除参数配置
export function delConfig(configId) {
return request({

View File

@ -66,3 +66,12 @@ export const InfApiErrorLogProcessStatusEnum = {
DONE: 1, // 已处理
IGNORE: 2, // 已忽略
}
/**
*
*/
export const InfConfigKeyEnum = {
API_LOG_KEY: 'yudao.web.api.log.enable',
DONE: 1,
IGNORE: 2,
}

View File

@ -36,6 +36,10 @@
<!-- 操作工具栏 -->
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button :type="enable?'info':'primary'" plain :icon="enable?'el-icon-close':'el-icon-check'" size="mini" @click="enableHandler"
v-hasPermi="['infra:api-access-log:export']">{{enable?'关闭日志':'开启日志'}}</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
v-hasPermi="['infra:api-access-log:export']">导出</el-button>
@ -114,7 +118,8 @@
<script>
import { getApiAccessLogPage, exportApiAccessLogExcel } from "@/api/infra/apiAccessLog";
import { getConfigKey,updateValueByKey } from "@/api/infra/config";
import { InfConfigKeyEnum } from "@/utils/constants";
export default {
name: "ApiAccessLog",
components: {
@ -125,6 +130,8 @@ export default {
loading: true,
//
showSearch: true,
//
enable: false,
//
total: 0,
// API 访
@ -150,9 +157,25 @@ export default {
};
},
created() {
this.getEnableConfig();
this.getList();
},
methods: {
//
enableHandler(){
let data = new FormData();
data.append("key",InfConfigKeyEnum.API_LOG_KEY);
data.append("value",this.enable?false:true);
updateValueByKey(data).then(response => {
this.msgSuccess(this.enable?"关闭成功":"开启成功");
this.getEnableConfig();
})
},
getEnableConfig(){
getConfigKey(InfConfigKeyEnum.API_LOG_KEY).then(response => {
this.enable = (response.data == "true");
})
},
/** 查询列表 */
getList() {
this.loading = true;
@ -212,4 +235,4 @@ export default {
}
}
};
</script>
</script>

View File

@ -0,0 +1,28 @@
package cn.iocoder.yudao.framework.common.enums;
/**
* @author syidong@aliyun.com
* @date 2021/5/10 23:40
*/
public interface ConfigKeyConstants {
/**
* 是否开启apiLog key
*/
String YUDAO_WEB_API_LOG_ENABLE_KEY = "yudao.web.api.log.enable";
/**
* 是否开启api error log 日志 key
*/
String YUDAO_WEB_API_ERROR_LOG_ENABLE_KEY = "yudao.web.api.error.og.enable";
/**
* 是否开启operateLog key
*/
String YUDAO_OPERATE_LOG_KEY = "yudao.operateLog.enable";
/**
* spring value Key
*/
String SPRING_YUDAO_OPERATE_LOG_KEY = "${"+YUDAO_OPERATE_LOG_KEY+":true}";
}

View File

@ -4,6 +4,7 @@ import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.yudao.framework.common.enums.ConfigKeyConstants;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.operatelog.core.dto.OperateLogCreateReqDTO;
@ -21,6 +22,7 @@ import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -65,18 +67,30 @@ public class OperateLogAspect {
*/
private static final ThreadLocal<Map<String, Object>> EXTS = new ThreadLocal<>();
/**
* operateLog 开关
*/
@Value(ConfigKeyConstants.SPRING_YUDAO_OPERATE_LOG_KEY)
private Boolean isEnable;
@Resource
private OperateLogFrameworkService operateLogFrameworkService;
@Around("@annotation(apiOperation)")
public Object around(ProceedingJoinPoint joinPoint, ApiOperation apiOperation) throws Throwable {
// 可能也添加了 @ApiOperation 注解
if(!isEnable){
return null;
}
OperateLog operateLog = getMethodAnnotation(joinPoint, OperateLog.class);
return around0(joinPoint, operateLog, apiOperation);
}
@Around("!@annotation(io.swagger.annotations.ApiOperation) && @annotation(operateLog)") // 兼容处理只添加 @OperateLog 注解的情况
public Object around(ProceedingJoinPoint joinPoint, OperateLog operateLog) throws Throwable {
if(!isEnable){
return null;
}
return around0(joinPoint, operateLog, null);
}

View File

@ -135,7 +135,7 @@ public class DBConfigRepository extends AbstractConfigRepository {
private Properties buildProperties(List<ConfigRespDTO> configs) {
Properties properties = propertiesFactory.getPropertiesInstance();
configs.stream().filter(ConfigRespDTO::getDeleted) // 过滤掉被删除的配置
configs.stream().filter(item -> !item.getDeleted()) // 过滤掉被删除的配置
.forEach(config -> properties.put(config.getKey(), config.getValue()));
return properties;
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.framework.apollo.spring.boot;
import cn.iocoder.yudao.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.google.common.base.Strings;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;

View File

@ -2,17 +2,20 @@ package cn.iocoder.yudao.framework.apilog.core.filter;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService;
import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateDTO;
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
import cn.iocoder.yudao.framework.web.config.WebProperties;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.framework.common.enums.ConfigKeyConstants;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.framework.web.config.WebProperties;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.OncePerRequestFilter;
@ -25,6 +28,7 @@ import java.io.IOException;
import java.util.Date;
import java.util.Map;
/**
* API 访问日志 Filter
*
@ -57,15 +61,26 @@ public class ApiAccessLogFilter extends OncePerRequestFilter {
try {
// 继续过滤器
filterChain.doFilter(request, response);
// 正常执行记录日志
createApiAccessLog(request, beginTim, queryString, requestBody, null);
// api log 开关
if(isEnable(ConfigKeyConstants.YUDAO_WEB_API_LOG_ENABLE_KEY)){
// 正常执行记录日志
createApiAccessLog(request, beginTim, queryString, requestBody, null);
}
} catch (Exception ex) {
// 异常执行记录日志
createApiAccessLog(request, beginTim, queryString, requestBody, ex);
// api log异常日志开关
if(isEnable(ConfigKeyConstants.YUDAO_WEB_API_ERROR_LOG_ENABLE_KEY)){
// 异常执行记录日志
createApiAccessLog(request, beginTim, queryString, requestBody, ex);
}
throw ex;
}
}
private boolean isEnable(String key){
String enable = SpringUtil.getProperty(key);
return Boolean.parseBoolean(StrUtil.isBlank(enable)?Boolean.TRUE.toString():enable);
}
private void createApiAccessLog(HttpServletRequest request, Date beginTime,
Map<String, String> queryString, String requestBody, Exception ex) {
ApiAccessLogCreateDTO accessLog = new ApiAccessLogCreateDTO();