Commit 2bf97a4b by yw

项目初始化

parent 4199d49e
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>datax-common</artifactId>
<groupId>cn.datax</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>datax-common-log</artifactId>
</project>
\ No newline at end of file
package cn.datax.common.log.annotation;
import cn.datax.common.log.config.AutoConfiguration;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({AutoConfiguration.class})
public @interface EnableDataLog {
}
package cn.datax.common.log.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAop {
/** 模块 */
String module() default "";
/** 描述 */
String value() default "";
}
package cn.datax.common.log.aop;
import java.lang.reflect.Method;
import cn.datax.common.log.annotation.LogAop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Aspect
public class LogAspect {
// 配置织入点
@Pointcut("@annotation(cn.datax.common.log.annotation.LogAop)")
public void logPointCut() {}
/**
* 前置通知 用于拦截操作
*
* @param joinPoint 切点
*/
@AfterReturning(pointcut = "logPointCut()")
public void doBefore(JoinPoint joinPoint) {
handleLog(joinPoint, null);
}
/**
* 拦截异常操作
*
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "logPointCut()", throwing = "e")
public void doAfter(JoinPoint joinPoint, Exception e) {
handleLog(joinPoint, e);
}
protected void handleLog(final JoinPoint joinPoint, final Exception e) {
try {
// 获得注解
LogAop logAop = getAnnotationLog(joinPoint);
if(logAop == null) {
return;
}
// 设置方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
log.info("[类名]:{},[方法]:{},[模块]:{},[描述]:{}", className, methodName, logAop.module(), logAop.value());
} catch (Exception exp) {
log.error("前置通知异常信息:{}", exp.getMessage(), exp);
}
}
/**
* 是否存在注解,如果存在就获取
*/
private LogAop getAnnotationLog(JoinPoint joinPoint) throws Exception {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if(method != null) {
return method.getAnnotation(LogAop.class);
}
return null;
}
}
package cn.datax.common.log.config;
import cn.datax.common.log.aop.LogAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
/**
* 扫描注入bean
* @author yuwei
* @since 2019/10/30
*/
@ComponentScan({"cn.datax.common.log"})
public class AutoConfiguration {
@Bean
public LogAspect logAspect() {
return new LogAspect();
}
}
......@@ -17,6 +17,7 @@
<module>datax-common-core</module>
<module>datax-common-mybatis</module>
<module>datax-common-security</module>
<module>datax-common-log</module>
</modules>
</project>
\ No newline at end of file
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