JAVA实现通用日志记录方法

 更新时间:2017年06月05日 16:15:24   作者:God_Ming  
本篇文章主要介绍了JAVA实现通用日志记录方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言:

之前想在filter层直接过滤httpServerletRequest请求进行日志处理,但是之后再getWriter()的 时候报already been call异常。查了下,才发现原来流形式的只能读取一次。。就好像食物,吃了就没了。。 所以在filter和inteceptor里面是没法通过获取request的流来进行日志记录的。

于是还是准备用通用的方法:controller层aop进行切面记录日志。

使用Aop记录操作日志

第一步:添加Aop

/**
 * 统一日志处理Handler
 * @author Mingchenchen
 *
 */
public class LogAopHandler {
  @Autowired
  private AuditLogDao auditLogDao;

  /**
   * controller层面记录操作日志
   * 注意此处是aop:around的 因为需要得到请求前的参数以及请求后接口返回的结果
   * @throws Throwable 
   */
  public Object doSaveLog(ProceedingJoinPoint joinPoint) throws Throwable { 
    MethodSignature method = (MethodSignature) joinPoint.getSignature();
    String methodName = method.getName();
    Object[] objects = joinPoint.getArgs();
    String requestBody = null;
    if (objects!=null && objects.length>0) {
      for (Object object : objects) {
        if (object == null) {
          requestBody = null;//POST接口参数为空 比如删除XXX
        }else if (object instanceof String) {
          requestBody = (String) object;//有些接口直接把参数转换成对象了
        }else {
          requestBody = JSONObject.toJSONString(object);
        }
      }
    }

    //只记录POST方法的日志
    boolean isNeedSaveLog = false;
    //此处不能用getAnnotationByType 是JAVA8的特性,因为注解能够重名,所以得到的是数组
    RequestMapping annotation = method.getMethod().getAnnotation(RequestMapping.class);
    for (RequestMethod requestMethod : annotation.method()) {
      if (requestMethod==RequestMethod.POST) {
        isNeedSaveLog = true;
      }
    }

    JSONObject requestBodyJson = null;
    try {
      requestBodyJson = JSONObject.parseObject(requestBody);
    } catch (Exception e) {
      //do nothing 即POST请求没传body
    }
    HttpServletRequest request = RequestContextUtil.getRequestByCurrentContext();
    String userName = RequestContextUtil.getUserNameByCurrentContext();
    if (StringUtil.isEmpty(userName)) {
      try {
        userName = DmsCache.get(requestBodyJson.getString("userName")).getName();
      } catch (Exception e) {
        userName = RequestContextUtil.getAsynUserInfoByAutoDeploy().getName();
      }
    }

    //得到request的参数后让方法执行它 
    //注意around的情况下需要返回result 否则将不会返回值给请求者
    Object result = joinPoint.proceed(objects);
    try {
      JSONObject resultJson = JSONObject.parseObject(result.toString());
      if (isNeedSaveLog) {//如果是POST请求 则记录日志
        LogTypeEnum logTypeEnum = LogTypeEnum.getDesByMethodName(methodName);
        if (logTypeEnum != null) {
          AuditLogEntity auditLogEntity = new AuditLogEntity();
          auditLogEntity.setUuid(StringUtil.createRandomUuid());
          auditLogEntity.setOperator(userName);
          auditLogEntity.setRequestIp(request.getRemoteAddr());
          auditLogEntity.setRequestUrl(request.getRequestURI().replace("/cloud-master", ""));
          auditLogEntity.setEventType(logTypeEnum.getKey());
          auditLogEntity.setEventDesc(logTypeEnum.getDescription());
          auditLogEntity.setRequest(requestBody);
          int isSuccess = "200".equals(resultJson.getString("code")) ? 1 : 0;
          auditLogEntity.setSuccessFlag(isSuccess);
          auditLogEntity.setResponse(result.toString());
          auditLogEntity.setCreateTime(new Date());
          auditLogDao.insert(auditLogEntity);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  } 
}

第二步:在spring的xml中声明

  <!-- 记录操作日志 -->
  <bean id="operationLogAop" class="com.ming.learn.core.aop.LogAopHandler"/>
   <aop:config>
    <aop:aspect id="logAOP" ref="operationLogAop">
     <aop:pointcut id="target" expression="execution(* com.ming.learn..*Controller.*(..))"/>
     <aop:around method="doSaveLog" pointcut-ref="target"/>
    </aop:aspect>
   </aop:config>

如此一来,核心步骤就完成了,剩下的就是自己组装需要记录的东西了。

第三步:写Dao、Entity、Mapper

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 日志审计
 * @author Mingchenchen
 *
 */
@Table(name="audit_log")
public class AuditLogEntity {
  @Id
  private String uuid;

  @Column(name="event_type")
  private String eventType;//事件类型

  @Column(name="event_desc")
  private String eventDesc;//事件中文描述

  @Column(name="operator")
  private String operator;//操作者

  @Column(name="request_ip")
  private String requestIp;//客户端地址

  @Column(name="request_url")
  private String requestUrl;//请求地址

  @Column(name="request")
  private String request;//请求body

  @Column(name="response")
  private String response;//请求返回值

  @Column(name="create_time")
  private Date createTime;

  public String getUuid() {
    return uuid;
  }

  public void setUuid(String uuid) {
    this.uuid = uuid;
  }

  public String getEventType() {
    return eventType;
  }

  public void setEventType(String eventType) {
    this.eventType = eventType;
  }

  public String getEventDesc() {
    return eventDesc;
  }

  public void setEventDesc(String eventDesc) {
    this.eventDesc = eventDesc;
  }

  public String getOperator() {
    return operator;
  }

  public void setOperator(String operator) {
    this.operator = operator;
  }

  public String getRequestIp() {
    return requestIp;
  }

  public void setRequestIp(String requestIp) {
    this.requestIp = requestIp;
  }

  public String getRequestUrl() {
    return requestUrl;
  }

  public void setRequestUrl(String requestUrl) {
    this.requestUrl = requestUrl;
  }

  public String getRequest() {
    return request;
  }

  public void setRequest(String request) {
    this.request = request;
  }

  public String getResponse() {
    return response;
  }

  public void setResponse(String response) {
    this.response = response;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }
}

第四步:根据Controller的方法名称定制响应的事件类型

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 操作日志类型
 * @author Mingchenchen
 *
 */
public enum LogTypeEnum {
  //用户
  COMMON_LOGIN("login","login","登录");
  //其他

  private String methodName;//方法名称与controller一致
  private String key;//保存到数据库的事件类型
  private String description;//保存到数据库的描述
  private LogTypeEnum(String methodName,String key,String description){
    this.methodName = methodName;
    this.key = key;
    this.description = description;
  }
  public String getMethodName() {
    return methodName;
  }
  public void setMethodName(String methodName) {
    this.methodName = methodName;
  }
  public String getKey() {
    return key;
  }
  public void setKey(String key) {
    this.key = key;
  }
  public String getDescription() {
    return description;
  }
  public void setDescription(String description) {
    this.description = description;
  }

  /**
   * 根据方法名返回
   * @param methodName
   * @return
   */
  public static LogTypeEnum getDesByMethodName(String methodName){
    return innerMap.map.get(methodName);
  }

  /**
   * 内部类 用户保存所有的enum 无须通过Enum.values()每次遍历
   * @author Mingchenchen
   *
   */
  private static class innerMap{
    private static Map<String, LogTypeEnum> map = new ConcurrentHashMap<>(128);

    static{
      //初始化整个枚举类到Map
      for (LogTypeEnum logTypeEnum : LogTypeEnum.values()) {
        map.put(logTypeEnum.getMethodName(), logTypeEnum);
      }
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • java实现简单聊天室单人版

    java实现简单聊天室单人版

    这篇文章主要为大家详细介绍了java实现简单聊天室的单人版,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Java中输入单个char类型的字符方式

    Java中输入单个char类型的字符方式

    这篇文章主要介绍了Java中输入单个char类型的字符方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Java线程池 ThreadPoolExecutor 详解

    Java线程池 ThreadPoolExecutor 详解

    这篇文章主要介绍了Java线程池 ThreadPoolExecutor,线程池包括线程集合、阻塞队列、拒绝策略处理器,更多相关内容需要的朋友可以参考一下
    2022-07-07
  • 解析Java中的Field类和Method类

    解析Java中的Field类和Method类

    这篇文章主要介绍了Java中的Field类和Method类,是Java入门学习中的基础知识,需要的朋友可以参考下
    2015-12-12
  • java 反射 动态调用不同类的静态方法(推荐)

    java 反射 动态调用不同类的静态方法(推荐)

    下面小编就为大家带来一篇JAVA 反射 动态调用不同类的静态方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • 超详细的Intellij IDEA 看源码必备技能

    超详细的Intellij IDEA 看源码必备技能

    这篇文章主要介绍了Intellij IDEA 看源码必备技能,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • 解决idea2020.2遇到pom.xml文件报错maven插件tomcat7的问题

    解决idea2020.2遇到pom.xml文件报错maven插件tomcat7的问题

    这篇文章主要介绍了idea2020.2遇到pom.xml文件报错maven插件tomcat7的问题,本文给大家分享解决方法,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • 剑指Offer之Java算法习题精讲数组与二叉树

    剑指Offer之Java算法习题精讲数组与二叉树

    跟着思路走,之后从简单题入手,反复去看,做过之后可能会忘记,之后再做一次,记不住就反复做,反复寻求思路和规律,慢慢积累就会发现质的变化
    2022-03-03
  • Java源码解析之GenericDeclaration详解

    Java源码解析之GenericDeclaration详解

    这篇文章主要介绍了Java源码解析之GenericDeclaration详解。有句古话说得好,源码能使人快乐!这里分享给大家,供需要的朋友参考。
    2017-10-10
  • JAVA生成pdf文件的实操教程

    JAVA生成pdf文件的实操教程

    PDF是可移植文档格式,是一种电子文件格式,具有许多其他电子文档格式无法相比的优点,下面这篇文章主要给大家介绍了关于JAVA生成pdf文件的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-11-11

最新评论