Code Monkey home page Code Monkey logo

autolog-spring-boot-starter's Introduction

autolog-spring-boot-starter

描述

  自动捕获操作日志组件

引用

maven坐标

<dependency>
    <groupId>com.github.aqiu202</groupId>
    <artifactId>autolog-spring-boot-starter</artifactId>
    <version>0.0.1</version>
</dependency>

用法

启用AutoLog
@EnableAutoLog(
    enable = true,//开启AutoLog日志
    dubug = true //开启debug模式,会打印所有前端传输参数    
)
@SpringBootApplication
public class Main {
    public static void main(String[] args){
      SpringApplication.run(Main.class, args);
    }
}
使用AutoLog
@RestController
public class TestController {
    
    //简单使用
    @GetMapping("test")
    @AutoLog
    public JsonResult test() {
        return JsonResult.ok();
    }

    //使用无参数的日志
    @GetMapping("test1")
    @AutoLog(value = "测试日志")
    public JsonResult test1() {
        return JsonResult.ok();
    }

    //使用Spring SpEl表达式注入指定参数
    //参数方式注入日志方式1,通过名称 #name
    @GetMapping("test2")
    @AutoLog(value = "'测试参数,参数name:' + #map[name]")
    public JsonResult test2(@RequestBody Map<String,String> map) {
        return JsonResult.ok();
    }

    //参数方式注入日志方式2,通过索引,#a0或者#p0,索引从0开始
    @GetMapping("test3")
    @AutoLog(value="'测试参数,参数1:' + #a0")
    public JsonResult test3(String name) {
        return JsonResult.ok();
    }
    
}
收集/处理捕获到的日志
@Configuration
public class AutoLogConfig {

    //收集日志
    @Bean
    public LogCollector logCollector() {
        return (
                request,//request HTTP servlet request
                autoLog,//autoLog 注解信息
                throwable,//throwable 异常信息,如果没有异常为 null
                args //args 方法参数
        ) -> {
            DefaultLogRequestParam param = DefaultLogRequestParam.create();
            //填充日志参数,没设置的字段会默认自动填充 result不需要填充
            param.setDesc("description");
            return param;
        };
    }
    //处理日志
    @Bean
    public LogHandler logHandler() {
        return (requestParam) -> {
            //处理捕获到的日志
        };
    }
}
自定义扩展日志参数

默认的日志采集参数只有 ip,uri,method,desc,result这几个字段,如果不满足需求的话, 我们可以扩展 DefaultLogRequestParam 类来满足我们的需求,试例如下:

//自定义扩展类
public class MyLogRequestParam extends DefaultLogRequestParam {

    private String sessionId;

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }
}
//配置类
@Configuration
class AutoLogConfig {

    //收集日志
    @Bean
    public LogCollector logCollector() {
        return (
                request,//request HTTP servlet request
                autoLog,//autoLog 注解信息
                throwable,//throwable 异常信息,如果没有异常为 null
                args //args 方法参数
        ) -> {
            MyLogRequestParam param = new MyLogRequestParam();
            param.setSessionId(request.getSession().getId());
            return param;
        };
    }
    
    //处理日志
    @Bean
    public LogHandler logHandler() {
        return (requestParam) -> {
            //处理捕获到的日志
            MyLogRequestParam myLogRequestParam = (MyLogRequestParam) requestParam;
            System.out.println(myLogRequestParam.getSessionId());
        };
    }
    
}

autolog-spring-boot-starter's People

Contributors

aqiu202 avatar aqiuweb avatar

Stargazers

 avatar  avatar  avatar

Forkers

1415749952

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.