<!--aop依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
AOP拦截类
/**
* 作者:张风捷特烈
* 时间:2018/8/17 0017:11:06
* 邮箱:1981462002@qq.com
* 说明:AOP+log4j 记录访问请求信息
*/
@Aspect
@Component
public class LogAspect {
private static final Logger sLogger = Logger.getLogger(LogAspect.class);
/**
* 拦截com.toly1994.toly_mybatis.controller包下所以方法
*/
@Pointcut("execution(public * com.toly1994.toly_mybatis.controller.*.*(..))")
public void log() {
}
/**
* 前置通知
*
* @param joinPoint
*/
@Before("log()")//log()方法之前
public void doBefore(JoinPoint joinPoint) {
//接收请求,记录请求
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = sra.getRequest();
//记录日志
sLogger.info("url" + request.getRequestURI().toString());
sLogger.info("method" + request.getMethod());
sLogger.info("ip" + request.getRemoteAddr());
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
sLogger.info("name:" + name + ",value:{" + request.getParameter(name) + "}");
}
}
/**
* 后置通知
* @param ret
*/
@AfterReturning(returning = "ret", pointcut = " log() ")
public void doAfter(Object ret) {
sLogger.info("response" + ret);//处理完成,返回
}
}
访问上篇的接口:http://localhost:8080/findByName?name=赤凰
12:04:39,451 INFO LogAspect:46 - url/findByName
12:04:39,453 INFO LogAspect:47 - methodGET
12:04:39,454 INFO LogAspect:48 - ip0:0:0:0:0:0:0:1
12:04:39,455 INFO LogAspect:53 - name:name,value:{赤凰}
12:04:39,460 INFO LogAspect:60 - responseSword(id=24, name=赤凰, atk=0, hit=100, crit=5, attr_id=1, type_id=2)
附录:log4j 简单配置。参考了这里,更多配置见
log4j.properties
#INFO级别 文件输出 控制台输出
log4j.rootLogger=INFO, FILE, CONSOLE
#将日志信息输出到磁盘
log4j.appender.FILE=org.apache.log4j.FileAppender
#磁盘路径
log4j.appender.FILE.File=F:/SpringBootFiles/log/log.txt
#缓存大小,$$:8k
log4j.appender.FILE.BufferSize=8192
#请求的日志消息被立即输出,$$:true
log4j.appender.FILE.ImmediateFlush=true
#日志存储到缓存当中,当缓存满了后才输出到磁盘文件中,$$:false,(与ImmediateFlush互斥)
log4j.appender.FILE.BufferedIO=false
#指定日志输出的最低级别,$$:DEBUG
log4j.appender.FILE.Threshold=DEBUG
#追加模式添加,$$:true
log4j.appender.FILE.Append=true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
################################################################
#将日志信息输出到控制台中
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
#将日志信息使用System.out.println输出到控制台
log4j.appender.CONSOLE.Target=System.out
#请求的日志消息被立即打印,$$:true
log4j.appender.CONSOLE.ImmediateFlush=true
#指定日志打印的最低级别,$$:DEBUG
log4j.appender.CONSOLE.Threshold=DEBUG
#打印编码
log4j.appender.CONSOLE.encoding=UTF-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
网友评论