java_切面日志
切面日志举例
package com.keyba1;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author KeyBa
* @date 2019/6/13
* 记录日志
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
String value() default "";
}
package com.keyba1;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
*
* @author KeyBa
* @date 2019/6/13
* 注释式日志切面
*/
@Aspect
@Slf4j
@Component
public class LogAspect {
@Pointcut("@annotation(Log)")
public void logPointCut() {
}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
long beginTime = System.currentTimeMillis();
// 执行方法
Object result = joinPoint.proceed();
// 执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
//异步保存日志
saveLog(joinPoint, time);
return result;
}
void saveLog(ProceedingJoinPoint joinPoint, long time) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
String methodName = signature.getName();
// 请求的方法名
String className = joinPoint.getTarget().getClass().getName();
log.info("------------------------接口日志-----------------------" + "\n" + "类名称:" + className
+ "\n" + "方法名:" + methodName + "\n" + "执行时间:" + time + "毫秒");
log.info("接口参数"+"\n"+ Arrays.toString(joinPoint.getArgs()));
}
}
package com.keyba1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Keyba
*/
@RestController
@SpringBootApplication
public class LogUseApplication {
@RequestMapping("/")
@Log
String myApp(String strParam1, String intParam2) {
System.out.println("hello");
System.out.println("strParam1: " + strParam1);
System.out.println("intParam2: " + intParam2);
return "Hello";
}
public static void main(String[] args) {
SpringApplication.run(LogUseApplication.class, args);
}
}
package com.keyba2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
/**
*
* @author KeyBa
* @date 2019/6/13
* 无注解式切面
*/
@Component //声明组件
@Aspect // 声明切面
@ComponentScan //组件自动扫描
@EnableAspectJAutoProxy //spring自动切换JDK动态代理和CGLIB
public class LogAspect2 {
@Resource
HttpServletRequest request;
/**
* 自定义日志
*/
private Logger logger = LoggerFactory.getLogger(LogAspect2.class);
/**
* 在方法执行前进行切面
*/
// @Pointcut("execution(* com.keyba1..*.*(..)) && (@annotation(org.springframework.web.bind.annotation.GetMapping)||@annotation(org.springframework.web.bind.annotation.PutMapping)||@annotation(org.springframework.web.bind.annotation.DeleteMapping)||@annotation(org.springframework.web.bind.annotation.PostMapping)||@annotation(org.springframework.web.bind.annotation.RequestMapping))")
public void log() {
}
//@Before("log()")
public void before(JoinPoint point) {
logger.info("---------------------请求开始---------------------");
logger.info("请求地址:" + request.getRequestURL().toString());
logger.info("请求方式:" + request.getMethod());
logger.info("请求类方法:" + point.getSignature());
logger.info("请求类方法参数:" + Arrays.toString(point.getArgs()));
logger.info("-------------------------------------------------");
}
}
package com.keyba3;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import com.keyba1.Log;
import java.util.HashMap;
import java.util.Map;
/**
* Created by KeyBa on 2019/6/13.
* 使用org.projectlombok的@slf4j代替private final Logger logger = LoggerFactory.getLogger(XXX.class);
* 使用log方法需要安装lombox插件,使用@Data注解可以为一个bean自动生成getter , setter方法及toString 方法
*/
@Aspect
@Component
@Slf4j
public class TestAspect {
//com.kzj.kzj_rabbitmq.controller 包中所有的类的所有方法切面
//@Pointcut("execution(public * com.kzj.kzj_rabbitmq.controller.*.*(..))")
//只针对 MessageController 类切面
//@Pointcut("execution(public * com.kzj.kzj_rabbitmq.controller.MessageController.*(..))")
//统一切点,对com.keyba及其子包中所有的类的所有方法切面
//@Pointcut("execution(public * com.keyba1..*.*(..))")
@Pointcut("@annotation(Log)")
public void Pointcut() {
}
//前置通知
@Before("Pointcut()")
public void beforeMethod(JoinPoint joinPoint) {
log.info("调用了前置通知");
}
//@After: 后置通知
@After("Pointcut()")
public void afterMethod(JoinPoint joinPoint) {
log.info("调用了后置通知");
}
//@AfterRunning: 返回通知 rsult为返回内容
@AfterReturning(value = "Pointcut()", returning = "result")
public void afterReturningMethod(JoinPoint joinPoint, Object result) {
log.info("调用了返回通知");
}
//@AfterThrowing: 异常通知
@AfterThrowing(value = "Pointcut()", throwing = "e")
public void afterReturningMethod(JoinPoint joinPoint, Exception e) {
log.info("调用了异常通知");
}
//@Around:环绕通知1
//@Around("Pointcut()")
//public Object Around(ProceedingJoinPoint pjp) throws Throwable {
// log.info("around执行方法之前");
// Object object = pjp.proceed();
// log.info("around执行方法之后--返回值:" + object);
// return object;
//}
//@Around:环绕通知2
@Around("Pointcut()")
public Object Around(ProceedingJoinPoint pjp) throws Throwable {
Map<String, Object> data = new HashMap<>();
//获取目标类名称
String clazzName = pjp.getTarget().getClass().getName();
//获取目标类方法名称
String methodName = pjp.getSignature().getName();
//记录类名称
data.put("clazzName", clazzName);
//记录对应方法名称
data.put("methodName", methodName);
//记录请求参数
data.put("params", pjp.getArgs());
//开始调用时间
// 计时并调用目标函数
long start = System.currentTimeMillis();
Object result = pjp.proceed();
Long time = System.currentTimeMillis() - start;
//记录返回参数
data.put("result", result);
//设置消耗总时间
data.put("consumeTime", time);
System.out.println(data);
return result;
}
}
java_切面日志的更多相关文章
- 使用Spring的AOP实现切面日志
AOP切面日志的使用方式 @Aspect @Component public class HttpAspect { private static final Logger logger = Logge ...
- 【Spring Boot学习之五】切面日志管理
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.log4j 常见方式:log4j.properties + org.apache.log4j.Logger比如:l ...
- Java_使用日志
日志有什么用? 在实际开发中,不可能使用控制台输出所有数据,可以用日志把程序运行的过程记录下来,包括运行中出现的异常和BUG 当出现问题的时候,程序员可以去查看日志,从而能快速的找到问题所在. 一般来 ...
- 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- Spring Boot 2.0 教程 | AOP 切面统一打印请求日志
欢迎关注微信公众号: 小哈学Java 文章首发于个人网站 https://www.exception.site/springboot/spring-boot-aop-web-request 本节中,您 ...
- Springboot项目使用aop切面保存详细日志到ELK日志平台
上一篇讲过了将Springboot项目中logback日志插入到ELK日志平台,它只是个示例.这一篇来看一下实际使用中,我们应该怎样通过aop切面,拦截所有请求日志插入到ELK日志系统.同时,由于往往 ...
- Spring Boot 自定义注解,AOP 切面统一打印出入参请求日志
其实,小哈在之前就出过一篇关于如何使用 AOP 切面统一打印请求日志的文章,那为什么还要再出一篇呢?没东西写了? 哈哈,当然不是!原因是当时的实现方案还是存在缺陷的,原因如下: 不够灵活,由于是以所有 ...
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- JavaWeb项目架构之Redis分布式日志队列
架构.分布式.日志队列,标题自己都看着唬人,其实就是一个日志收集的功能,只不过中间加了一个Redis做消息队列罢了. 前言 为什么需要消息队列? 当系统中出现"生产"和" ...
随机推荐
- Java高并发程序设计学习笔记(四):无锁
转自:https://blog.csdn.net/dataiyangu/article/details/86440836#1__3 1. 无锁类的原理详解简介:1.1. CAS1.2. CPU指令2. ...
- windows环境下,kafka常用命令
创建topics kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partition 3 - ...
- shell中sed的简单使用
sed命令行格式为: sed [-nefri] ‘command’ 输入文本/文件 常用选项: -n∶取消默认的输出,使用安静(silent)模式.在一 ...
- java调用ffmpeg获取视频文件信息的一些参数
一.下载ffmpeg http://www.ffmpeg.org/download.html 主要需要bin目录下的ffmpeg可执行文件 二.java代码实现 package com.aw.util ...
- Kernel boot options
There are three ways to pass options to the kernel and thus control its behavior: When building the ...
- D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)
D - Yet Another Problem On a Subsequence CodeForces - 1000D The sequence of integers a1,a2,-,aka1,a2 ...
- for(auto count:counts)
c++中for(auto count : counts) 这是C++11中的语法,即:Range-based for loop.其中counts应满足:begin(counts), end(count ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏]
[易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏] 项目实战 实战1:猜数字游戏 我们今天来来开始简单的项目实战. 第一个简单项目是猜数字游戏. 简单来说,系统给了 ...
- 题解 【POJ1157】LITTLE SHOP OF FLOWERS
先把题目意思说一下: 你有F束花,编号为\(1\)~\(F\)(\(1<=F<=100\)),\(V\)个花瓶,编号为\(1\) ~\(V\)(\(1<=V<=100\)), ...
- jquery checked选择器 语法
jquery checked选择器 语法 作用::checked 选择器选取所有选中的复选框或单选按钮.直线电机参数 语法:$(":checked") jquery checked ...