mirror of https://gitee.com/stylefeng/roses
【7.1.6】【log】优化日志,增加一个全局开关和特殊标记的注解
parent
d88039c274
commit
3cf3000177
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* Guns采用APACHE 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.api.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用来标记在控制器类或方法上,进行判断是否需要对接口进行日志记录
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2022/1/12 20:48
|
||||||
|
*/
|
||||||
|
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface BusinessLog {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否进行日志记录,默认是开启
|
||||||
|
*/
|
||||||
|
boolean openLog() default true;
|
||||||
|
|
||||||
|
}
|
|
@ -64,4 +64,9 @@ public interface LogFileConstants {
|
||||||
*/
|
*/
|
||||||
Integer DEFAULT_API_LOG_AOP_SORT = 500;
|
Integer DEFAULT_API_LOG_AOP_SORT = 500;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认全局记录日志的开关
|
||||||
|
*/
|
||||||
|
Boolean DEFAULT_GLOBAL_LOG_FLAG = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,4 +59,14 @@ public class LogConfigExpander {
|
||||||
return ConfigContext.me().getSysConfigValueWithDefault("SYS_LOG_FILE_SAVE_PATH_LINUX", String.class, LogFileConstants.DEFAULT_FILE_SAVE_PATH_LINUX);
|
return ConfigContext.me().getSysConfigValueWithDefault("SYS_LOG_FILE_SAVE_PATH_LINUX", String.class, LogFileConstants.DEFAULT_FILE_SAVE_PATH_LINUX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局日志记录,如果开启则所有请求都将记录日志
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2022/1/12 20:32
|
||||||
|
*/
|
||||||
|
public static Boolean getGlobalControllerOpenFlag() {
|
||||||
|
return ConfigContext.me().getSysConfigValueWithDefault("SYS_LOG_GLOBAL_FLAG", Boolean.class, LogFileConstants.DEFAULT_GLOBAL_LOG_FLAG);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,10 @@ package cn.stylefeng.roses.kernel.log.requestapi;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.stylefeng.roses.kernel.log.api.LogRecordApi;
|
import cn.stylefeng.roses.kernel.log.api.LogRecordApi;
|
||||||
|
import cn.stylefeng.roses.kernel.log.api.annotation.BusinessLog;
|
||||||
import cn.stylefeng.roses.kernel.log.api.constants.LogConstants;
|
import cn.stylefeng.roses.kernel.log.api.constants.LogConstants;
|
||||||
import cn.stylefeng.roses.kernel.log.api.constants.LogFileConstants;
|
import cn.stylefeng.roses.kernel.log.api.constants.LogFileConstants;
|
||||||
|
import cn.stylefeng.roses.kernel.log.api.expander.LogConfigExpander;
|
||||||
import cn.stylefeng.roses.kernel.log.api.factory.LogRecordFactory;
|
import cn.stylefeng.roses.kernel.log.api.factory.LogRecordFactory;
|
||||||
import cn.stylefeng.roses.kernel.log.api.factory.appender.AuthedLogAppender;
|
import cn.stylefeng.roses.kernel.log.api.factory.appender.AuthedLogAppender;
|
||||||
import cn.stylefeng.roses.kernel.log.api.factory.appender.HttpLogAppender;
|
import cn.stylefeng.roses.kernel.log.api.factory.appender.HttpLogAppender;
|
||||||
|
@ -87,6 +89,12 @@ public class RequestApiLogRecordAop implements Ordered {
|
||||||
Object result = point.proceed();
|
Object result = point.proceed();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 判断是否需要记录日志
|
||||||
|
boolean ensureMakeLog = this.ensureMakeLog(point);
|
||||||
|
if (!ensureMakeLog) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (ObjectUtil.isNotEmpty(result)) {
|
if (ObjectUtil.isNotEmpty(result)) {
|
||||||
// 获取接口上@PostResource或者@GetResource的name属性和requiredLogin属性
|
// 获取接口上@PostResource或者@GetResource的name属性和requiredLogin属性
|
||||||
Map<String, Object> annotationProp = getAnnotationProp(point);
|
Map<String, Object> annotationProp = getAnnotationProp(point);
|
||||||
|
@ -116,7 +124,7 @@ public class RequestApiLogRecordAop implements Ordered {
|
||||||
* @date 2020/12/22 21:18
|
* @date 2020/12/22 21:18
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> getAnnotationProp(ProceedingJoinPoint joinPoint) {
|
private Map<String, Object> getAnnotationProp(ProceedingJoinPoint joinPoint) {
|
||||||
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
|
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
||||||
Method method = methodSignature.getMethod();
|
Method method = methodSignature.getMethod();
|
||||||
|
|
||||||
// 通过map封装参数和参数值,key参数名,value是参数值
|
// 通过map封装参数和参数值,key参数名,value是参数值
|
||||||
|
@ -231,7 +239,7 @@ public class RequestApiLogRecordAop implements Ordered {
|
||||||
// 解决上传文件接口aop记录日志报错
|
// 解决上传文件接口aop记录日志报错
|
||||||
if (args[i] instanceof MultipartFile && args[i] != null) {
|
if (args[i] instanceof MultipartFile && args[i] != null) {
|
||||||
// 根据参数名只记录文件名
|
// 根据参数名只记录文件名
|
||||||
paramMap.put(parameterNames[i], ((MultipartFile)args[i]).getOriginalFilename());
|
paramMap.put(parameterNames[i], ((MultipartFile) args[i]).getOriginalFilename());
|
||||||
} else {
|
} else {
|
||||||
paramMap.put(parameterNames[i], args[i]);
|
paramMap.put(parameterNames[i], args[i]);
|
||||||
}
|
}
|
||||||
|
@ -249,7 +257,7 @@ public class RequestApiLogRecordAop implements Ordered {
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
if (args[i] instanceof MultipartFile && args[i] != null) {
|
if (args[i] instanceof MultipartFile && args[i] != null) {
|
||||||
// 根据参数名只记录文件名
|
// 根据参数名只记录文件名
|
||||||
paramMap.put("args" + i, ((MultipartFile)args[i]).getOriginalFilename());
|
paramMap.put("args" + i, ((MultipartFile) args[i]).getOriginalFilename());
|
||||||
} else {
|
} else {
|
||||||
paramMap.put("args" + i, args[i]);
|
paramMap.put("args" + i, args[i]);
|
||||||
}
|
}
|
||||||
|
@ -260,4 +268,57 @@ public class RequestApiLogRecordAop implements Ordered {
|
||||||
return paramMap;
|
return paramMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 确定当前接口是否需要记录日志
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2022/1/12 20:44
|
||||||
|
*/
|
||||||
|
private boolean ensureMakeLog(ProceedingJoinPoint point) {
|
||||||
|
// 判断是否需要记录日志,如果不需要直接返回
|
||||||
|
Boolean openFlag = LogConfigExpander.getGlobalControllerOpenFlag();
|
||||||
|
|
||||||
|
// 获取类上的业务日志开关注解
|
||||||
|
Class<?> controllerClass = point.getTarget().getClass();
|
||||||
|
BusinessLog businessLog = controllerClass.getAnnotation(BusinessLog.class);
|
||||||
|
|
||||||
|
// 获取方法上的业务日志开关注解
|
||||||
|
BusinessLog methodBusinessLog = null;
|
||||||
|
MethodSignature methodSignature = null;
|
||||||
|
if (!(point.getSignature() instanceof MethodSignature)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
methodSignature = (MethodSignature) point.getSignature();
|
||||||
|
Object target = point.getTarget();
|
||||||
|
try {
|
||||||
|
Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
|
||||||
|
methodBusinessLog = currentMethod.getAnnotation(BusinessLog.class);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果开关开启
|
||||||
|
if (openFlag) {
|
||||||
|
// 如果控制器类上特意标明不做日志,则不记录日志
|
||||||
|
if (businessLog != null && !businessLog.openLog()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 如果方法上标明不记录日志,则不记录日志
|
||||||
|
if (methodBusinessLog != null && !methodBusinessLog.openLog()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
// 如果全局开关没开启,但是类上有特殊标记开启日志,则以类上标注为准
|
||||||
|
if (businessLog != null && businessLog.openLog()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// 如果方法上标明不记录日志,则不记录日志
|
||||||
|
if (methodBusinessLog != null && methodBusinessLog.openLog()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue