Commit 00b03e8d authored by yanzg's avatar yanzg

不记录系统日志

parent d000e9db
......@@ -51,6 +51,8 @@ public class BaseRequestAspect {
@Autowired
private LogLocal logLocal;
private ResponseResult responseDefault = new ResponseResult();
/**
* 开始记录日志对象
*
......@@ -163,10 +165,11 @@ public class BaseRequestAspect {
* @param joinPoint
* @return
*/
protected long requestLog(String tag, ProceedingJoinPoint joinPoint) {
protected long requestLog(String tag, boolean logFlag, ProceedingJoinPoint joinPoint) {
long start = System.currentTimeMillis();
try {
if (logCommon) {
boolean isLog = logFlag && logCommon;
if (isLog) {
String name = joinPoint.getSignature().getName();
Log.info(joinPoint.getSignature().getDeclaringType(), " %s [ %s ] request: %s",
tag, name, this.getMaxString(JsonHelper.serialize(getFirstDataParameter(joinPoint.getArgs()))));
......@@ -177,8 +180,6 @@ public class BaseRequestAspect {
return start;
}
private ResponseResult responseDefault = new ResponseResult();
/**
* 保存日志
*
......@@ -187,8 +188,10 @@ public class BaseRequestAspect {
* @param resultEx
* @param start
*/
protected void responseLog(LogVo log, boolean clear, String tag, ProceedingJoinPoint joinPoint, long start, Object result, Exception resultEx) {
protected void responseLog(LogVo log, String tag, boolean logFlag, ProceedingJoinPoint joinPoint, long start, Object result, Exception resultEx) {
try {
long time = System.currentTimeMillis() - start;
boolean isLog = (logFlag && logCommon) || resultEx != null;
String name = joinPoint.getSignature().getName();
// 处理结果
ResponseResult responseResult = responseDefault;
......@@ -197,23 +200,20 @@ public class BaseRequestAspect {
} else if (result != null) {
responseResult = ResponseResult.result(result);
}
long time = System.currentTimeMillis() - start;
// 执行时间
if (resultEx != null) {
responseResult = ExceptionHelper.getError(resultEx);
Log.error(joinPoint.getSignature().getDeclaringType(), resultEx, "%s [ %s ] time %d ms, result: %s",
tag, name, time, getMaxString(JsonHelper.serialize(responseResult)));
resultEx.printStackTrace();
} else if (logCommon) {
Log.info(joinPoint.getSignature().getDeclaringType(), "%s [ %s ] time %d ms, result: %s",
}
if (isLog) {
Log.error(joinPoint.getSignature().getDeclaringType(), resultEx, "%s [ %s ] time %d ms, result: %s",
tag, name, time, getMaxString(JsonHelper.serialize(responseResult)));
logLocal.result(log, name, responseResult.getCode(), JsonHelper.serialize(responseResult));
}
logLocal.result(log, name, responseResult.getCode(), JsonHelper.serialize(responseResult));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (clear) {
if (logFlag) {
Log.threadCommit();
}
}
......
......@@ -38,7 +38,7 @@ public class FeignAspect extends BaseRequestAspect {
@Around(value = "feignAspect()")
public Object requestFeignAround(ProceedingJoinPoint joinPoint) throws Throwable {
boolean clear = requestLogInit();
long start = requestLog(TAG, joinPoint);
long start = requestLog(TAG, clear, joinPoint);
LogVo log = startLog(TAG, getMethodUrl(joinPoint), getRequestBody(joinPoint));
Object result = null;
Exception ex = null;
......@@ -63,7 +63,7 @@ public class FeignAspect extends BaseRequestAspect {
ex = e;
throw e;
} finally {
responseLog(log, clear, TAG, joinPoint, start, result, ex);
responseLog(log, TAG, clear, joinPoint, start, result, ex);
}
}
......
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.mq.plan.YzgMqConsumer;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo;
......@@ -8,9 +9,10 @@ import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
......@@ -41,13 +43,15 @@ public class MqAspect extends BaseRequestAspect {
@Around(value = "mqAspect()")
public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
boolean clear = isRabbit(joinPoint);
LogVo log = null;
long start = System.currentTimeMillis();
if (clear) {
Log.threadBegin();
start = requestLog(TAG, clear, joinPoint);
log = startLog(TAG, getMethodUrl(joinPoint), getRequestBody(joinPoint));
} else {
clear = requestLogInit();
}
long start = requestLog(TAG, joinPoint);
LogVo log = startLog(TAG, getMethodUrl(joinPoint), getRequestBody(joinPoint));
Object result = null;
Exception ex = null;
try {
......@@ -59,26 +63,32 @@ public class MqAspect extends BaseRequestAspect {
ex = e;
throw e;
} finally {
responseLog(log, clear, TAG, joinPoint, start, result, ex);
responseLog(log, TAG, clear, joinPoint, start, result, ex);
}
}
private boolean isRabbit(ProceedingJoinPoint joinPoint) {
if (joinPoint.getSignature() instanceof MethodSignature) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
if ("YzgMqConsumer".equals(targetMethod.getDeclaringClass().getSimpleName())) {
return false;
boolean ret = false;
if (!(joinPoint.getSignature() instanceof MethodSignature)) {
return false;
}
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
if (YzgMqConsumer.class.equals(targetMethod.getDeclaringClass())) {
ret = false;
} else {
RabbitListener annotation = targetMethod.getAnnotation(RabbitListener.class);
if (annotation != null) {
ret = true;
}
Annotation[] annotations = targetMethod.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() != null
&& "RabbitListener".equals(annotation.annotationType().getSimpleName())) {
return true;
if (!ret) {
Message message = getMessage(joinPoint.getArgs());
if (message != null) {
ret = true;
}
}
}
return false;
return ret;
}
private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
......
......@@ -72,7 +72,7 @@ public class WebAspect extends BaseRequestAspect {
}
boolean clear = requestLogInit();
// 用户数据库记录
long start = requestLog(TAG, joinPoint);
long start = requestLog(TAG, clear, joinPoint);
LogVo log = startLog(TAG, HttpAspectUtil.getHttpRequestUrl(), getRequestBody(joinPoint));
Exception ex = null;
boolean isInit = false;
......@@ -115,7 +115,7 @@ public class WebAspect extends BaseRequestAspect {
if (isInit) {
TokenHelper.remove();
}
responseLog(log, clear, TAG, joinPoint, start, result, ex);
responseLog(log, TAG, clear, joinPoint, start, result, ex);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment