Commit 58a3be11 authored by 于桐's avatar 于桐

日志切面调整

parent 29842182
...@@ -110,3 +110,4 @@ dump.rdb ...@@ -110,3 +110,4 @@ dump.rdb
transaction-logs/ transaction-logs/
.settings/ .settings/
/bin/
package cn.quantgroup.xyqb.aspect.logcaller; package cn.quantgroup.xyqb.aspect.logcaller;
import cn.quantgroup.xyqb.util.IpUtil; import java.util.concurrent.TimeUnit;
import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j; import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; 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.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.concurrent.TimeUnit; import com.google.common.base.Stopwatch;
import cn.quantgroup.xyqb.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
/** /**
* 调用者记录 * 调用者记录
...@@ -28,31 +31,35 @@ import java.util.concurrent.TimeUnit; ...@@ -28,31 +31,35 @@ import java.util.concurrent.TimeUnit;
@Order(value = Ordered.HIGHEST_PRECEDENCE) @Order(value = Ordered.HIGHEST_PRECEDENCE)
public class LogCallHttpAspect { public class LogCallHttpAspect {
@Pointcut(value = "execution(public * cn.quantgroup.xyqb.controller..*.*(..)) " + @Autowired
"&& !execution(* cn.quantgroup.xyqb.controller.ExceptionHandlingController.*(..))") private ObjectMapper objectMapper;
private void logHttpCaller() {
} @Pointcut(value = "execution(public * cn.quantgroup.xyqb.controller..*.*(..)) "
+ "&& !execution(* cn.quantgroup.xyqb.controller.ExceptionHandlingController.*(..))")
@Around("logHttpCaller()") private void logHttpCaller() {
public Object record(ProceedingJoinPoint pjp) throws Throwable { }
Stopwatch stopwatch = Stopwatch.createStarted();
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); @Around("logHttpCaller()")
HttpServletRequest request = attrs.getRequest(); public Object record(ProceedingJoinPoint pjp) throws Throwable {
String remoteIP = IpUtil.getRemoteIP(request); Stopwatch stopwatch = Stopwatch.createStarted();
Object[] args = pjp.getArgs(); ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
boolean hasException = false; HttpServletRequest request = attrs.getRequest();
Object result; String remoteIP = IpUtil.getRemoteIP(request);
try { Object[] args = pjp.getArgs();
result = pjp.proceed(); boolean hasException = false;
} catch (Throwable e) { Object result = null;
hasException = true; try {
throw e; result = pjp.proceed();
} finally { } catch (Throwable e) {
Stopwatch stop = stopwatch.stop(); hasException = true;
long elapsed = stop.elapsed(TimeUnit.MILLISECONDS); throw e;
log.info("[httpRequestLog],url:[{}],remoteIP:[{}],args:[{}],duration:[{}],exception:[{}]", } finally {
request.getRequestURL(), remoteIP, args, elapsed, hasException); Stopwatch stop = stopwatch.stop();
} long elapsed = stop.elapsed(TimeUnit.MILLISECONDS);
return result; String resultStr = result == null ? "" : objectMapper.writeValueAsString(result).substring(0, 500);
} log.info("[httpRequestLog],url:[{}],remoteIP:[{}],args:[{}],duration:[{}],exception:[{}],result:[{}]",
request.getRequestURL(), remoteIP, args, elapsed, hasException, resultStr);
}
return result;
}
} }
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