【7.1.6】【log】优化日志,增加一个全局开关和特殊标记的注解

pull/25/head
fengshuonan 2022-01-12 21:12:30 +08:00
parent d88039c274
commit 3cf3000177
4 changed files with 124 additions and 3 deletions

View File

@ -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.
*
* 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.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;
}

View File

@ -64,4 +64,9 @@ public interface LogFileConstants {
*/
Integer DEFAULT_API_LOG_AOP_SORT = 500;
/**
*
*/
Boolean DEFAULT_GLOBAL_LOG_FLAG = false;
}

View File

@ -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);
}
/**
*
*
* @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);
}
}

View File

@ -26,8 +26,10 @@ package cn.stylefeng.roses.kernel.log.requestapi;
import cn.hutool.core.util.ObjectUtil;
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.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.appender.AuthedLogAppender;
import cn.stylefeng.roses.kernel.log.api.factory.appender.HttpLogAppender;
@ -87,6 +89,12 @@ public class RequestApiLogRecordAop implements Ordered {
Object result = point.proceed();
try {
// 判断是否需要记录日志
boolean ensureMakeLog = this.ensureMakeLog(point);
if (!ensureMakeLog) {
return result;
}
if (ObjectUtil.isNotEmpty(result)) {
// 获取接口上@PostResource或者@GetResource的name属性和requiredLogin属性
Map<String, Object> annotationProp = getAnnotationProp(point);
@ -116,7 +124,7 @@ public class RequestApiLogRecordAop implements Ordered {
* @date 2020/12/22 21:18
*/
private Map<String, Object> getAnnotationProp(ProceedingJoinPoint joinPoint) {
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
// 通过map封装参数和参数值key参数名value是参数值
@ -231,7 +239,7 @@ public class RequestApiLogRecordAop implements Ordered {
// 解决上传文件接口aop记录日志报错
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 {
paramMap.put(parameterNames[i], args[i]);
}
@ -249,7 +257,7 @@ public class RequestApiLogRecordAop implements Ordered {
for (int i = 0; i < args.length; i++) {
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 {
paramMap.put("args" + i, args[i]);
}
@ -260,4 +268,57 @@ public class RequestApiLogRecordAop implements Ordered {
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;
}
}
}