SpringBoot+Maven项目实战(6):整合Log4j和Aop,实现简单的日志记录
来源:互联网 发布:知乎怎么称呼别人 编辑:程序博客网 时间:2024/06/10 14:04
目录结构
pom.xml文件添加依赖
<!--log4j2--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId></dependency><!-- aop --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?><Configuration status="INFO"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> </Appenders> <Loggers> <Logger name="com.sbm" level="trace" additivity="false"> <AppenderRef ref="Console" /> </Logger> <Root level="error"> <AppenderRef ref="Console" /> </Root> </Loggers></Configuration>
Aop记录Log类:AppLogAspect
package com.sbm.aspects;import com.sbm.util.IPUtil;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import java.util.Arrays;import java.util.Enumeration;/** * sbm * Created by yadong.zhang on com.sbm.aspects * User:yadong.zhang * Date:2016/10/21 * Time:15:53 */@Component@Aspect/* * 定义切面执行的优先级,数字越低,优先级越高 * 在切入点之前执行:按order值有小到大的顺序执行 * 在切入点之后执行:按order值由大到小的顺序执行 */@Order(-5)public class AppLogAspect { private Logger logger = LogManager.getLogger(AppLogAspect.class); // 保证每个线程都有一个单独的实例 private ThreadLocal<Long> time = new ThreadLocal<>(); @Pointcut("execution(* com.sbm.controller..*.*(..))") public void pointcut() { } @Before("pointcut()") public void doBefore(JoinPoint joinPoint) { time.set(System.currentTimeMillis()); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //记录请求的内容 logger.info("Request URL: " + request.getRequestURL().toString()); logger.info("Request Method: " + request.getMethod()); logger.info("User-Agent: " + request.getHeader("User-Agent")); logger.info("Class Method: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); logger.info("Cookies: " + request.getCookies()); logger.info("Params: " + Arrays.toString(joinPoint.getArgs())); Enumeration<String> enums = request.getParameterNames(); while (enums.hasMoreElements()) { String paraName = enums.nextElement(); logger.info(paraName + ":" + request.getParameter(paraName)); } } @AfterReturning("pointcut()") public void doAfterReturning(JoinPoint joinPoint) { logger.info("耗时 : " + ((System.currentTimeMillis() - time.get())) + "ms"); logger.info("AppLogAspect.doAfterReturning()"); }}
简单的controller请求
@RequestMapping("/message/{currentPage}")public String message(@PathVariable("currentPage") Integer currentPage, Model model){ if(currentPage!= null){ PageHelper.startPage(currentPage, 11); } LOGGER.debug("程序执行的时候输出Log日志..."); List<Message> messages = messageService.list(); model.addAttribute("messages", messages); return "message";}
测试效果
0 0
- SpringBoot+Maven项目实战(6):整合Log4j和Aop,实现简单的日志记录
- SpringBoot+Maven项目实战(3):整合Freemark模板
- 采用AOP+log4j记录项目日志
- 采用AOP+log4j记录项目日志
- 采用AOP+log4j记录项目日志
- Spring AOP+Log4j记录项目日志
- springboot 自定义注解+AOP 实现日志记录
- AOP+log4j记录日志
- springboot AOP+日志的注解实现
- spring aop记录日志,Log4j
- 【Log4J 系列】:log4j 整合SpringMVC+MyBatis 实现日志记录(01)
- Spring Aop实现简单日志记录
- Spring + Aop+注解 集成使用 Log4j,实现异常日志记录
- SpringBoot 学习记录(五): aop记录日志
- Log4j +Mongodb整合 记录日志
- Servlet整合Log4j日志记录
- JavaScript中AOP的实现和日志记录
- 一个简单的SpringBoot整合Mybatis项目
- 接收http发送的报文,并且打印处理
- C 程序的内存空间布局
- 学会阅读Java字节码
- JMP caLL 机器码
- 熵与生命 概率
- SpringBoot+Maven项目实战(6):整合Log4j和Aop,实现简单的日志记录
- Mxnet学习系列3----网络的可视化
- Struts2单文件上传
- mtk打印spi注册log
- php验证类
- Android Studio 基本设置与运行
- 内存中堆和栈
- 从一个标准 url 里取出文件的扩展名
- 实用工具函数