hsweb 监控日志

"集成教程"

Posted by mrchang on September 16, 2017      @ Views:

声明记录访问日志

  1. 如果你是maven工程
    • 引入私服配置

        <repositories>
            <repository>
                <id>hsweb-nexus</id>
                <name>Nexus Release Repository</name>
                <url>http://nexus.hsweb.me/content/groups/public/</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
      
    • 直接引入依赖

        <dependency>
            <groupId>org.hswebframework.web</groupId>
            <artifactId>hsweb-access-logging-aop</artifactId>
            <version>3.0-SNAPSHOT</version>
        </dependency>
      
  2. 如果你是非maven工程,请自行去以下地址下载jar包

监听访问日志

  1. 开启访问日志
    • 在启动类中注解@EnableAccessLogger

        @SpringBootApplication
        @EnableAccessLogger
        public class AppApplication {
            public static void main(String[] args) {
                SpringApplication.run(AppApplication.class, args);
            }	
        }
      
  2. 访问日志 API

    • controller类或者方法上,注解 @AccessLogger(“功能描述”)

        @AccessLogger("hello")
        @RequestMapping(value = "/",method = RequestMethod.GET)
        public String  hello() {
            return "Hello World ! ";
        }        
      
  3. 日志监听

    • 创建类,实现: AccessLoggerListener接口并注入到spring容器, 当有日志产生时,会调用接口方法onLogger,并传入日志信息

        @Component
        public class MyLoggingListener implements AccessLoggerListener {
            @Override
            public void onLogger(AccessLoggerInfo loggerInfo) {
                System.out.println(loggerInfo.toString());
            }
        }
      

效果

{
    "action":"hello",
    "describe":"",
    "httpHeaders":{
        "accept":"*/*",
        "cache-control":"no-cache",
        "host":"localhost:8080",
        "connection":"Keep-Alive",
        "user-agent":"Apache-HttpClient/4.5.2 (Java/1.8.0_112-release)",
        "accept-encoding":"gzip,deflate"
    },
    "httpMethod":"GET",
    "ip":"127.0.0.1",
    "method":{
        "accessible":false,
        "annotatedExceptionTypes":[

        ],
        "annotatedParameterTypes":[

        ],
        "annotatedReceiverType":{
            "annotations":[

            ],
            "declaredAnnotations":[

            ],
            "type":"com.check.controller.HelloController"
        },
        "annotatedReturnType":{
            "annotations":[

            ],
            "declaredAnnotations":[

            ],
            "type":"java.lang.String"
        },
        "annotations":[
            {
                "value":"hello",
                "ignore":false,
                "describe":[
                    ""
                ]
            },
            {
                "path":[

                ],
                "headers":[

                ],
                "method":[
                    "GET"
                ],
                "name":"",
                "produces":[

                ],
                "params":[

                ],
                "value":[
                    "/"
                ],
                "consumes":[

                ]
            }
        ],
        "bridge":false,
        "declaringClass":"com.check.controller.HelloController",
        "default":false,
        "exceptionTypes":[

        ],
        "genericExceptionTypes":[

        ],
        "genericParameterTypes":[

        ],
        "genericReturnType":"java.lang.String",
        "modifiers":1,
        "name":"hello",
        "parameterAnnotations":[

        ],
        "parameterCount":0,
        "parameterTypes":[

        ],
        "returnType":"java.lang.String",
        "synthetic":false,
        "typeParameters":[

        ],
        "varArgs":false
    },
    "parameters":{

    },
    "requestTime":1505451897197,
    "response":"Hello World ! ",
    "responseTime":1505451897214,
    "target":"com.check.controller.HelloController",
    "url":"http://localhost:8080/"
}