<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
DROP TABLE IF EXISTS `rule_operate_log`; CREATE TABLE `rule_operate_log` ( id INT(11) NOT NULL AUTO_INCREMENT COMMENT '紀錄檔id', path VARCHAR(4000) NULL DEFAULT NULL COMMENT '介面地址', http_method VARCHAR(32) NULL DEFAULT NULL COMMENT '請求方法', status_code VARCHAR(32) NULL DEFAULT NULL COMMENT '請求返回狀態碼', create_time_char VARCHAR(32) NULL DEFAULT NULL COMMENT '紀錄檔時間', create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '紀錄檔時間戳', ip varchar(200) NULL DEFAULT NULL COMMENT '請求ip', params mediumtext NULL COMMENT '請求引數', result mediumtext NULL COMMENT '返回值', exception mediumtext NULL COMMENT '介面異常', user_id VARCHAR(32) NULL DEFAULT NULL COMMENT '操作使用者', user_account VARCHAR(32) NULL DEFAULT NULL COMMENT '操作使用者賬號', user_name VARCHAR(200) NULL DEFAULT NULL COMMENT '操作使用者名稱稱', user_org_id VARCHAR(32) NULL DEFAULT NULL COMMENT '操作使用者機構id', user_org_name VARCHAR(200) NULL DEFAULT NULL COMMENT '操作使用者機構名稱', operate_name VARCHAR(200) NULL DEFAULT NULL COMMENT '操作名稱', operate_position VARCHAR(200) NULL DEFAULT NULL COMMENT '操作位置', log_type VARCHAR(32) NULL DEFAULT NULL COMMENT '紀錄檔型別 error:錯誤紀錄檔 operate:操作紀錄檔', category_id VARCHAR(32) NULL DEFAULT NULL COMMENT '分類機構id', cost INT(11) NULL DEFAULT NULL COMMENT '介面耗時', PRIMARY KEY (id) ) COMMENT = '操作紀錄檔表';
import java.util.Date; public class RuleOperateLog { /** * id */ private Integer id; /** * 介面地址 */ private String path; /** * 請求方法 */ private String httpMethod; /** * 請求返回狀態碼 */ private String statusCode; /** * 紀錄檔時間 */ private String createTimeChar; /** * 紀錄檔時間戳 */ private Date createTime; /** * 請求ip */ private String ip; /** * 請求引數 */ private String params; /** * 返回值 */ private String result; /** * 介面異常 */ private String exception; /** * 操作使用者 */ private String userId; /** * 操作使用者賬號 */ private String userAccount; /** * 操作使用者名稱稱 */ private String userName; /** * 操作使用者機構 */ private String userOrgId; /** * 操作使用者機構名稱 */ private String userOrgName; /** * 操作名稱 */ private String operateName; /** * 操作位置 */ private String operatePosition; /** * 紀錄檔型別 */ private String logType; /** * 分類機構id */ private String categoryId; /** * 請求耗時 */ private Integer cost; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getHttpMethod() { return httpMethod; } public void setHttpMethod(String httpMethod) { this.httpMethod = httpMethod; } public String getStatusCode() { return statusCode; } public void setStatusCode(String statusCode) { this.statusCode = statusCode; } public String getCreateTimeChar() { return createTimeChar; } public void setCreateTimeChar(String createTimeChar) { this.createTimeChar = createTimeChar; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getException() { return exception; } public void setException(String exception) { this.exception = exception; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserAccount() { return userAccount; } public void setUserAccount(String userAccount) { this.userAccount = userAccount; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserOrgId() { return userOrgId; } public void setUserOrgId(String userOrgId) { this.userOrgId = userOrgId; } public String getUserOrgName() { return userOrgName; } public void setUserOrgName(String userOrgName) { this.userOrgName = userOrgName; } public String getOperateName() { return operateName; } public void setOperateName(String operateName) { this.operateName = operateName; } public String getOperatePosition() { return operatePosition; } public void setOperatePosition(String operatePosition) { this.operatePosition = operatePosition; } public String getLogType() { return logType; } public void setLogType(String logType) { this.logType = logType; } public String getCategoryId() { return categoryId; } public void setCategoryId(String categoryId) { this.categoryId = categoryId; } public Integer getCost() { return cost; } public void setCost(Integer cost) { this.cost = cost; } }
import com.xxx.xxx.xxx.entity.RuleOperateLog; /** * 操作紀錄檔(RuleOperateLog)表資料庫存取層 * * @author hx * @since 2022-08-23 */ public interface RuleOperateLogDao { /** * 新增資料 * * @param operateLog * @return */ int insert(RuleOperateLog operateLog); }
<?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="com.xxx.xxx.xxx.dao.RuleOperateLogDao"> <resultMap type="com.xxx.xxx.xxx.entity.RuleOperateLog" id="RuleOperateLogMap"> <result property="id" column="id" jdbcType="INTEGER"/> <result property="path" column="path" jdbcType="VARCHAR"/> <result property="httpMethod" column="http_method" jdbcType="VARCHAR"/> <result property="statusCode" column="status_code" jdbcType="VARCHAR"/> <result property="createTimeChar" column="create_time_char" jdbcType="VARCHAR"/> <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/> <result property="ip" column="ip" jdbcType="VARCHAR"/> <result property="params" column="params" jdbcType="VARCHAR"/> <result property="result" column="result" jdbcType="VARCHAR"/> <result property="exception" column="exception" jdbcType="VARCHAR"/> <result property="userId" column="user_id" jdbcType="VARCHAR"/> <result property="userAccount" column="user_account" jdbcType="VARCHAR"/> <result property="userName" column="user_name" jdbcType="VARCHAR"/> <result property="userOrgId" column="user_org_id" jdbcType="VARCHAR"/> <result property="userOrgName" column="user_org_name" jdbcType="VARCHAR"/> <result property="operateName" column="operate_name" jdbcType="VARCHAR"/> <result property="operatePosition" column="operate_position" jdbcType="VARCHAR"/> <result property="logType" column="log_type" jdbcType="VARCHAR"/> <result property="categoryId" column="category_id" jdbcType="VARCHAR"/> <result property="cost" column="cost" jdbcType="INTEGER"/> </resultMap> <insert id="insert" keyProperty="id" useGeneratedKeys="true"> insert into rule_operate_log (id, path, http_method, status_code, create_time_char, create_time, ip, params, result, exception, user_id, user_account, user_name, user_org_id, user_org_name, operate_name, operate_position, log_type, category_id, cost) values (#{id}, #{path}, #{httpMethod}, #{statusCode}, #{createTimeChar}, #{createTime}, #{ip}, #{params}, #{result}, #{exception},#{userId}, #{userAccount}, #{userName}, #{userOrgId}, #{userOrgName}, #{operateName}, #{operatePosition}, #{logType}, #{categoryId}, #{cost}) </insert> </mapper>
import com.xxx.xxx.xxx.entity.RuleOperateLog; /** * 操作紀錄檔(RuleOperateLog)表服務介面 * * @author hx * @since 2022-08-23 */ public interface RuleOperateLogService { /** * 儲存紀錄檔 * * @param ruleOperateLog * @return */ void saveLog(RuleOperateLog ruleOperateLog); }
import com.xxx.xxx.xxx.dao.RuleOperateLogDao; import com.xxx.xxx.xxx.entity.RuleOperateLog; import com.xxx.xxx.xxx.service.RuleOperateLogService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * 操作紀錄檔(RuleOperateLog)表服務實現類 * * @author hx * @since 2022-08-23 */ @Service("RuleOperateLogService") public class RuleOperateLogServiceImpl implements RuleOperateLogService { @Autowired private RuleOperateLogDao operateLogDao; @Override public void saveLog(RuleOperateLog ruleOperateLog) { operateLogDao.insert(ruleOperateLog); } }
import java.lang.annotation.*; @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface LogResource { /** * 服務名稱 * @return */ String name(); /** * 操作位置描述 * @return */ String position() default ""; /** * 紀錄檔型別 * @return */ String logType() default ""; }
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.com.xxx.xxx.xxx.annotation.LogResource; import com.com.xxx.xxx.xxx.constants.LogTypeConstants; import com.com.xxx.xxx.xxx.entity.RuleOperateLog; import com.com.xxx.xxx.xxx.service.RuleOperateLogService; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; /** * 操作紀錄檔切面類 * * @author hx * @since 2022-08-23 */ @Aspect @Component public class OperateLogAspect { @Autowired private RuleOperateLogService operateLogService; //掃描使用@LogResource註解的方法 @Pointcut("@annotation(com.com.xxx.xxx.xxx.annotation.LogResource)") public void logPointCut() { }; @Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { Date startTime = new Date(); String exception = null; String result = null; try { Object obj = point.proceed(); if (obj != null) { result = JSONObject.toJSONString(obj); } return obj; } catch (Exception e) { //請求時報錯 exception = e.toString(); throw e; } finally { //操作和報錯紀錄檔都記錄 HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); int statusCode = response.getStatus(); if (exception != null) { /** CHECKSTYLE:OFF:MagicNumber */ statusCode = 500; /** CHECKSTYLE:ON:MagicNumber */ } syncSaveLog(point, startTime, new Date(), exception, result, statusCode); } } @Async void syncSaveLog(ProceedingJoinPoint joinPoint, Date startTime, Date endTime, String exception, String result, int statusCode) { RuleOperateLog log = new RuleOperateLog(); try { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); LogResource annotation = method.getAnnotation(LogResource.class); if (annotation != null) { //註解上的描述 log.setOperateName(annotation.name()); } Date nowDate = new Date(); log.setCreateTimeChar(new SimpleDateFormat("yyyyMMddhhmmss").format(nowDate)); log.setCreateTime(nowDate); //入參 if (joinPoint.getArgs() != null) { try { log.setParams(JSONObject.toJSONString(joinPoint.getArgs(), SerializerFeature.IgnoreNonFieldGetter)); } catch (Exception e) { e.printStackTrace(); } } Long cost = endTime.getTime() - startTime.getTime(); log.setCost(cost.intValue()); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); if (request != null) { log.setUserName(request.getHeader(HttpHeaders.USER_AGENT)); log.setPath(request.getRequestURI()); log.setHttpMethod(request.getMethod()); log.setIp(request.getRemoteAddr()); } log.setStatusCode(String.valueOf(statusCode)); log.setResult(result); /** CHECKSTYLE:OFF:MagicNumber */ if (statusCode > 400 && exception != null) { log.setException(exception); log.setLogType(LogTypeConstants.ERROR); } else { log.setLogType(LogTypeConstants.OPERATE); } /** CHECKSTYLE:ON:MagicNumber */ operateLogService.saveLog(log); } catch (Exception e) { e.printStackTrace(); } /* //啟動一個執行緒,執行報錯紀錄檔防止影響主請求 new Thread() { @Override public void run() { try { //儲存到資料庫 operLogMapper.insertOper(operLog); } catch (Exception e) { e.printStackTrace(); } } }.start();*/ } }
到此這篇關於Spring AOP實現介面請求記錄到資料庫的文章就介紹到這了,更多相關Spring AOP介面請求記錄資料庫內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45