/** * Advice that logs methods throwing exceptions. */ @AfterThrowing(pointcut = "loggingPointcut()", throwing = "e") public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { if (env.acceptsProfiles("dev")) { log.error("Exception in {}.{}() with cause = \'{}\' and exception = \'{}\'", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : "NULL", e.getMessage(), e ); } else { log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : "NULL" ); } }
@AfterThrowing(pointcut = "execution(* cn.org.once.cstack.service.ModuleService.remove(..)) " + "|| execution(* cn.org.once.cstack.service.ModuleService.initModule(..))", throwing = "e") public void afterThrowingModule(StaticPart staticPart, Exception e) throws ServiceException { User user = this.getAuthentificatedUser(); Message message = null; logger.debug("CALLED CLASS : " + staticPart.getSignature().getName()); switch (staticPart.getSignature().getName().toUpperCase()) { case initModule: message = MessageUtils.writeAfterThrowingModuleMessage(e, user, initModule); break; case deleteType: message = MessageUtils.writeAfterThrowingModuleMessage(e, user, deleteType); break; } if (message != null) { messageService.create(message); } }
@AfterThrowing(value = "execution(@org.smartlog.aop.Loggable * *(..))", throwing = "t") public void afterThrowingLoggable(final JoinPoint joinPoint, final Throwable t) throws Throwable { final LogContext ctx = SmartLog.throwable(t); if (ctx.level() == null) { // use ERROR level by default when got uncaught exception ctx.level(LogLevel.ERROR); } final Object result = ctx.result(); if (result == null) { ctx.result(t.toString()); } else { ctx.result(result.toString() + "; " + t.toString()); } finish(joinPoint, ctx); }
@AfterThrowing(throwing = "ex", pointcut = "webLog()") public void doAfterThrowing(Throwable ex) { //有异常 WebOosLog commLogger = commLoggerThreadLocal.get(); if (ex instanceof BusinessException) { commLogger.setActionResCode(ResponseStatus.BUSINESS_FAILED); } else if (ex instanceof ValidateException) { commLogger.setActionResCode(ResponseStatus.VALIDATE_ERR); } else if (ex instanceof LoginTimeoutException) { commLogger.setActionResCode(ResponseStatus.LOGIN_TIME_OUT); } else if (ex instanceof NoPrivilegeException) { commLogger.setActionResCode(ResponseStatus.NO_PRIVILEGE); } else { commLogger.setActionResCode(ResponseStatus.SYSTEM_ERR); } commLogger.setReqEndTime(new Date()); commLogger.setReqDealTime((int) (commLogger.getReqEndTime().getTime() - commLogger.getReqStartTime().getTime())); commLogger.setIsUndefinedException(true); commLogger.setExcepStackInfo(ExceptionUtil.parseException(ex)); loggerRecordService.doRecord(commLogger); logger.debug("EXCEPTION : " + ExceptionUtil.parseException(ex)); logger.debug("SPEND TIME : " + commLogger.getReqDealTime() + "ms"); logger.debug("***************请求" + commLogger.getActionDesc() + "结束***************"); }
@AfterThrowing(pointcut = "controllerAspect()", throwing = "exception") public void doException(JoinPoint joinPoint, Exception exception) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); // 读取session中的用户 // User user = SessionUtils.getUserFromSession(request); // if(null == user){ // logger.info("未登录日志不记录"); // return; // } try { Object[] args = getLogMethodDescription(joinPoint); // *========数据库日志=========*// // logService.saveLog(user.getId(), user.getAccount(), LogType.EXCEPTION_LOG, // IpUtils.getIpAddr(request), args[0].toString() + "-->" + exception.getMessage(), // user.getManufacturerId(), reqDescription(request)); } catch (Exception e) { // 记录本地异常日志 logger.error(e); e.printStackTrace(); } }
@AfterThrowing(throwing = "ex", pointcut = "execution(* com.github.izhangzhihao.SSMSeedProject.*.*.*(..)))") public void LogToDB(JoinPoint joinPoint, Throwable ex) { //出错行 int lineNumber = ex.getStackTrace()[0].getLineNumber(); //方法签名 Signature signature = joinPoint.getSignature(); //参数 Object[] args = joinPoint.getArgs(); //拼接参数 StringBuilder argString = new StringBuilder(); if (args.length > 0) for (Object arg : args) argString.append(arg); ex.printStackTrace(); log.error("方法" + signature, "参数" + argString, "错误行:" + lineNumber, "时间" + new Date(), "异常内容" + ex.toString()); }
/** * After throwing advice. * * @param joinPoint the join point * @param error the error */ @AfterThrowing(pointcut = "selectAll()", throwing = "error") public void afterThrowingAdvice(JoinPoint joinPoint, Throwable error) { if (isAlreadySysPointCut(error)) { return; } setAlreadySysPointCut(error); if (error instanceof TestDataException) { ITestResult itr = Reporter.getCurrentTestResult(); itr.setThrowable(error); } // String[] fullST = Utils.stackTrace(error, false); // LogbackWriter.writeSysError(joinPoint.getTarget().toString()+ error.hashCode() + fullST[1] ); Class<?> cls = joinPoint.getTarget().getClass(); if (null == cls) throw GlobalUtils.createInternalError("jvm"); LogbackWriter.writeSysError(cls, error); }
/** * 出现异常自动记录日志 * @param ex 异常 */ @AfterThrowing(pointcut="methods()", throwing="ex") public void ExceptionLog(Exception ex) { LoginInfo loginUser = UserContextHolder.getUser(); //保证用户已经登陆后并通过菜单访问功能时才记录操作时间 if (loginUser != null ) { Log log = new Log(); log.setDf("0"); log.setIp(loginUser.getIp()); log.setPermission(loginUser.getPermission()); log.setTs(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); log.setType("APP_LOG001"); log.setUsername(loginUser.getUserName()); logManager.save(log); } }
@AfterThrowing( pointcut = "execution(* (@javax.jws.WebService *).*(..))", throwing = "throwable") public void auditAfterThrowing(JoinPoint joinPoint, Throwable throwable) throws Throwable { String id = getId(); Class targetClass = joinPoint.getTarget().getClass(); String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); LOG.info("[" + id + "] After exception: " + targetClass.getName() + "." + methodName + " with " + args.length + " params"); AuditEntity ae = AuditEntity.getInstance( new Timestamp(new Date().getTime()).toString(), methodName, objectsToString(args), throwable != null ? throwable.toString() : null, STATE_EXCEPTION ); LOG.info("[" + id + "] Exception: " + ae); }
/** * Advice that logs methods throwing exceptions. * * @param joinPoint join point for advice * @param e exception */ @AfterThrowing(pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e") public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { log.error("Exception in {}.{}() with cause = \'{}\' and exception = \'{}\'", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL", e.getMessage(), e); } else { log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL"); } }
/** * Advice that logs methods throwing exceptions. * * @param joinPoint join point for advice * @param e exception */ @AfterThrowing(pointcut = "loggingPointcut()", throwing = "e") public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { log.error("Exception in {}.{}() with cause = \'{}\' and exception = \'{}\'", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL", e.getMessage(), e); } else { log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL"); } }
/** * @param joinPoint * @param e */ @AfterThrowing(value = "doAfterThrowing()", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); Map<String, Object> map = new HashMap<String, Object>(); map.put("className", joinPoint.getTarget().getClass().getName()); map.put("methodName", method.getName()); map.put("args", JSONUtils.toJSONString(joinPoint.getArgs().toString())); map.put("errorMsg", e.getMessage()); logger.error(JSONUtils.toJSONString(map)); }
@AfterThrowing(pointcut = "loggingPointcut()", throwing = "e") public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { if (env.acceptsProfiles(Constants.SPRING_PROFILE_DEVELOPMENT)) { log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause(), e); } else { log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause()); } }
@AfterThrowing(pointcut = "execution(* cn.org.once.cstack.ServerService.updateType(..))", throwing = "e") public void afterThrowingServer(StaticPart staticPart, Exception e) throws ServiceException { User user = this.getAuthentificatedUser(); Message message = null; logger.debug("CALLED CLASS : " + staticPart.getSignature().getName()); switch (staticPart.getSignature().getName().toUpperCase()) { case updateType: message = MessageUtils.writeAfterThrowingModuleMessage(e, user, updateType); break; } if (message != null) { messageService.create(message); } }
@AfterThrowing(pointcut = "execution(* cn.org.once.cstack.service.*.*(..))", throwing= "error") public void logAfterThrowing(Throwable error) { if(error.getMessage() != null && error.getMessage() .contains("org.hibernate.exception.JDBCConnectionException: could not extract ResultSet")) { publisher.publishEvent(new DatabaseConnectionFailEvent("Database connection has been lost")); } }
@AfterThrowing(pointcut = "anyMethod() && withStepAnnotation()", throwing = "e") public void stepFailed(final JoinPoint joinPoint, final Throwable e) { getLifecycle().updateStep(result -> result .withStatus(getStatus(e).orElse(Status.BROKEN)) .withStatusDetails(getStatusDetails(e).orElse(null))); getLifecycle().stopStep(); }
/** * Advice that logs methods throwing exceptions. */ @AfterThrowing(pointcut = "loggingPointcut()", throwing = "e") public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { log.error("Exception in {}.{}() with cause = \'{}\' and exception = \'{}\'", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL", e.getMessage(), e); } else { log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL"); } }
@AfterThrowing(pointcut = "execution(@org.alfresco.traitextender.Extend * *(..) throws TestException) && (@annotation(extendAnnotation))", throwing = "ete") public void intercept(Extend extendAnnotation, ExtensionTargetException ete) throws TestException { Throwable exception = AJExtender.asCheckThrowable(ete.getCause(), TestException.class); if (exception instanceof TestException) { throw (TestException) exception; } else { throw ete; } }
/** * Advice that logs methods throwing exceptions. */ @AfterThrowing(pointcut = "loggingPointcut()", throwing = "e") public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { if (env.acceptsProfiles(Constants.SPRING_PROFILE_DEVELOPMENT)) { log.error("Exception in {}.{}() with cause = \'{}\' and exception = \'{}\'", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL", e.getMessage(), e); } else { log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL"); } }
/** * Advice that logs methods throwing exceptions. * * @param joinPoint join point for advice * @param e exception */ @AfterThrowing(pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e") public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { log.error("Exception in {}.{}() with cause = \'{}\' and exception = \'{}\'", joinPoint.getSignature() .getDeclaringTypeName(), joinPoint.getSignature() .getName(), e.getCause() != null ? e.getCause() : "NULL", e.getMessage(), e); }
@AfterThrowing( pointcut = "allPublicControllerMethodsPointcut() && " + "methodLoggingNotDisabledPointcut() && " + "methodOrClassLoggingEnabledPointcut()", throwing = "t") public void onException(@Nonnull JoinPoint joinPoint, @Nonnull Throwable t) { String methodName = joinPoint.getSignature().getName() + "()"; LOG.info(methodName + " threw exception: [" + t + "]"); }
/** * Find and return the first AspectJ annotation on the given method * (there <i>should</i> only be one anyway...) */ @SuppressWarnings("unchecked") protected static AspectJAnnotation<?> findAspectJAnnotationOnMethod(Method method) { Class<?>[] classesToLookFor = new Class<?>[] { Before.class, Around.class, After.class, AfterReturning.class, AfterThrowing.class, Pointcut.class}; for (Class<?> c : classesToLookFor) { AspectJAnnotation<?> foundAnnotation = findAnnotation(method, (Class<Annotation>) c); if (foundAnnotation != null) { return foundAnnotation; } } return null; }
@AfterThrowing(pointcut = "pointcut()", throwing = "error") public void logAfterThrowing(JoinPoint joinPoint, Throwable error) { System.out.println("******"); System.out.println("logAfterThrowing() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("Exception : " + error); System.out.println("******"); }
@AfterThrowing(value = "within(com.kloia.eventapis.api.CommandHandler+ || com.kloia.eventapis.api.EventHandler+) && execution(* execute(..))", throwing = "e") public void afterThrowing( Exception e) throws Throwable { try { log.info("afterThrowing method:"+e); kafkaOperationRepository.failOperation(operationContext.getContext(),operationContext.getCommandContext(),event -> event.setEventState(EventState.TXN_FAILED)); } finally { operationContext.clearContext(); } }
@AfterThrowing(pointcut = "empController() && anyMethod() && args(empId, request)", throwing = "e") public void handleAfterMethodWithEmpIdThrow(JoinPoint joinPoint, Integer empId, HttpServletRequest request, Throwable e) { int action = Integer.parseInt((((MethodSignature)joinPoint.getSignature()) .getMethod()).getAnnotation(RequestMapping.class).name()); AuditInfo auditRecord = new AuditInfo(null, empId, request.getRemoteAddr(), "Id: "+empId, action, e.getMessage()); auditService.createRecord(auditRecord); }
@AfterThrowing(pointcut = "empController() && anyMethod() && args(emp, request)", throwing= "e") public void handleAfterMethodWithEmpThrows(JoinPoint joinPoint, Employee emp, HttpServletRequest request, Throwable e) { int action = Integer.parseInt((((MethodSignature)joinPoint.getSignature()) .getMethod()).getAnnotation(RequestMapping.class).name()); AuditInfo auditRecord = new AuditInfo(null, emp.getEmpID()==0?null:emp.getEmpID(), request.getRemoteAddr(), emp.toString(), action, e.getMessage()); auditService.createRecord(auditRecord); }
@AfterThrowing(pointcut = "empController() && anyMethod() && args(request)", throwing = "e") public void handleAfterMethodAllEmpThrow(JoinPoint joinPoint, Throwable e, HttpServletRequest request) { int action = Integer.parseInt((((MethodSignature)joinPoint.getSignature()) .getMethod()).getAnnotation(RequestMapping.class).name()); AuditInfo auditRecord = new AuditInfo(null,null, request.getRemoteAddr(), "Get all emp", action, e.getMessage()); auditService.createRecord(auditRecord); }
@AfterThrowing(pointcut = "empController() && anyMethod() && args(start, num, request) )", throwing = "e") public void handleAfterMethodPartEmpThrow(JoinPoint joinPoint, Throwable e, int start, int num, HttpServletRequest request) { int action = Integer.parseInt((((MethodSignature)joinPoint.getSignature()) .getMethod()).getAnnotation(RequestMapping.class).name()); AuditInfo auditRecord = new AuditInfo(null,null, request.getRemoteAddr(), "Start:num - " +start +":"+num, action, e.getMessage()); auditService.createRecord(auditRecord); }
@AfterThrowing(pointcut = "empController() && anyMethod() && args(.., request) )", throwing = "e") public void handleAfterMethodIntCertThrow(JoinPoint joinPoint, Throwable e, HttpServletRequest request) { int action = Integer.parseInt((((MethodSignature)joinPoint.getSignature()) .getMethod()).getAnnotation(RequestMapping.class).name()); AuditInfo auditRecord = new AuditInfo(null, null, request.getRemoteAddr(), "", action, e.getMessage()); auditService.createRecord(auditRecord); }