<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</art所得税汇算清缴ifactid> <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> <d运动会加油稿件ependency> <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.urdao.java
package com.dzj.dao; public interface urdao { void add(); void update(); void delete(); void query();}
文件路径:com.dzj.dao.urdaoimpl.java
package com.dzj.dao; import org.springframework.stereotype.rvice; @rvicepublic class urdaoimpl implements urdao { @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.rvice.urrvice.java
package com.dzj.rvice; import com.dzj.dao.urdaoimpl;import org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.rvice; @rvicepublic class urrvice { @autowired urdaoimpl urdao; public void add(){ urdao.add(); } public void update(){ urdao.update(); } public void delete(){ urdao.delete(); } public void query(){ urdao.query(); }}
文件路径:com.dzj.controller.urcontroller.java
package com.dzj.controller; import com.dzj.rvice.urrvice;import org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotati礼仪课程培训on.responbody; @controller@responbodypublic class urcontroller { @autowired urrvice urrvice; @requestmapping("/test") public string urtest(){ urrvice.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.rvletrequestattributes; import javax.rvlet.http.httprvletrequest;import java.util.arrays; @component@aspectpublic class advicemethod { private static final logger log = loggerfactory.getlogger(advicemethod.class); @pointcut("execution(public * com.dzj.rvice.urrvice.*(..))") public void weblog(){} @before("weblog()") public void before(joinpoint joinpoint){ //这个requestcontextholder是springmvc提供来获得请求的东西 requestattributes requestattributes = requestcontextholder.getrequestattributes(); httprvletrequest request = ((rvletrequestattributes)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())); /** * getsignature().getdeclaringtypename() 是获取包+类名的 * joinpoint.getsignature.getname() 获取了方法名 * joinpoint.gettarget() 返回的是需要加强的目标类的对象 * joinpoint.getthis()) 返回的是经过加强后的代理类的对象 */ 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日志文件内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 07:52:39,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/0074dc90a1e8430f73e47cd251b42eed.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:SpringBoot中通过AOP整合日志文件的实现.doc
本文 PDF 下载地址:SpringBoot中通过AOP整合日志文件的实现.pdf
留言与评论(共有 0 条评论) |