LoggingService
package me.zhengjie.common.aop.log; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* @author jie
* @date 2018-11-24
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
String description() default "";
}
package me.zhengjie.common.aop.log; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.common.exception.BadRequestException;
import me.zhengjie.monitor.domain.Logging;
import me.zhengjie.monitor.service.LoggingService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import java.io.PrintWriter;
import java.io.StringWriter; /**
* @author jie
* @date 2018-11-24
*/
@Component
@Aspect
@Slf4j
public class LogAspect { @Autowired
private LoggingService loggingService; private long currentTime = 0L; /**
* 配置切入点
*/
@Pointcut("@annotation(me.zhengjie.common.aop.log.Log)")
public void logPointcut() {
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
} /**
* 配置环绕通知,使用在方法logPointcut()上注册的切入点
*
* @param joinPoint join point for advice
*/
@Around("logPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint){
Object result = null;
currentTime = System.currentTimeMillis();
try {
result = joinPoint.proceed();
} catch (Throwable e) {
throw new BadRequestException(e.getMessage());
}
Logging logging = new Logging("INFO",System.currentTimeMillis() - currentTime);
loggingService.save(joinPoint, logging);
return result;
} /**
* 配置异常通知
*
* @param joinPoint join point for advice
* @param e exception
*/
@AfterThrowing(pointcut = "logPointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
Logging logging = new Logging("ERROR",System.currentTimeMillis() - currentTime);
logging.setExceptionDetail(e.getMessage());
loggingService.save((ProceedingJoinPoint)joinPoint, logging);
}
}
package me.zhengjie.monitor.service; import me.zhengjie.monitor.domain.Logging;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.scheduling.annotation.Async; /**
* @author jie
* @date 2018-11-24
*/
public interface LoggingService { /**
* 新增日志
* @param joinPoint
* @param logging
*/
@Async
void save(ProceedingJoinPoint joinPoint, Logging logging);
}
package me.zhengjie.monitor.service.impl; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import me.zhengjie.common.utils.IpUtil;
import me.zhengjie.common.utils.RequestHolder;
import me.zhengjie.core.security.AuthorizationUser;
import me.zhengjie.core.utils.JwtTokenUtil;
import me.zhengjie.monitor.domain.Logging;
import me.zhengjie.monitor.repository.LoggingRepository;
import me.zhengjie.monitor.service.LoggingService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method; /**
* @author jie
* @date 2018-11-24
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class LoggingServiceImpl implements LoggingService { @Autowired
private LoggingRepository loggingRepository; @Value("${jwt.header}")
private String tokenHeader; @Autowired
private JwtTokenUtil jwtTokenUtil; private final String LOGINPATH = "authenticationLogin"; @Override
@Transactional(rollbackFor = Exception.class)
public void save(ProceedingJoinPoint joinPoint, Logging logging){ // 获取request
HttpServletRequest request = RequestHolder.getHttpServletRequest();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
me.zhengjie.common.aop.log.Log log = method.getAnnotation(me.zhengjie.common.aop.log.Log.class); // 描述
if (log != null) {
logging.setDescription(log.description());
} // 方法路径
String methodName = joinPoint.getTarget().getClass().getName()+"."+signature.getName()+"()"; String params = "{";
//参数值
Object[] argValues = joinPoint.getArgs();
//参数名称
String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
// 用户名
String username = ""; if(argValues != null){
for (int i = 0; i < argValues.length; i++) {
params += " " + argNames[i] + ": " + argValues[i];
}
} // 获取IP地址
logging.setRequestIp(IpUtil.getIP(request)); if(!LOGINPATH.equals(signature.getName())){
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
username = userDetails.getUsername();
} else {
AuthorizationUser user = JSONUtil.toBean(new JSONObject(argValues[0]),AuthorizationUser.class);
username = user.getUsername(); }
if (params.length() > 1000){
params = params.substring(0,999);
}
logging.setUsername(username);
logging.setParams(params + " }");
loggingRepository.save(logging);
}
}
package me.zhengjie.monitor.domain; import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.sql.Timestamp; /**
* @author jie
* @date 2018-11-24
*/
@Entity
@Data
@Table(name = "log")
@NoArgsConstructor
public class Logging { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; /**
* 操作用户
*/
private String username; /**
* 描述
*/
private String description; /**
* 方法名
*/
private String method; /**
* 参数
*/
@Column(length = 1500)
private String params; /**
* 日志类型
*/
private String logType; /**
* 请求ip
*/
private String requestIp; /**
* 请求耗时
*/
private Long time; /**
* 异常详细
*/
@Column(length = 1500)
private String exceptionDetail; /**
* 创建日期
*/
@CreationTimestamp
private Timestamp createTime; public Logging(String logType, Long time) {
this.logType = logType;
this.time = time;
}
}
package me.zhengjie.monitor.repository; import me.zhengjie.monitor.domain.Logging;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import java.util.List; /**
* @author jie
* @date 2018-11-24
*/
@Repository
public interface LoggingRepository extends JpaRepository<Logging,Long>, JpaSpecificationExecutor { /**
* 获取一个时间段的IP记录
* @param date1
* @param date2
* @return
*/
@Query(value = "select count(*) FROM (select * FROM log where createTime between ?1 and ?2 GROUP BY requestIp) as s",nativeQuery = true)
Long findIp(String date1, String date2);
}
package me.zhengjie.monitor.service.query; import me.zhengjie.monitor.domain.Logging;
import me.zhengjie.monitor.repository.LoggingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List; /**
* @author jie
* @date 2018-11-24
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class LoggingQueryService { @Autowired
private LoggingRepository loggingRepository; public Page queryAll(Logging logging, Pageable pageable){ return loggingRepository.findAll(new Spec(logging),pageable);
} public List queryAll(Logging logging){ return loggingRepository.findAll(new Spec(logging));
} class Spec implements Specification<Logging> { private Logging logging; public Spec(Logging logging){
this.logging = logging;
} @Override
public Predicate toPredicate(Root<Logging> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) { List<Predicate> list = new ArrayList<Predicate>(); if(!ObjectUtils.isEmpty(logging.getUsername())){
list.add(cb.like(root.get("username").as(String.class),"%"+logging.getUsername()+"%"));
} if (!ObjectUtils.isEmpty(logging.getLogType())) {
list.add(cb.equal(root.get("logType").as(String.class), logging.getLogType()));
} Predicate[] p = new Predicate[list.size()];
return cb.and(list.toArray(p));
}
}
}
LoggingService的更多相关文章
- .Net中的AOP系列之《单元测试切面》
返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle DynamicProxy测试 测试一个拦截器 注入依赖 ...
- 用Feature的方式删除SharePoint2010的Page中重复的WebPart
用Feature的方式删除SharePoint2010的Page中重复的WebPart. 代码如下所示: public class SupportCenterDuplicatedWebpartRemo ...
- 计划将项目中使用entity framework的要点记录到改栏目下
ef监控sql执行性能日志.http://www.cnblogs.com/CreateMyself/p/5277681.html http://123.122.205.38/cn_sql_server ...
- Weblogic 所有BEA错误代码详细信息列表
范围 子系统 类别 BEA-000001 – BEA-009999 ConsensusLeasing DatabaseLessLeasing DatabaseLessLeasing BEA-00010 ...
- 快速稳定的维护PHP
Just to recap, previously we'd have this sort of thing: namespace me\adamcameron\testApp; use Guzzle ...
- Net中的AOP
.Net中的AOP系列之<单元测试切面> 返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle ...
- 分布式内存网格Hazelcast源码导读
去年项目需要看了hazelcast源码,当时记录的笔记. Node是节点的抽象,里面包含节点引擎.客户端引擎.分区服务.集群服务.组播服务.连接管理.命令管理.组播属性.节点配置.本地成员.tcp地址 ...
- .NET Core TDD 前传: 编写易于测试的代码 -- 构建对象
该系列第1篇: 讲述了如何创造"缝". "缝"(seam)是需要知道的概念. 本文是第2篇, 介绍的是如何避免在构建对象时写出不易测试的代码. 本文的概念性内 ...
- Asp.Net Core中利用Seq组件展示结构化日志功能
在一次.Net Core小项目的开发中,掌握的不够深入,对日志记录并没有好好利用,以至于一出现异常问题,都得跑动服务器上查看,那时一度怀疑自己肯定没学好,不然这一块日志不可能需要自己扒服务器日志来查看 ...
随机推荐
- mysql初始化数据库建表脚本
set names utf8; set global validate_password.policy=LOW;set global validate_password.length=6;CREATE ...
- 201771010123汪慧和《面向对象程序设计Java》第十五周实验总结
一.理论部分 1.JAR文件 (1)Java程序的打包:程序编译完成后,程序员将.class文件压缩打包为.jar文件后,GUI界面 程序就可以直接双击图标运行. (2).jar文件(Java归档)既 ...
- PyMySQL学习笔记
一些常用函数及解释 db = pymysql.connect('host','user','password','database') # 连接数据库 cursor = db.cursor() # 创 ...
- opencv vs2013提示缺少Qedit.h问题
#pragma include_alias( "dxtrans.h", "qedit.h" ) #define __IDxtCompositor_INTERFA ...
- xv6的启动过程
bootloader 1. bootasm.S : start32 2. bootmain.c : bootmain kernel 3. main.c : main 4. proc.c : useri ...
- bitcoind
Bitcoin Core Daemon version v0.15.1.0-g7b57bc998f Usage: bitcoind [options] Start Bitcoin Core Daemo ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:Array(数组) 对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 斐波那契数列 yield 和list 生成
def fab_demo4(max): a,n,b = 0,0,1 while n < max: yield b # 生成器走到这一步返回b,需要再次调用才能继续执行 a,b = b,a+b n ...
- Vue.js——6.创建组件
Vue组件组件就是为了拆分Vue实例的代码量,能够不同的功能定义不同的组件创建组件的方法 1. // 创建组件 let com1=Vue.extend({ template:'<h1>he ...
- axios 等待同步请求用法及多请求并发
axios等待同步请求 直接上代码 首先在函数中返回一个Promise对象,在调用函数使用同步函数,调用目标函数使用await等待即可 参考http://www.cnblogs.com/cckui/p ...