Commit 00b03e8d authored by yanzg's avatar yanzg

不记录系统日志

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