1.自定义注解
通过自定义注解,来标示方法需不需要进行记录日志,如果该方法在访问时需要记录日志,则在该方法上标示该注 解既可。
@Inherited
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OperateLog {
}
2.定义通知类
@Component
@Aspect
public class OperateAdvice {
private static Logger log = Logger.getLogger(OperateAdvice.class);
@Autowired
private OperationLogService operationLogService;
@Around("execution(* cn.itcast.controller.*.*(..)) && @annotation(operateLog)")
public Object insertLogAround(ProceedingJoinPoint pjp , OperateLog operateLog)
throws Throwable{
System.out.println(" ************************ 记录日志 [start]
****************************** ");
OperationLog op = new OperationLog();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
op.setOperateTime(sdf.format(new Date()));
op.setOperateUser(DataUtils.getRandStr(8));
op.setOperateClass(pjp.getTarget().getClass().getName());
op.setOperateMethod(pjp.getSignature().getName());
//获取方法调用时传递的参数
Object[] args = pjp.getArgs();
op.setParamAndValue(Arrays.toString(args));
long start_time = System.currentTimeMillis();
//放行
Object object = pjp.proceed();
long end_time = System.currentTimeMillis();
op.setCostTime(end_time - start_time);
if(object != null){
op.setReturnClass(object.getClass().getName());
op.setReturnValue(object.toString());
}else{
op.setReturnClass("java.lang.Object");
op.setParamAndValue("void");
}
log.error(JsonUtils.obj2JsonString(op));
operationLogService.insert(op);
System.out.println(" ************************** 记录日志 [end]
*************************** ");
return object;
}
}
3.方法上加注解
在需要记录日志的方法上加上注解@OperateLog。
@OperateLog
@RequestMapping("/insert")
public Result insert(@RequestBody Brand brand){
try {
brandService.insert(brand);
return new Result(true,"操作成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"操作失败");
}
}
4.日志查询后端代码实现
Mapper接口
public interface OperationLogMapper {
public void insert(OperationLog operationLog);
public List<OperationLog> selectListByCondition(Map dataMap);
public Long countByCondition(Map dataMap);
}
Mapper.xml 映射配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.itcast.mapper.OperationLogMapper" >
<insert id="insert" parameterType="operationLog">
INSERT INTO
operation_log(id,return_value,return_class,operate_user,operate_time,param_and_valu
e,
operate_class,operate_method,cost_time)
VALUES(NULL,#{returnValue},#{returnClass},#{operateUser},#{operateTime},#
{paramAndValue},
#{operateClass},#{operateMethod},#{costTime})
</insert>
<select id="selectListByCondition" parameterType="map"
resultType="operationLog">
select
id ,
operate_class as operateClass ,
operate_method as operateMethod,
return_class as returnClass,
operate_user as operateUser,
operate_time as operateTime,
param_and_value as paramAndValue,
cost_time as costTime,
return_value as returnValue
from operation_log
<include refid="oplog_where"/>
limit #{start},#{size}
</select>
<select id="countByCondition" resultType="long" parameterType="map">
select count(*) from operation_log
<include refid="oplog_where"/>
</select>
<sql id="oplog_where">
<where>
<if test="operateClass != null and operateClass != '' ">
and operate_class = #{operateClass}
</if>
<if test="operateMethod != null and operateMethod != '' ">
and operate_method = #{operateMethod}
</if>
<if test="returnClass != null and returnClass != '' ">
and return_class = #{returnClass}
</if>
<if test="costTime != null">
and cost_time = #{costTime}
</if>
</where>
</sql>
</mapper>
Service
@Service
@Transactional
public class OperationLogService {
//private static Logger logger = Logger.getLogger(OperationLogService.class);
@Autowired
private OperationLogMapper operationLogMapper;
//插入数据
public void insert(OperationLog operationLog){
operationLogMapper.insert(operationLog);
}
//根据条件查询
public PageResult selectListByCondition(Map dataMap, Integer pageNum , Integer
pageSize){
if(paramMap ==null){
paramMap = new HashMap();
}
paramMap.put("start" , (pageNum-1)*rows);
paramMap.put("rows",rows);
Object costTime = paramMap.get("costTime");
if(costTime != null){
if("".equals(costTime.toString())){
paramMap.put("costTime",null);
}else{
paramMap.put("costTime",new
Long(paramMap.get("costTime").toString()));
}
}
System.out.println(dataMap);
long countStart = System.currentTimeMillis();
Long count = operationLogMapper.countByCondition(dataMap);
long countEnd = System.currentTimeMillis();
System.out.println("Count Cost Time : " + (countEnd-countStart)+" ms");
List<OperationLog> list =
operationLogMapper.selectListByCondition(dataMap);
long queryEnd = System.currentTimeMillis();
System.out.println("Query Cost Time : " + (queryEnd-countEnd)+" ms");
return new PageResult(count,list);
}
}
Controller
@RestController
@RequestMapping("/operationLog")
public class OperationLogController {
@Autowired
private OperationLogService operationLogService;
@RequestMapping("/findList")
public PageResult findList(@RequestBody Map dataMap, Integer pageNum , Integer
pageSize){
PageResult page = operationLogService.selectListByCondition(dataMap,
pageNum, pageSize);
return page;
}
}