springBootAOP环绕增强、⾃定义注解、log4j2、MDC (⼀)log4j2 maven配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 切换log4j2⽇志读取 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 配置 log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 加上这个才能辨认到l⽂件 -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
在resources ⽂件夹下创建l ⽂件
Configuration:
status: warn
Properties: # 定义全局变量
Property: # 缺省配置(⽤于开发环境)。其他环境需要在VM参数中指定,如下:
#测试:-sole=warn -Dlog.level.xjj=trace
#⽣产:-sole=warn -Dlog.level.xjj=info
- name: sole
value: trace
- name: log.level.xjj
value: trace
-
name: log.path
value: D:/Logs/log
- name: project.name
value: my-spring-boot
Appenders:
Console: #输出到控制台
name: CONSOLE
target: SYSTEM_OUT
ThresholdFilter:
level: ${sys:sole} # “sys:”表⽰:如果VM参数中没指定这个变量值,则使⽤本⽂件中定义的缺省全局变量值
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m %X{REQUESTID} %n"
RollingFile: # 输出到⽂件,超过128MB归档
- name: ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/${project.name}.log
filePattern: "${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%"
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m %X{REQUESTID} %n"
Policies:
SizeBasedTriggeringPolicy:
size: "128 MB"
DefaultRolloverStrategy:
max: 1000
Loggers:
Root:
level: info
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
Logger: # 为com.xjj包配置特殊的Log级别,⽅便调试
-
name: com.xjj
additivity: false
level: ${sys:log.level.xjj}
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
其中 %X{REQUESTID} 为MDC设定的请求标识,每个请求都会有,⽤于跟踪⽇志。
(⼆)MDC和AOP 环绕增强、⾃定义注解
创建⽇志类
public class tools_log {
public static Logger getLogger(Class class_) {
Logger logger = Logger(class_);
return logger;
}
}//end
AOP、MDC
@Aspect
@Component
@Slf4j
public class LogAspect {
@Pointcut("execution(public * 包路径..*.*(..))")
public void LogHelp() {
}
@Pointcut("@annotation(⾃定义注解路径)")
public void noAnnotation() {
}
final Logger logger = Class());
@Around("LogHelp()&&!noAnnotation()")
public Object arround(ProceedingJoinPoint joinPoint) {
MDC.put("REQUESTID", UUID.randomUUID().toString());
logger.info("⽅法环绕");
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestAttributes();
HttpServletRequest request = Request();
// 记录下请求内容
logger.info("URL : " + RequestURL().toString());
logger.info("HTTP_METHOD : " + Method());
logger.info("IP : " + RemoteAddr());
logger.info("CLASS_METHOD : " + Signature().getDeclaringTypeName() + "." + Signature().getName()); logger.info("ARGS : " + Args()));
try {
Object o = joinPoint.proceed();
logger.info("⽅法环绕proceed,结果是 :" + o);
return o;
} catch (Throwable e) {
<(e.toString());
return null;
} finally {
MDC.clear();
}
}springboot aop
}//end
.. 表⽰匹配多个参数
* 表⽰匹配⼀个参数
.* 表⽰匹配多个类
⾃定义注解
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface noAroundAnno {
}//end
定义在⽅法上
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论