日志管理-aop
来源:互联网 发布:线切割手动圆形编程 编辑:程序博客网 时间:2024/05/22 12:25
日志管理操作一般分为aop和拦截器两种,我做的是aop,但用aop有一个缺点,在做复杂登录验证上是无法获得登录的用户id
自定义注解
* 自定义日志注解 * @author biling */@Target({ElementType.PARAMETER, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ServiceLog { /** * 日志类型 * @return */ String logType() default ""; /** * 日志内容 * @return */ String logContent() default "";}
切面
import com.gxt.databean.Log;import com.gxt.service.LogService;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import javax.annotation.Resource;import java.lang.reflect.Method;import java.util.Date;@Aspect@Componentpublic class LogAspect { @Resource private LogService logService; private static final Logger logger = LoggerFactory.getLogger(LogAspect.class); @Pointcut("@annotation(com.gxt.annotation.ServiceLog)") public void serviceAspect() { } @Before("serviceAspect()") public void doBefore(JoinPoint joinPoint) { if(logger.isInfoEnabled()){ logger.info("before " + joinPoint); } } //配置service环绕通知,使用在方法aspect()上注册的切入点 @Around("serviceAspect()") public void around(JoinPoint joinPoint){ System.out.println("==========开始执行controller环绕通知==============="); long start = System.currentTimeMillis(); try { ((ProceedingJoinPoint) joinPoint).proceed(); long end = System.currentTimeMillis(); if(logger.isInfoEnabled()){ logger.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms!"); } System.out.println("==========结束执行service环绕通知==============="); } catch (Throwable e) { long end = System.currentTimeMillis(); if(logger.isInfoEnabled()){ logger.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : " + e.getMessage()); } } } /** * 用于拦截service层记录操作日志 * * @param joinPoint 切点 */ @After("serviceAspect()") public void after(JoinPoint joinPoint) { /*========控制台输出=========*/ System.out.println("=====截取通知开始====="); System.out.println("动作方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")); try { //获取注解中对方方法的描述信息,用于service层注解 String targetName = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); Object[] arguments = joinPoint.getArgs(); Class targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods(); String type = ""; String content = ""; for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs.length == arguments.length) { type = method.getAnnotation(ServiceLog.class).logType(); content = method.getAnnotation(ServiceLog.class).logContent(); break; } } } Log log = new Log(); log.setType(Integer.valueOf(type)); log.setContent(content); log.setCst_create(new Date()); log.setUrl(joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"); logService.add(log); }catch (Exception e){ e.printStackTrace(); } } //配置后置返回通知,使用在方法aspect()上注册的切入点 @AfterReturning("serviceAspect()") public void afterReturn(JoinPoint joinPoint){ System.out.println("=====执行service后置返回通知====="); if(logger.isInfoEnabled()){ logger.info("afterReturn " + joinPoint); } }}
在service层加上自定义的注解
@Transactional @Override **@ServiceLog(logType = "1",logContent = "添加漏洞白名单")** public ResultBO<Void> add(List<WhiteAddForm> adf,Long id){ for (int i = 0 ; i < adf.size() ; i++){ White white = new White(); WhiteAddForm whiteAddForm = adf.get(i); white.setAssets_id(whiteAddForm.getId()); white.setVul(whiteAddForm.getName()); white.setVul_type(whiteAddForm.getType()); white.setCompany_id(id); whiteMapper.insertSelective(white); } return new ResultBO<Void>(SUCCESS); }
启动项目后debug跟踪结果图:
数据库展现:
阅读全文
0 0
- Spring Aop 日志管理
- AOP日志管理
- Spring AOP 日志管理
- Spring AOP 日志管理
- Spring AOP 日志管理
- spring AOP日志管理
- Spring AOP 日志管理
- Spring Aop 日志管理
- AOP日志管理
- 日志管理-aop
- AOP实现日志管理
- spring aop开发日志管理
- 通过spring aop管理日志
- Spring AOP实现日志管理
- spring aop 日志记录管理
- spring aop实现日志管理
- Spring-aop 日志管理小例子
- Spring AOP进行日志记录,管理
- C#实现Word转PDF
- js 长图拖动效果
- python websocket client
- 电阻丝烧断原因分析
- 数据库第一范式,第二范式,第三范式
- 日志管理-aop
- react系列框架
- 使用@Controller注解为什么要配置<mvc:annotation-driven />
- 一、Rxjava从头学:响应式编程
- 理解Js作用域和作用域链
- 2016最流行的Android组件、工具、框架大全
- 图片的二次采样
- GIS软件开发工具包TatukGIS Developer Kernel v11.8发布,改进DWG阅读器
- <Qt>实例:视频播放器