使用AOP给springboot项目添加日志
发布日期:2021-05-14 06:37:50 浏览次数:11 分类:精选文章

本文共 6803 字,大约阅读时间需要 22 分钟。

AOP��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ������������������������������

AOP���������������

AOP������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Spring Boot������������AOP������������ AspectJ ���������������������

������Spring Boot������������������

������������������AOP������

������������������������������Spring Boot���������������������������������Spring AOP���������������������������������������������������������������������������Spring Boot Starter AOP���������

org.springframework.boot
spring-boot-starter-aop

���������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.component;
@/component
@public
class WebLogAspect {
private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
// ���������������������������
@Pointcut("execution(public * com.yt.controller.*.*(..))") // ������������������������
public void webLog() {}
// ������������������...
}

������������������

������������������������������������������������Spring���������������������������������������������������������������������������������������

import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotationAfterReturning;
import org.aspectj.lang.annotationAfterThrowing;
import org.aspectj.lang.JoinPoint;
@ component
@public
class WebLogAspect {
// ������������������...
@Pointcut("execution(public * com.yt.controller.*.*(..))")
public void webLog() {}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) {
// ���������������������������������
}
// ������������������...
}

������������������

���������������������������������������������������������������������������

1.-days������������������������������������������������ 2. ������������������������������������������ 3. ������������������������������������

###isodes

���������������������������������������

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.component;
@ component
@public
class WebLogAspect {
private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
@Autowired
private LogService logService;
@Autowired
private UserService userService;
private String url;
private String ip;
private Integer port;
private User user;
@Pointcut("execution(public * com.yt.controller..*.*(..))")
public void webLog() {}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// ch��� ���������GET������������������
if (!request.getMethod().equals("GET")) {
logger.info("������URL: " + request.getRequestURL().toString());
url = request.getRequestURI();
ip = request.getRemoteHost();
port = request.getRemotePort();
user = UserService.getCurrentUser();
}
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
Integer logType = LogType.OPERATION.getId();
if (ret instanceof ResultInfo) {
ResultInfo result = (ResultInfo) ret;
String msg = result.getMsg();
String module = result.getModule();
Integer code = result.getCode();
if (!StringUtils.isBlank(msg)) {
logger.info(msg);
// ���������������200���������������
if (code != null && code.equals(200)) {
logService.save( user, ip, port, msg, logType, module, url);
}
}
}
}
@AfterThrowing(pointcut = "webLog()", throwing = "e")
public void handleException(JoinPoint joinPoint, Throwable e) {
if (e != null) {
StringBuilder message = new StringBuilder( "���������������������" );
message.append( e.getMessage() );
Logger logger = LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringClass());
logger.error( message.toString(), e );
}
}
}

������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������controller���������������������������������

���������������������

���������������������������������������

  • execution(): ���������������
  • public *������������������������������������������������*���������������������
  • ������������������������������ com����t��r������������������������com���������������������������������������������
  • ���������*������������������
  • ������������*������������������������������������������
  • ..���������������������������������������
  • ������������������������"execution(public * com.yt.controller...(..))" ���������com.yt.controller������������������������������������������

    ���������������������

    ������������������������������������������������������������������������������������������������������������������������������������module���������������������������������������������������������������������������������

    • ���������������ResultInfo���������������module������
    • ���������������������������������������������������������������������������
    • ���������������������������������

    ������

    ������������������������������������������������Spring Boot������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������AOP������������������������������������������������������������������������������������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:Lambda表达式使用整理总结
    下一篇:实现html和word的相互转换(带图片)

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2025年04月23日 01时50分04秒