AOP切面对日志的打印及管理

package org.skyviewpacs.service.task;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.skyviewpacs.common.ServerResponse;
import org.skyviewpacs.common.util.DateTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

@Aspect
@Component
public class HttpAspect {
    
    
	private Logger log = LoggerFactory.getLogger(HttpAspect.class);
    String methodName;      // 方法名
    long startTime;         // 开始时间

    /**
     * 切入点
     */
    @Pointcut("execution( * org.skyviewpacs.us.controller.*.*.*(..))")
    public void aopPointCut() {
    
    }

    /**
     * 统计方法耗时环绕通知
     * @param joinPoint
     */
    // @Around("aopPointCut()")
    // public Object timeAround(ProceedingJoinPoint joinPoint) {
    
    
    //     long startTime;
    //     long E_time;
    //     Object obj;
    //     try {
    
    
    //         // 获取开始时间
    //         startTime = System.currentTimeMillis();
    //         // 获取返回结果集
    //         obj = joinPoint.proceed(joinPoint.getArgs());
    //         // 获取方法执行时间
    //         E_time = System.currentTimeMillis() - startTime;
    //     } catch (Throwable t) {
    
    
    //         throw new MethodRunningTimeException(ResultEnum.METHOD_RUNNING_TIME_ERROR, t);
    //     }
    //     String classAndMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
    //     log.info("执行 " + classAndMethod + " 耗时为:" + E_time + "ms");
    //     return obj;
    // }

    @Before("aopPointCut()")
    public void doBefore(JoinPoint joinPoint) {
    
    
        methodName = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        log.info("执行 " + methodName + " 开始时间为:"+DateTimeUtil.getFormatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
        System.out.println("执行 " + methodName + " 开始时间为:"+DateTimeUtil.getFormatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
        startTime = System.currentTimeMillis();
    }

    @After("aopPointCut()")
    public void doAfter() {
    
    
        long E_time = System.currentTimeMillis() - startTime;
        log.info("执行 " + methodName + " 结束时间为:"+DateTimeUtil.getFormatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
        System.out.println("执行 " + methodName + " 结束时间为:"+DateTimeUtil.getFormatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
        log.info("执行 " + methodName + " 耗时为:" + E_time + "ms");
        System.out.println("执行 " + methodName + " 耗时为:" + E_time + "ms");
    }

    @AfterReturning(returning = "object", pointcut = "aopPointCut()")
    public void doAfterReturning(ServerResponse<?> object) {
    
    
        log.info("response={}", JSON.toJSON(object));
    }}

效果如下

2020-11-24 09:16:03.426 SvUSService [http-nio-8082-exec-2] INFO  org.skyviewpacs.service.task.HttpAspect - 执行 org.skyviewpacs.us.controller.business.BusnissQueryHandllerController.getQueueCallInfo 开始时间为:2020-11-24 09:16:03:426
执行 org.skyviewpacs.us.controller.business.BusnissQueryHandllerController.getQueueCallInfo 开始时间为:2020-11-24 09:16:03:426
2020-11-24 09:16:03.426 SvUSService [http-nio-8082-exec-2] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2020-11-24 09:16:03.426 SvUSService [http-nio-8082-exec-2] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@20c5b18d] was not registered for synchronization because synchronization is not active
2020-11-24 09:16:03.429 SvUSService [http-nio-8082-exec-2] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [HikariProxyConnection@771906533 wrapping ConnectionID:1 ClientConnectionId: 9edd485b-814f-491c-8328-16e0359d85a6] will not be managed by Spring
2020-11-24 09:16:03.429 SvUSService [http-nio-8082-exec-2] DEBUG org.skyviewpacs.model.dao.localMaster.VReservationqueueMapper.selectByExample - ==>  Preparing: select auto_id, serial_no, reg_no, person_name, sex, dept_name, status, queue_date, st_number, check_type, age, window_name, book_Number, body_Part, peri_Level, arrival_status, queue_time, lock_sign, request_id, critical_sign, vip_sign, triage_doctor, triage_dept, R_ID, R_Number, R_InstanceID, R_Name, R_EnglishName, R_Country, R_Nationality, R_Gender, R_Weight, R_Height, R_Birthday, R_Age, R_Phone, R_Address, R_Occupation, R_Date, R_Time, R_PrivateData, R_State, R_SSAET, R_SPSSD, R_SPSST, R_Modality, R_SubModality, R_BedNumber, R_BodyPartExamines, R_OutPatientNumber, R_CheckInNumber, R_Equipment, R_OperatingPhys, R_SectionIn, R_RequestingOffice, R_PerformancePhys, R_ReasonForStudy, R_RequestingPhys, R_BodyPosition, R_AccessionNumber, R_RequestID, R_Description, R_InHospitalNumber, R_Fee, R_BackToHis, R_Age_Unit, R_Age_English_Unit, R_English_Gender, R_Check_Type, R_Patient_Type, R_Patient_Source, R_Operation_Nurse, R_Operation_Engineer, R_Scan_Type, R_ArchNumber, R_HospitalCode, R_Card_Type, R_Card_Number, R_WorkUnit, R_Memo, R_Fee_Type, R_ExamineItem, R_Identity_Card, R_Delete_Flag, R_Arrival_State, R_Request_Items, R_Noon_Type, R_Film_Type, R_Film_Amount, R_Registrant, R_Subspecialty_Code, R_Fee_Remark, R_Period_Time, R_Emergency_Flag, R_Visit_Serial_No from V_ReservationQueue WHERE ( R_Modality = ? and book_Number = ? and R_HospitalCode = ? and queue_date between ? and ? ) 
2020-11-24 09:16:03.429 SvUSService [http-nio-8082-exec-2] DEBUG org.skyviewpacs.model.dao.localMaster.VReservationqueueMapper.selectByExample - ==> Parameters: US(String), (String), 1000200,1000200(String), 2020-11-11 00:00:00.0(Timestamp), 2020-11-11 23:59:59.999(Timestamp)
2020-11-24 09:16:03.434 SvUSService [http-nio-8082-exec-2] DEBUG org.skyviewpacs.model.dao.localMaster.VReservationqueueMapper.selectByExample - <==      Total: 0
2020-11-24 09:16:03.434 SvUSService [http-nio-8082-exec-2] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@20c5b18d]
2020-11-24 09:16:03.434 SvUSService [http-nio-8082-exec-2] INFO  org.skyviewpacs.service.task.HttpAspect - 执行 org.skyviewpacs.us.controller.business.BusnissQueryHandllerController.getQueueCallInfo 结束时间为:2020-11-24 09:16:03:434
执行 org.skyviewpacs.us.controller.business.BusnissQueryHandllerController.getQueueCallInfo 结束时间为:2020-11-24 09:16:03:434
2020-11-24 09:16:03.434 SvUSService [http-nio-8082-exec-2] INFO  org.skyviewpacs.service.task.HttpAspect - 执行 org.skyviewpacs.us.controller.business.BusnissQueryHandllerController.getQueueCallInfo 耗时为:8ms
执行 org.skyviewpacs.us.controller.business.BusnissQueryHandllerController.getQueueCallInfo 耗时为:8ms

猜你喜欢

转载自blog.csdn.net/weixin_44215804/article/details/110038496