一、首先定义一个log的日志(TLog.java);
package entity; import java.util.Date; public class TLog { private Integer id; private String type; private Integer operator; private Date createtime; private String data; private String ip; private String content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Integer getOperator() { return operator; } public void setOperator(Integer operator) { this.operator = operator; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getData() { return data; } public void setData(String data) { this.data = data; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
二、用自定义注解来来定义接口(Log.java);
@Target({ElementType.METHOD})//指定注解添加到method方法上 @Retention(RetentionPolicy.RUNTIME)//指定作用域为运行时 public @interface Log { //定义接口方法,使用接口时,会当做注解的属性 String description() default ""; //日志操作类型,删除,修改,登录,查询,新增 LogType logType(); }
三、利用枚举来定义日志的操作(LogType.java);
/** * 枚举类,用来定义有限个的对象 */ public enum LogType { LOGIN("1"),INSERT("2"),DELETE("3") ,SELECT("4"),UPDATE("5"),PASSWORD("6"); //定义类中的属性和方法,跟写其他类一样 private String type; LogType(String type){ this.type=type; } public String getType() { return type; } }
四、分别在mapper层和service层定义insert方法;
mapper层: LogMapper.java public interface LogMapper { int insert(TLog tLog); } LogMapper.xml "1.0" encoding="UTF-8"?> "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">namespace="mapper.LogMapper"> service层: LogService.java public interface LogService { int insert(TLog tLog); } LogServiceImpl.java @Service public class LogServiceImpl implements LogService{ @Autowired private LogMapper logMapper; @Override public int insert(TLog tLog) { return logMapper.insert(tLog); } }"insert"> insert into T_LOG(id, type, operator, createtime, data, ip, content) values (SEQ_T_LOG.nextval,#{type},#{operator},#{createtime},#{data},#{ip},#{content})
五、我们最后写一个切面的类来实现我们的日志(LogAspect.java);
@Component @Aspect public class LogAspect2 { @Autowired private LogService logService; //定义切入点,使用注解的方法 @Pointcut("@annotation(aop.Log)") public void logPointCut(){ } @Before("logPointCut()") public void log(JoinPoint joinPoint) throws JsonProcessingException { //获取日志信息,封装成日志对象,并保存到数据库 TLog tLog = new TLog(); //获取到方法签名对象 MethodSignature signature= (MethodSignature) joinPoint.getSignature(); //通过方法签名对象获取方法对象 Method method = signature.getMethod(); //获取方法上面的特定注解 Log logAnnotation = method.getAnnotation(Log.class); //描述信息 String description = logAnnotation.description(); //日志类型 LogType logType = logAnnotation.logType(); //获取到日志类型对应的数字,存入tLog对象中 tLog.setType(logType.getType()); //设置操作时间 tLog.setCreatetime(new Date()); //获取request对象 ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = requestAttributes.getRequest(); //获取客户端IP地址 String ip = request.getRemoteUser(); tLog.setIp(ip); //获取请求数据 MapparameterMap = request.getParameterMap(); //创建JackSon的ObjectMapper对象,通过该对象将java对象转换成json ObjectMapper objectMapper = new ObjectMapper(); //将java对象转换成json字符串 String requestDataJsonStr = objectMapper.writeValueAsString(parameterMap); //将json字符串转换成java对象 // HashMap map=objectMapper.readValue(s, HashMap,class); // String content=description+",请求数据:"+requestDataJsonStr; tLog.setContent(description); //设置请求数据 tLog.setData(requestDataJsonStr); if (!(LogType.LOGIN ==logType)){//如果不是登录操作,获取操作人信息 Admin admin = (Admin) request.getSession().getAttribute("admin"); tLog.setOperator(admin.getId()); } //将日志信息保存到数据库 logService.insert(tLog); } }
六、为了测试结果,我们可以在事先写好的增删改查,登录或修改密码的方法上加上对应的注解,然后我们执行有注解的相关操作即可,就可以在控制台看到相应的日志信息,或者在数据库中看到相应的日志信息;
1)加入的注解;
2)控制台的相关信息;
3)数据库表中页面;