SpringBoot+Aop实现记录日志
- maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
- 自定义WebLog
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface WebLog {
String description() default "";
}
- WebAspect
@Aspect
@Component
public class WebLogAspect {
private final static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
private static final String LINE_SEPARATOR = System.lineSeparator();
private Long startTime;
@Autowired
SysLogService sysLogService;
@Pointcut("@annotation(com.li.thesismanagement.controller.system.logs.WebLog)")
public void webLog() {
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String methodDescription = getAspectLogDescription(joinPoint);
logger.info("========================================== Start ==========================================");
logger.info("URL : {}", request.getRequestURL().toString());
logger.info("Description : {}", methodDescription);
logger.info("HTTP Method : {}", request.getMethod());
logger.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
logger.info("IP : {}", request.getRemoteAddr());
logger.info("Request Args : {}", joinPoint.getArgs());
SysLogs sysLogs=new SysLogs();
sysLogs.setIp(request.getRemoteAddr());
sysLogs.setGmtCreate(TimeUtils.getCurrentTime());
sysLogs.setOperation(methodDescription);
sysLogs.setMethod( request.getRequestURL().toString());
SessionInfo currentuser = (SessionInfo) request.getSession().getAttribute("currentuser");
sysLogs.setUsername(currentuser.getName());
sysLogs.setUserId(currentuser.getId());
sysLogService.addLogs(sysLogs);
}
@After("webLog()")
public void doAfter() throws Throwable {
logger.info("=========================================== End ===========================================" + LINE_SEPARATOR);
}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
logger.info("Response Args : {}", new Gson().toJson(result));
logger.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
return result;
}
- 日志Sql表
CREATE TABLE `NewTable` (
`id` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`user_id` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户id' ,
`username` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名' ,
`operation` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户操作' ,
`time` int(11) NULL DEFAULT NULL COMMENT '响应时间' ,
`method` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '请求方法' ,
`params` varchar(5000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '请求参数' ,
`ip` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'IP地址' ,
`gmt_create` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
COMMENT='系统日志'
ROW_FORMAT=COMPACT
;