在SpringBoot项目中使用切面记录系统日志
导入aop依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
|
创建日志类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Data public class SysLog {
private Long id;
@JsonFormat(pattern = "yyy-MM-dd HH:mm:ss") private Date visitTime;
private String ip; private String uri; private String username;
private String method; private Long execTime; }
|
先创建查看日志的接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @RestController @RequestMapping("/syslog") public class SysLogController {
@Autowired private SysLogService sysLogService;
@GetMapping public ResponseEntity<List<SysLog>> querySysLog() { List<SysLog> sysLogs = sysLogService.querySysLog(); if (CollectionUtils.isEmpty(sysLogs)) { return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); } return ResponseEntity.ok(sysLogs); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| @Service public class SysLogService {
@Autowired private StringRedisTemplate redisTemplate;
private ObjectMapper objectMapper = new ObjectMapper();
private final String KEY = "syslog";
public List<SysLog> querySysLog() { BoundListOperations<String, String> ops = redisTemplate.boundListOps(KEY);
return ops.range(0, -1).stream() .map(s -> { try { return objectMapper.readValue(s, SysLog.class); } catch (JsonProcessingException e) { e.printStackTrace(); return null; } }).collect(Collectors.toList()); }
public void save(SysLog sysLog) { if (sysLog == null) { return; }
BoundListOperations<String, String> ops = redisTemplate.boundListOps(KEY); try { ops.rightPush(objectMapper.writeValueAsString(sysLog)); } catch (JsonProcessingException e) { e.printStackTrace(); } } }
|
创建AOP切面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| @Component @Aspect @Slf4j public class SysLogAop {
private Class clazz; private String methodName; private Date visitTime;
@Autowired private HttpServletRequest request; @Autowired private SysLogService sysLogService;
@Pointcut("execution(* com.tan.controller.*.*(..))") public void controllerPointcut() { }
@Before("controllerPointcut()") public void before(JoinPoint joinPoint) { visitTime = new Date(); clazz = joinPoint.getTarget().getClass(); methodName = joinPoint.getSignature().getName(); }
@After("controllerPointcut()") public void after(JoinPoint joinPoint) { if (clazz == SysLogController.class) { return; }
String username = "user"; long execTime = System.currentTimeMillis() - visitTime.getTime(); String ip = request.getRemoteAddr(); String uri = request.getRequestURI(); SysLog sysLog = new SysLog(); sysLog.setVisitTime(visitTime); sysLog.setExecTime(execTime); sysLog.setMethod(methodName); sysLog.setIp(ip); sysLog.setUri(uri); sysLog.setUsername(username); sysLogService.save(sysLog); log.info("SysLog: {}", sysLog); } }
|
重启服务,此后我们所访问controller都会被记录下来啦。