Skip to content

Commit d5541bf

Browse files
committed
ApiBoot Logging Plugin插件初版发布,支持openfeign链路日志
1 parent b6e05ca commit d5541bf

23 files changed

+1560
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright [2019] [恒宇少年 - 于起宇]
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
~
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<parent>
23+
<artifactId>api-boot-plugins</artifactId>
24+
<groupId>org.minbox.framework</groupId>
25+
<version>2.1.1-SNAPSHOT</version>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
<properties>
29+
<main.basedir>${basedir}/../../..</main.basedir>
30+
</properties>
31+
<description>
32+
ApiBoot Logging
33+
</description>
34+
<artifactId>api-boot-plugin-logging</artifactId>
35+
<dependencies>
36+
<!--ApiBoot Plugin-->
37+
<dependency>
38+
<groupId>org.minbox.framework</groupId>
39+
<artifactId>api-boot-plugin</artifactId>
40+
</dependency>
41+
<!--SpringBoot Web-->
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-web</artifactId>
45+
<optional>true</optional>
46+
</dependency>
47+
<!--SpringCloud Openfeign-->
48+
<dependency>
49+
<groupId>org.springframework.cloud</groupId>
50+
<artifactId>spring-cloud-starter-openfeign</artifactId>
51+
<optional>true</optional>
52+
</dependency>
53+
</dependencies>
54+
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging;
19+
20+
import lombok.Data;
21+
22+
import java.util.Map;
23+
24+
/**
25+
* ApiBoot Log Object
26+
*
27+
* @author:恒宇少年 - 于起宇
28+
* <p>
29+
* DateTime:2019-07-15 16:41
30+
* Blog:http://blog.yuqiyu.com
31+
* WebSite:http://www.jianshu.com/u/092df3f77bca
32+
* Gitee:https://gitee.com/hengboy
33+
* GitHub:https://github.com/hengboy
34+
*/
35+
@Data
36+
public class ApiBootLog {
37+
/**
38+
* trace id
39+
*/
40+
private String traceId;
41+
/**
42+
* span id
43+
*/
44+
private String spanId;
45+
/**
46+
* parent span id
47+
*/
48+
private String parentSpanId;
49+
/**
50+
* request uri
51+
*/
52+
private String requestUri;
53+
/**
54+
* request method
55+
*/
56+
private String requestMethod;
57+
/**
58+
* http status code
59+
*/
60+
private int httpStatus;
61+
/**
62+
* request ip
63+
*/
64+
private String requestIp;
65+
/**
66+
* start time
67+
*/
68+
private Long startTime;
69+
/**
70+
* end time
71+
*/
72+
private Long endTime;
73+
/**
74+
* this request time consuming
75+
*/
76+
private long timeConsuming;
77+
/**
78+
* service id
79+
*/
80+
private String serviceId;
81+
/**
82+
* request headers
83+
*/
84+
private Map<String, String> requestHeaders;
85+
/**
86+
* request param
87+
*/
88+
private String requestParam;
89+
/**
90+
* request body
91+
*/
92+
private String requestBody;
93+
/**
94+
* response headers
95+
*/
96+
private Map<String, String> responseHeaders;
97+
/**
98+
* response body
99+
*/
100+
private String responseBody;
101+
/**
102+
* exception stack
103+
*/
104+
private String exceptionStack;
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging;
19+
20+
/**
21+
* ApiBoot Log Constant
22+
*
23+
* @author:恒宇少年 - 于起宇
24+
* <p>
25+
* DateTime:2019-07-16 14:55
26+
* Blog:http://blog.yuqiyu.com
27+
* WebSite:http://www.jianshu.com/u/092df3f77bca
28+
* Gitee:https://gitee.com/hengboy
29+
* GitHub:https://github.com/hengboy
30+
*/
31+
public interface ApiBootLogConstant {
32+
/**
33+
* ApiBoot TraceId Header Name
34+
*/
35+
String HEADER_NAME_TRACE_ID = "API-BOOT-X-TRACE-ID";
36+
/**
37+
* ApiBoot Parent SpanId Header Name
38+
*/
39+
String HEADER_NAME_PARENT_SPAN_ID = "API-BOOT-X-PARENT-SPAN-ID";
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging;
19+
20+
/**
21+
* Using threadLocal to store log objects in multithreaded situations
22+
*
23+
* @author:恒宇少年 - 于起宇
24+
* <p>
25+
* DateTime:2019-07-15 16:41
26+
* Blog:http://blog.yuqiyu.com
27+
* WebSite:http://www.jianshu.com/u/092df3f77bca
28+
* Gitee:https://gitee.com/hengboy
29+
* GitHub:https://github.com/hengboy
30+
*/
31+
public class ApiBootLogThreadLocal {
32+
/**
33+
* ApiBoot Log Thread Local
34+
*/
35+
private static final ThreadLocal<ApiBootLog> API_BOOT_LOG_THREAD_LOCAL = new ThreadLocal();
36+
37+
/**
38+
* Get This Thread ApiBoot Log Object Instance
39+
*
40+
* @return This Thread ApiBoot Log
41+
*/
42+
public static ApiBootLog get() {
43+
return API_BOOT_LOG_THREAD_LOCAL.get();
44+
}
45+
46+
/**
47+
* Set This Thread ApiBoot Log Object Instance
48+
*
49+
* @param apiBootLog This Thread ApiBoot Log
50+
*/
51+
public static void set(ApiBootLog apiBootLog) {
52+
API_BOOT_LOG_THREAD_LOCAL.set(apiBootLog);
53+
}
54+
55+
/**
56+
* Remove This Thread ApiBoot Log Object Instance
57+
*/
58+
public static void remove() {
59+
API_BOOT_LOG_THREAD_LOCAL.remove();
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging.filter;
19+
20+
import javax.servlet.*;
21+
import javax.servlet.annotation.WebFilter;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
import java.io.IOException;
25+
26+
/**
27+
* ApiBoot Logging Body(Request / Response) Filter
28+
* Encapsulation principal obtains the corresponding replicated content
29+
*
30+
* @author:恒宇少年 - 于起宇
31+
* <p>
32+
* DateTime:2019-07-16 12:26
33+
* Blog:http://blog.yuqiyu.com
34+
* WebSite:http://www.jianshu.com/u/092df3f77bca
35+
* Gitee:https://gitee.com/hengboy
36+
* GitHub:https://github.com/hengboy
37+
*/
38+
@WebFilter(urlPatterns = "/*", filterName = "apiBootLoggingFilter")
39+
public class ApiBootLoggingBodyFilter implements Filter {
40+
/**
41+
* Wrapper Body
42+
* replace http request body instance
43+
* replace http response body instance
44+
*
45+
* @param request http request
46+
* @param response http response
47+
* @param filterChain filter chain
48+
* @throws IOException ioException
49+
* @throws ServletException servlet Exception
50+
*/
51+
@Override
52+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
53+
RequestWrapper requestWrapper = new RequestWrapper((HttpServletRequest) request);
54+
ResponseWrapper responseWrapper = new ResponseWrapper((HttpServletResponse) response);
55+
filterChain.doFilter(requestWrapper, responseWrapper);
56+
responseWrapper.flushBuffer();
57+
}
58+
}

0 commit comments

Comments
 (0)