1.搭建一个SpringBoot web环境

maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>
|
2.自定义注解
1 2 3 4 5 6 7 8
| @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Log { String module() default "";
String operator() default ""; }
|
3.切面和切点
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| @Slf4j @Aspect @Component public class LogAspect {
@Pointcut("@annotation(com.yang.aop.Log)") public void pointcut() { }
@Around("pointcut()") public Object log(ProceedingJoinPoint pjp) { Object result = null; long begin = System.currentTimeMillis(); try { result = pjp.proceed(); } catch (Throwable e) { e.printStackTrace(); } long time = System.currentTimeMillis() - begin;
log.info("result===>:{}",result); recordLog(pjp, time);
return result; }
private void recordLog(ProceedingJoinPoint pjp, long time) { MethodSignature signature = (MethodSignature) pjp.getSignature(); Method method = signature.getMethod(); log.info("=====================log start================================"); Log annotation = method.getAnnotation(Log.class); log.info("module:{}", annotation.module()); log.info("operator:{}", annotation.operator());
Object target = pjp.getTarget();
String methodName = signature.getName(); log.info("request method:{}", target.getClass() + "." + methodName + "()");
Object[] args = pjp.getArgs();
LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); String[] parameterNames = u.getParameterNames(method); if (args != null && parameterNames != null) { StringBuilder params = new StringBuilder(); for (int i = 0; i < args.length; i++) { params.append(" ").append(parameterNames[i]).append(": ").append(args[i]); } log.info("params:{}", params.toString()); }
HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); log.info("ip:{}", IpUtils.getIpAddr(request));
log.info("excute time : {} ms",time); log.info("=====================log end================================");
} }
|
两个工具类
HttpContextUtils
1 2 3 4 5 6 7 8 9 10
|
public class HttpContextUtils {
public static HttpServletRequest getHttpServletRequest() { return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); } }
|
IpUtils
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
|
@Slf4j public class IpUtils {
public static String getIpAddr(HttpServletRequest request) { String ip = null, unknown = "unknown", seperator = ","; int maxLength = 15; try { ip = request.getHeader("x-forwarded-for"); if (StringUtils.isEmpty(ip) || unknown.equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (StringUtils.isEmpty(ip) || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (StringUtils.isEmpty(ip) || unknown.equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (StringUtils.isEmpty(ip) || unknown.equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (StringUtils.isEmpty(ip) || unknown.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } } catch (Exception e) { log.error("IpUtils ERROR ", e); }
if (StringUtils.isEmpty(ip) && ip.length() > maxLength) { int idx = ip.indexOf(seperator); if (idx > 0) { ip = ip.substring(0, idx); } }
return ip; }
public static String getIpAddr() { HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); return getIpAddr(request); } }
|
4.测试
LogRecordController
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
| @RestController public class LogRecordController {
@Log(module = "日志记录模块",operator = "One") @GetMapping("/one") public void methodOne(String name) { }
@Log(module = "日志记录模块",operator = "Two") @GetMapping("/two") public void methodTwo() throws InterruptedException { Thread.sleep(2000); }
@Log(module = "日志记录模块",operator = "Three") @GetMapping("/three") public void methodThree(String name, String age) { }
@Log(module = "日志记录模块",operator = "Four") @GetMapping("/four") public List<String> methodFour(String name, String age) { List<String> list = new ArrayList<>(); list.add(name); list.add(age); return list;
} }
|
启动项目访问 http://localhost:8086/four?name=11&age=12
查看日志输出

最终项目工程结构图
