Python 官方文档:入门教程 => 点击学习
目录1.导入相关的依赖 2.log4j2 日志文件 3.dao层的接口以及实现类 4.Service层业务实现类 5.Controller层接口控制类 6.编写业务类增强类,加入一个
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions> <!-- 去掉SpringBoot自带的日志,不然会冲突 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<!-- log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<!-- log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<!-- slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- slf4j-simple 解决冲突-->
<!--<dependency>-->
<!--<groupId>org.slf4j</groupId>-->
<!--<artifactId>slf4j-simple</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-WEB</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration 后面的 status 用于设置 log4j2 自身内部的信息输出,可以不设置,当设置成 trace 时,可以看到 log4j2 内部各种详细输出-->
<configuration status="INFO">
<!--先定义所有的 appender-->
<appenders>
<!--输出日志信息到控制台-->
<console name="Console" target="SYSTEM_OUT">
<!--控制日志输出的格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</console>
</appenders>
<!--然后定义 logger,只有定义 logger 并引入的 appender,appender 才会生效-->
<!--root:用于指定项目的根日志,如果没有单独指定 Logger,则会使用 root 作为默认的日志输出-->
<loggers>
<root level="info">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
文件路径:com.dzj.dao.Userdao.java
package com.dzj.dao;
public interface Userdao {
void add();
void update();
void delete();
void query();
}
文件路径:com.dzj.dao.UserDaoImpl.java
package com.dzj.dao;
import org.springframework.stereotype.Service;
@Service
public class UserDaoImpl implements Userdao {
@Override
public void add() {
System.out.println("执行了添加方法...");
}
@Override
public void update() {
System.out.println("执行了修改方法...");
}
@Override
public void delete() {
System.out.println("执行了删除方法...");
}
@Override
public void query() {
System.out.println("执行了查找方法...");
}
}
文件路径:com.dzj.service.UserService.java
package com.dzj.service;
import com.dzj.dao.UserDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserDaoImpl userDao;
public void add(){
userDao.add();
}
public void update(){
userDao.update();
}
public void delete(){
userDao.delete();
}
public void query(){
userDao.query();
}
}
文件路径:com.dzj.controller.UserController.java
package com.dzj.controller;
import com.dzj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/test")
public String userTest(){
userService.add();
return "chengGongle...";
}
}
package com.dzj.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.Http.httpservletRequest;
import java.util.Arrays;
@Component
@Aspect
public class AdviceMethod {
private static final Logger log = LoggerFactory.getLogger(AdviceMethod.class);
@Pointcut("execution(public * com.dzj.service.UserService.*(..))")
public void webLog(){}
@Before("webLog()")
public void before(JoinPoint joinPoint){
//这个RequestContextHolder是springMVC提供来获得请求的东西
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
// 记录下请求内容
log.info("################URL : " + request.getRequestURL().toString());//获取请求的地址
log.info("################HTTP_METHOD : " + request.getMethod());//获取请求的方法
log.info("################IP : " + request.getRemoteAddr());
log.info("################THE ARGS OF THE CONTROLLER : " + Arrays.toString(joinPoint.getArgs()));
log.info("################CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
log.info("################TARGET: " + joinPoint.getTarget());
log.info("################THIS: " + joinPoint.getThis());
System.out.println("方法执行之前...");
}
@After("webLog()")
public void after(){
System.out.println("方法执行之后...");
}
}
启动主启动器类,在浏览器中输入http://localhost:8080/test,查看控制台输出信息
到此这篇关于SpringBoot中通过AOP整合日志文件的实现的文章就介绍到这了,更多相关SpringBoot AOP日志文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot中通过AOP整合日志文件的实现
本文链接: https://lsjlt.com/news/161032.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0