36 lines
1.0 KiB
Markdown
36 lines
1.0 KiB
Markdown
# 统一记录日志
|
|
|
|
---
|
|
|
|
AOP
|
|
|
|

|
|
|
|
```java
|
|
@Component
|
|
@Aspect
|
|
public class ServiceLogAspect {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ServiceLogAspect.class);
|
|
|
|
@Pointcut("execution(* com.greate.community.service.*.*(..))")
|
|
public void pointcut() {
|
|
|
|
}
|
|
|
|
@Before("pointcut()")
|
|
public void before(JoinPoint joinPoint) {
|
|
// 用户[IP 地址], 在某个时间访问了 [com.greate.community.service.xxx]
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
HttpServletRequest request = attributes.getRequest();
|
|
String ip = request.getRemoteHost();
|
|
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
String target = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
|
|
logger.info(String.format("用户[%s], 在[%s], 访问了[%s].", ip, time, target));
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|