|
@@ -33,99 +33,98 @@ import java.time.LocalDateTime;
|
|
|
@Component
|
|
|
public class XjrLogAspect {
|
|
|
|
|
|
- @Autowired
|
|
|
- private LogMapper logMapper;
|
|
|
-
|
|
|
-
|
|
|
- @Pointcut("@annotation(com.xjrsoft.common.annotation.XjrLog)")
|
|
|
- public void logPointCut() {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Around("logPointCut()")
|
|
|
- public Object around(ProceedingJoinPoint point) throws Throwable {
|
|
|
- long beginTime = System.currentTimeMillis();
|
|
|
- Object result = null;
|
|
|
- try {
|
|
|
-
|
|
|
- result = point.proceed();
|
|
|
- } catch (Throwable e) {
|
|
|
-
|
|
|
- long time = System.currentTimeMillis() - beginTime;
|
|
|
- insertLog(point, time, LogCategoryEnum.EXCEPTION.getCode());
|
|
|
- throw e;
|
|
|
- }
|
|
|
-
|
|
|
- long time = System.currentTimeMillis() - beginTime;
|
|
|
-
|
|
|
-
|
|
|
- insertLog(point, time, null);
|
|
|
-
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- private void insertLog(ProceedingJoinPoint joinPoint, long time, Integer category) {
|
|
|
- MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
- Method method = signature.getMethod();
|
|
|
-
|
|
|
- Log logEntity = new Log();
|
|
|
- XjrLog xjrLog = method.getAnnotation(XjrLog.class);
|
|
|
- if(xjrLog != null){
|
|
|
-
|
|
|
- logEntity.setOperation(xjrLog.value());
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- String className = joinPoint.getTarget().getClass().getName();
|
|
|
- String methodName = signature.getName();
|
|
|
- logEntity.setMethod(className + "." + methodName + "()");
|
|
|
-
|
|
|
-
|
|
|
- Object[] args = joinPoint.getArgs();
|
|
|
- try{
|
|
|
- String params = new Gson().toJson(args);
|
|
|
- if (params.length() < 4000) {
|
|
|
- logEntity.setParams(params);
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
|
|
|
-
|
|
|
- logEntity.setIp(ServletUtil.getClientIP(request));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if(StpUtil.isLogin()){
|
|
|
- SaSession tokenSession = StpUtil.getTokenSession();
|
|
|
- User user = tokenSession.get(GlobalConstant.LOGIN_USER_INFO_KEY, new User());
|
|
|
- logEntity.setUsername(user.getUserName());
|
|
|
- logEntity.setTime(time);
|
|
|
- logEntity.setCategory(category == null ? getCategory(request) : category);
|
|
|
- logEntity.setCreateTime(LocalDateTime.now());
|
|
|
-
|
|
|
- logMapper.insert(logEntity);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * @title 获取日志分类
|
|
|
- * @create 2020年11月11日 19:23:30
|
|
|
- * */
|
|
|
- private int getCategory(HttpServletRequest request){
|
|
|
- if(request.getRequestURI().equalsIgnoreCase("/system/login")){
|
|
|
- return LogCategoryEnum.LOGIN.getCode();
|
|
|
- }
|
|
|
- else if (request.getMethod().toLowerCase().equals("get")){
|
|
|
- return LogCategoryEnum.GET.getCode();
|
|
|
- }
|
|
|
- else if(request.getMethod().toLowerCase().equals("post") || request.getMethod().toLowerCase().equals("put") || request.getMethod().toLowerCase().equals("patch") || request.getMethod().toLowerCase().equals("delete")) {
|
|
|
- return LogCategoryEnum.OPERAT.getCode();
|
|
|
- }
|
|
|
- else
|
|
|
- return LogCategoryEnum.EXCEPTION.getCode();
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ private LogMapper logMapper;
|
|
|
+
|
|
|
+
|
|
|
+ @Pointcut("@annotation(com.xjrsoft.common.annotation.XjrLog)")
|
|
|
+ public void logPointCut() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Around("logPointCut()")
|
|
|
+ public Object around(ProceedingJoinPoint point) throws Throwable {
|
|
|
+ long beginTime = System.currentTimeMillis();
|
|
|
+ Object result = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ result = point.proceed();
|
|
|
+ } catch (Throwable e) {
|
|
|
+
|
|
|
+ long time = System.currentTimeMillis() - beginTime;
|
|
|
+ insertLog(point, time, LogCategoryEnum.EXCEPTION.getCode());
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+
|
|
|
+ long time = System.currentTimeMillis() - beginTime;
|
|
|
+
|
|
|
+
|
|
|
+ insertLog(point, time, null);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void insertLog(ProceedingJoinPoint joinPoint, long time, Integer category) {
|
|
|
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = signature.getMethod();
|
|
|
+
|
|
|
+ Log logEntity = new Log();
|
|
|
+ XjrLog xjrLog = method.getAnnotation(XjrLog.class);
|
|
|
+ if (xjrLog != null) {
|
|
|
+
|
|
|
+ logEntity.setOperation(xjrLog.value());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ String className = joinPoint.getTarget().getClass().getName();
|
|
|
+ String methodName = signature.getName();
|
|
|
+ logEntity.setMethod(className + "." + methodName + "()");
|
|
|
+
|
|
|
+ if (xjrLog != null && xjrLog.saveRequestData()) {
|
|
|
+
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+ try {
|
|
|
+ String params = new Gson().toJson(args);
|
|
|
+ if (params.length() < 4000) {
|
|
|
+ logEntity.setParams(params);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
|
|
|
+
|
|
|
+ logEntity.setIp(ServletUtil.getClientIP(request));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (StpUtil.isLogin()) {
|
|
|
+ SaSession tokenSession = StpUtil.getTokenSession();
|
|
|
+ User user = tokenSession.get(GlobalConstant.LOGIN_USER_INFO_KEY, new User());
|
|
|
+ logEntity.setUsername(user.getUserName());
|
|
|
+ logEntity.setTime(time);
|
|
|
+ logEntity.setCategory(category == null ? getCategory(request) : category);
|
|
|
+ logEntity.setCreateTime(LocalDateTime.now());
|
|
|
+
|
|
|
+ logMapper.insert(logEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * @title 获取日志分类
|
|
|
+ * @create 2020年11月11日 19:23:30
|
|
|
+ */
|
|
|
+ private int getCategory(HttpServletRequest request) {
|
|
|
+ if (request.getRequestURI().equalsIgnoreCase("/system/login")) {
|
|
|
+ return LogCategoryEnum.LOGIN.getCode();
|
|
|
+ } else if (request.getMethod().toLowerCase().equals("get")) {
|
|
|
+ return LogCategoryEnum.GET.getCode();
|
|
|
+ } else if (request.getMethod().toLowerCase().equals("post") || request.getMethod().toLowerCase().equals("put") || request.getMethod().toLowerCase().equals("patch") || request.getMethod().toLowerCase().equals("delete")) {
|
|
|
+ return LogCategoryEnum.OPERAT.getCode();
|
|
|
+ } else
|
|
|
+ return LogCategoryEnum.EXCEPTION.getCode();
|
|
|
+ }
|
|
|
}
|