@Aop 记录请求日志案例
Posted 健身小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@Aop 记录请求日志案例相关的知识,希望对你有一定的参考价值。
import cn.yccj.ccb.gnet.common.model.ResponseJsonModel;
import cn.yccj.ccb.gnet.data.model.jpa.AccessLogEbo;
import cn.yccj.ccb.gnet.data.services.AccessLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
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.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@Aspect
@Component
public class LoggingDescriber {
@Autowired
private AccessLogService accessLogService;
@Pointcut("execution(org.springframework.http.ResponseEntity cn.yccj.ccb.gnet.apis.*.*(..))")
public void pc(){}
@Around("pc()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
AccessLogEbo accessLogEbo = new AccessLogEbo();
accessLogEbo.setClassName(pjp.getTarget().getClass().getName());
accessLogEbo.setMethodName(pjp.getSignature().getName());
accessLogEbo.setUrl(request.getRequestURI());
accessLogEbo.setKeyPoint(request.getParameter("username"));
StopWatch stopWatch = new StopWatch();
stopWatch.start();
try {
Object o = pjp.proceed();
ResponseEntity entity = (ResponseEntity)o;
if(entity.getBody() instanceof ResponseJsonModel){
accessLogEbo.setReturned(entity.getBody().toString());
}else if(entity.getBody() instanceof byte[]){
accessLogEbo.setReturned("file bytes.");
}
accessLogEbo.setHttpStatus(entity.getStatusCodeValue());
return o;
}catch (Throwable t){
accessLogEbo.setThrowable(ExceptionUtils.getStackTrace(t));
throw t;
}finally {
stopWatch.stop();
accessLogEbo.setTimeConsuming(stopWatch.getTotalTimeMillis());
accessLogService.save(accessLogEbo);
}
}
}
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.Date;
@Data
@Entity
@Table(name = "tb_access_log")
@EntityListeners(AuditingEntityListener.class)
public class AccessLogEbo {
@Id
@Column(name = "id",length = 32)
@GenericGenerator(name="idGenerator", strategy="uuid")
@GeneratedValue(generator = "idGenerator")
private String id;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@Column(name="access_at", updatable = false)
@CreatedDate
private Date accessAt;
@Column(name="url",length = 64)
private String url;
@Column(name="class_name",length = 64)
private String className;
@Column(name="method_name",length = 32)
private String methodName;
@Column(name="time_consuming",precision = 10)
private Long timeConsuming;
@Column(name="http_status",precision = 5)
private Integer httpStatus;
@Column(name="key_point",columnDefinition = "text")
private String keyPoint;
@Column(name="returned",columnDefinition = "text")
private String returned;
@Column(name="throwable",columnDefinition = "text")
private String throwable;
}
@Data
public class ResponseJsonModel {
private Boolean success=true;
private String errorMessage;
private String successMessage;
private Object mapper;
}
import cn.yccj.ccb.gnet.data.model.jpa.AccessLogEbo;
import cn.yccj.ccb.gnet.data.repositories.AccessLogEboRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class AccessLogService {
@Autowired
private AccessLogEboRepository accessLogEboRepository;
public void save(AccessLogEbo accessLogEbo){
accessLogEboRepository.save(accessLogEbo);
}
}
以上是关于@Aop 记录请求日志案例的主要内容,如果未能解决你的问题,请参考以下文章