【spring】 spring AOP 和 annotation 实现日志功能
日志注解:
package com.wa.xwolf.sblog.dao; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 日志记录的注解 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Inherited public @interface Log { /** * 是否需要打印日志 */ boolean print() default true; /** * 日志信息。<p> */ String content() default ""; /** * 需要附带打印的请求参数 */ String[] parameters() default {}; }
定义切面:
package com.wa.xwolf.sblog.aspectj; import java.lang.reflect.Method; import java.util.UUID; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.wa.xwolf.sblog.dao.Log; /** * 用于记录日志的组件 */ @Aspect @Component public class LoggerBean { private static Logger logger = LoggerFactory.getLogger(LoggerBean.class); ThreadLocal<Long> time = new ThreadLocal<Long>(); ThreadLocal<String> tag = new ThreadLocal<String>(); @Pointcut("execution (* com.wa.xwolf.controller..*.*(..))") public void log(){ System.out.println("我是一个切入点"); } @Before("log()") public void beforeExec(JoinPoint joinPoint){ time.set(System.currentTimeMillis()); tag.set(UUID.randomUUID().toString()); info(joinPoint); MethodSignature ms=(MethodSignature) joinPoint.getSignature(); Method method=ms.getMethod(); if(method.getAnnotation(Log.class).content().equals(null)){ logger.error(method.getAnnotation(Log.class).content()+"标记"+tag.get()); } System.out.println(method.getAnnotation(Log.class).content()+"标记"+tag.get()); } @After("log()") public void afterExec(JoinPoint joinPoint){ MethodSignature ms=(MethodSignature) joinPoint.getSignature(); Method method=ms.getMethod(); System.out.println("标记为"+tag.get()+"的方法"+method.getName()+"运行消耗"+(System.currentTimeMillis()-time.get())+"ms"); } @Around("log()") public void aroundExec(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("我是Around,来打酱油的"); pjp.proceed(); } private void info(JoinPoint joinPoint){ System.out.println("--------------------------------------------------"); System.out.println("King:\t"+joinPoint.getKind()); System.out.println("Target:\t"+joinPoint.getTarget().toString()); Object[] os=joinPoint.getArgs(); System.out.println("Args:"); for(int i=0;i<os.length;i++){ System.out.println("\t==>参数["+i+"]:\t"+os[i].toString()); } System.out.println("Signature:\t"+joinPoint.getSignature()); System.out.println("SourceLocation:\t"+joinPoint.getSourceLocation()); System.out.println("StaticPart:\t"+joinPoint.getStaticPart()); System.out.println("--------------------------------------------------"); } }
在spring声明要扫描的注解所在的包,即可。
@Log(content="测试日志信息...........") @RequestMapping("/tLog") public void testLOG(){ }
部署启动项目,即可看到控制台的日志信息。
不过项目中大部分用Log4j,此处是同学的写法,学习一下AOP.
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号