All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.doudou.common.tool.config.JapSqlLogAspect Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package top.doudou.common.tool.config;

import lombok.extern.slf4j.Slf4j;
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.springframework.stereotype.Component;
import top.doudou.common.tool.aspect.WriteLogToFile;
import top.doudou.common.tool.config.entity.SqlLogDto;

/**
 * @author 傻男人<[email protected]>
 * @description  jpa语句输出到文件
 * @date 2020-07-27
 */
@Aspect
@Component
@Slf4j
public class JapSqlLogAspect {

    private static final String SQL_LOG = "jpa_sql_log.log";
    @Around("crud()")
    public Object logPerformance(ProceedingJoinPoint pjp) throws Throwable{
        long start = System.currentTimeMillis();
        SqlLogDto sqlLogDto = new SqlLogDto();
        String name="-";
        String result="Y";
        try{
            name = pjp.getSignature().toShortString();
            sqlLogDto.setSqlId(name);
            return pjp.proceed();
        }catch (Throwable t){
            result = "N";
            throw t;
        }finally {
            long end =System.currentTimeMillis();
            log.info("{};{};{}ms",name,result,end-start);
            sqlLogDto.setCost(end);
            printSql(sqlLogDto);
        }

    }

    @Pointcut("execution(* *..*.*repository..*(..))")
    private void crud(){}

    private void printSql(SqlLogDto sqlLogDto){
        if(null == sqlLogDto){
            return;
        }
        System.out.println("==>  sql id                " + sqlLogDto.getSqlId());
        System.out.println("==>  sql sentence          " + sqlLogDto.getSentence());
        System.out.println("==>  sql cost              " + sqlLogDto.getCost());
        WriteLogToFile.logToFile(SQL_LOG,sqlLogDto.toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy