spring Boot使用AOP统一处理Web请求日志记录
1.使用spring boot实现一个拦截器
1、引入依赖:
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-aop</
artifactId
>
</
dependency
>
/**
* 拦截器:记录O2O调用接口记录
*/
@Aspect
@Component
public class O2oInterfaceInterceptor {
@Autowired
private SysCallInterfaceLogRepository sysCallInterfaceLogRepository;
@Autowired
private SysEmailService sysEmailService;
@Autowired
private SysPropertyService sysPropertyService;
private static Logger logger = LoggerFactory.getLogger(O2oInterfaceInterceptor.class);
/**
* 定义拦截规则:拦截com.ctop.wms.interfaces.ActivitiInterfaces包下面的所有类中,有@RequestMapping注解的方法。
*/
@Pointcut("execution(* com.ctop.wms.interfaces.ActivitiInterfaces.*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void controllerMethodPointcut() {}
@Around("controllerMethodPointcut()")
public Object controllerMethodPointcutInterceptor(ProceedingJoinPoint pjp) {
return Interceptor(pjp);
}
/**
* 拦截器具体实现
* @param pjp
* @return JsonResult(被拦截方法的执行结果)
*/
public Object Interceptor(ProceedingJoinPoint pjp){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod(); //获取被拦截的方法
String methodName = method.getName(); //获取被拦截的方法名
String ip = request.getRemoteAddr();
String url = request.getRequestURL().toString();
logger.info("被调接口,请求开始----------------------------");
logger.info("ip : " + request.getRemoteAddr());
logger.info("url : " + request.getRequestURL().toString());
logger.info("methodName : " + methodName);
Object result = "";
String param = Arrays.toString(pjp.getArgs());
logger.info("param : " + param);
SysCallInterfaceLog log = new SysCallInterfaceLog();
log.setExt1("called");// 被叫
log.setRequestIp(ip);
log.setRequestMethod(methodName);
log.setRequestParam(param);
log.setRequestUrl(url);
try {
// 一切正常的情况下,继续执行被拦截的方法
result = pjp.proceed();
String resultStr = "";
if(result != null) {
if(result instanceof String) {
resultStr = (String) result;
} else {
Gson gson = new Gson();
resultStr = gson.toJson(result);
}
}
log.setResult(resultStr);
logger.info("result : " + resultStr);
logger.info("请求结束,请求成功");
// 记录结果到数据库
log.setSuccess("success");
log = sysCallInterfaceLogRepository.save(log);
} catch (Throwable e) {
logger.info("请求结束,请求失败");
// 记录结果到数据库, 并发送邮件
log.setSuccess("fail");
Gson gson = new Gson();
String exceptionStr = gson.toJson(e);
log.setResult(exceptionStr);
SysProperty sysPropertyName= sysPropertyService.getSysProperty("o2o.wms.log.name");
SysProperty sysPropertyEmail= sysPropertyService.getSysProperty("o2o.wms.log.email");
log = sysCallInterfaceLogRepository.save(log);
SysEmailDto sysEmailDto = new SysEmailDto();
List<SysEmailInfoDto> sysEmailInfoDtoLs = new ArrayList<SysEmailInfoDto>();
SysEmailInfoDto dtoDetails = new SysEmailInfoDto();
if(sysPropertyName !=null && sysPropertyEmail !=null){
dtoDetails.setReceiverEmail(sysPropertyEmail.getPropValue());
dtoDetails.setReceiverName(sysPropertyName.getPropValue());
}
sysEmailDto.setTitle("O2O调用WMS接口失败日志!");
sysEmailDto.setContent("调用:"+url+"失败!sys_Call_Interface_Log.Scil_Uuid="+log.getScilUuid()+",\n请求参数:"+param+",\n调用结果:"+exceptionStr);
sysEmailInfoDtoLs.add(dtoDetails);
sysEmailDto.setSysEmailInfoDto(sysEmailInfoDtoLs);
try {
sysEmailService.addSysEmail(sysEmailDto);
} catch (Exception e1) {
e1.printStackTrace();
}
e.printStackTrace();
throw new BusinessException(e, null, null);
}
return result;
}
}
spring Boot使用AOP统一处理Web请求日志记录的更多相关文章
- spring boot使用AOP统一处理web请求
为了保证服务的高可用,及时发现问题,迅速解决问题,为应用添加log是必不可少的. 但是随着项目的增大,方法增多,每个方法加单独加日志处理会有很多冗余 那在SpringBoot项目中如何统一的处理Web ...
- 46. Spring Boot中使用AOP统一处理Web请求日志
在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- SpringBoot2.0 使用AOP统一处理Web请求日志(完整版)
一,加入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- (转)Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- 转:Spring Boot中使用AOP统一处理Web请求日志
在spring boot中,简单几步,使用spring AOP实现一个拦截器: 1.引入依赖: <dependency> <groupId>org.springframewor ...
- Spring Boot2.0之统一处理web请求日志
试问,你的项目中,如果有几万个方法,你还这么写log.info("name"+name+",age"+age )日志么?low~ 所以用AOP呀 1.首先创建个 ...
- springboot Aop 统一处理Web请求日志
1.增加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
随机推荐
- Linux内核分析-分析system_call中断处理过程
姓名:江军 ID:fuchen1994 分析system_call中断处理过程 使用gdb跟踪分析一个系统调用内核函数(您上周选择那一个系统调用),系统调用列表参见http://codelab.shi ...
- ESXi6.5中将虚拟机从厚置备转换为精简置备
本文来自:https://blog.csdn.net/wangjingkaibear/article/details/77097041 用ESXi做虚拟化,创建了一个原始虚拟机并安装好系统做好基本设置 ...
- 数据库SQL优化大总结之 百万级数据库优化方案(转载)
网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 这篇文章我花费了大量的时间查找资料.修改.排版,希望大家阅读之后,感觉 ...
- 37行代码实现一个简单的打游戏AI
不废话,直接上码,跟神经网络一点关系都没有,这37行代码只能保证电脑的对敌牺牲率是1:10左右,如果想手动操控,注释掉autopilot后边的代码即可. 哪个大神有兴趣可以用tensorflow或者s ...
- 正则表达式,清除HTML标签,但要保留 <br>和<img>标签,其他的清除
最近有个需求, 要替换到html当中的除了br和img以外的所有标签, 遂百度之, 在百度知道遇到大神 , 在这记录一下 /<(?!\/?br\/?.+?>|\/?img.+?>)[ ...
- matlab学习笔记 函数bsxfun repmat
一.举例 a=rand(3,1);b=rand(1,3); c=bsxfun(@plus,a,b); d=a*b; c和d的运算类似,只不过c是外加,d是外乘. 作用:速度快>for循环> ...
- c++ 字符数组-print and 写入文件
1.print ][] = { }; method1: 为了打印出unsigned char数据所对应的数值,可以将其强制转换成int类型,并做打印输出. std::cout << ][] ...
- 30秒让让你的电脑快一倍 - 计算机基础 - 中国红客联盟 - Powered
一.清理垃圾 在Windows在安装和使用过程中都会产生相当多的垃圾文件,包括临时文件(如:*.tmp.*._mp)日志文件(*.log).临时帮助文件(*.gid).磁盘检查文件(*.chk).临时 ...
- xdoj 1044---炸红花 (话说 小时候经常玩这个被虐。。。。qwq)
// 我真的好笨 只会枚举 话说那个ac的370b到底是怎么做的 /(ㄒoㄒ)/~~ #include <iostream> #include <algorithm> usin ...
- 使用 WPF 开发一个 Windows 屏幕保护程序
最近有小伙伴问我如何可以让 Windows 静置一段时间不操作之后,显示一个特殊的界面.我想了想,屏幕保护程序可以做到这一点,而且,屏幕保护程序的开发也是非常简单的. 本文将介绍如何为 Windows ...