欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot中通过AOP整合日志文件的实现

程序员文章站 2022-06-16 13:06:08
目录1.导入相关的依赖2.log4j2 日志文件3.dao层的接口以及实现类4.service层业务实现类5.controller层接口控制类6.编写业务类增强类,加入一个日志文件记录7.运行测试,查...

1.导入相关的依赖

<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>

2.log4j2 日志文件

<?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>

3.dao层的接口以及实现类

文件路径: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("执行了查找方法...");
    }
}

4.service层业务实现类

文件路径: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();
    }
}

5.controller层接口控制类

文件路径: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...";
    }
}

6.编写业务类增强类,加入一个日志文件记录

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()));
 
        /**
         * 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("方法执行之后...");
    }
}

7.运行测试,查看结果

启动主启动器类,在浏览器中输入http://localhost:8080/test,查看控制台输出信息

SpringBoot中通过AOP整合日志文件的实现

到此这篇关于springboot中通过aop整合日志文件的实现的文章就介绍到这了,更多相关springboot aop日志文件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!