+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.log.common.annotation; + +import java.lang.annotation.*; + +/** + * 日志注解 + *
用于接口方法或类上,辅助 Spring Doc 使用效果最佳
+ * + * @author Charles7c + * @since 1.1.0 + */ +@Documented +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Log { + + /** + * 日志描述(仅用于接口方法上) + *+ * 优先级:@Log("描述") > @Operation(summary="描述") + *
+ */ + String value() default ""; + + /** + * 所属模块(用于接口方法或类上) + *+ * 优先级: 接口方法上的 @Log(module = "模块") > 接口类上的 @Log(module = "模块") > @Tag(name = "模块") 内容 + *
+ */ + String module() default ""; + + /** + * 是否忽略日志记录(用于接口方法或类上) + */ + boolean ignore() default false; +} diff --git a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/dao/LogDao.java b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/dao/LogDao.java new file mode 100644 index 00000000..1e047596 --- /dev/null +++ b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/dao/LogDao.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.common.dao;
+
+import top.charles7c.continew.starter.log.common.model.LogRecord;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * 日志持久层接口
+ *
+ * @author Charles7c
+ * @since 1.1.0
+ */
+public interface LogDao {
+
+ /**
+ * 查询日志列表
+ *
+ * @return 日志列表
+ */
+ default List
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.common.dao.impl;
+
+import top.charles7c.continew.starter.log.common.dao.LogDao;
+import top.charles7c.continew.starter.log.common.model.LogRecord;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * 日志持久层接口默认实现类(基于内存)
+ *
+ * @author Dave Syer(Spring Boot Actuator)
+ * @author Olivier Bourgain(Spring Boot Actuator)
+ * @author Charles7c
+ * @since 1.1.0
+ */
+public class LogDaoDefaultImpl implements LogDao {
+
+ /**
+ * 容量
+ */
+ private int capacity = 100;
+
+ /**
+ * 是否降序
+ */
+ private boolean reverse = true;
+
+ /**
+ * 日志列表
+ */
+ private final List
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.common.enums;
+
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+/**
+ * 日志包含信息
+ *
+ * @author Wallace Wadge(Spring Boot Actuator)
+ * @author Emily Tsanova(Spring Boot Actuator)
+ * @author Joseph Beeton(Spring Boot Actuator)
+ * @author Charles7c
+ * @since 1.1.0
+ */
+public enum Include {
+
+ /**
+ * 描述
+ */
+ DESCRIPTION,
+
+ /**
+ * 模块
+ */
+ MODULE,
+
+ /**
+ * 请求头
+ */
+ REQUEST_HEADERS,
+
+ /**
+ * 请求体
+ */
+ REQUEST_BODY,
+
+ /**
+ * 请求参数
+ */
+ REQUEST_PARAM,
+
+ /**
+ * IP 归属地
+ */
+ IP_ADDRESS,
+
+ /**
+ * 浏览器
+ */
+ BROWSER,
+
+ /**
+ * 操作系统
+ */
+ OS,
+
+ /**
+ * 响应头
+ */
+ RESPONSE_HEADERS,
+
+ /**
+ * 响应体
+ */
+ RESPONSE_BODY,
+
+ /**
+ * 响应参数
+ */
+ RESPONSE_PARAM,
+
+ /**
+ * 耗时
+ */
+ TIME_TAKEN;
+
+ private static final Set
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.common.model;
+
+
+import lombok.Data;
+import top.charles7c.continew.starter.log.common.enums.Include;
+
+import java.time.Clock;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Set;
+
+/**
+ * 日志信息
+ *
+ * @author Dave Syer(Spring Boot Actuator)
+ * @author Andy Wilkinson(Spring Boot Actuator)
+ * @author Phillip Webb(Spring Boot Actuator)
+ * @author Charles7c
+ * @since 1.1.0
+ */
+@Data
+public class LogRecord {
+
+ /**
+ * 描述
+ */
+ private String description;
+
+ /**
+ * 模块
+ */
+ private String module;
+
+ /**
+ * 请求信息
+ */
+ private LogRequest request;
+
+ /**
+ * 响应信息
+ */
+ private LogResponse response;
+
+ /**
+ * 耗时
+ */
+ private Duration timeTaken;
+
+ /**
+ * 时间戳
+ */
+ private final Instant timestamp;
+
+ public LogRecord(Instant timestamp, LogRequest request, LogResponse response, Duration timeTaken) {
+ this.timestamp = timestamp;
+ this.request = request;
+ this.response = response;
+ this.timeTaken = timeTaken;
+ }
+
+ /**
+ * 开始记录日志
+ *
+ * @param request 请求信息
+ * @return 日志记录器
+ */
+ public static Started start(RecordableHttpRequest request) {
+ return start(Clock.systemUTC(), request);
+ }
+
+ /**
+ * 开始记录日志
+ *
+ * @param timestamp 开始时间
+ * @param request 请求信息
+ * @return 日志记录器
+ */
+ public static Started start(Clock timestamp, RecordableHttpRequest request) {
+ return new Started(timestamp, request);
+ }
+
+ /**
+ * 日志记录器
+ */
+ public static final class Started {
+
+ private final Instant timestamp;
+
+ private final RecordableHttpRequest request;
+
+ private Started(Clock clock, RecordableHttpRequest request) {
+ this.timestamp = Instant.now(clock);
+ this.request = request;
+ }
+
+ /**
+ * 结束日志记录
+ *
+ * @param response 响应信息
+ * @param includes 包含信息
+ * @return 日志记录
+ */
+ public LogRecord finish(RecordableHttpResponse response, Set
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.common.model;
+
+import lombok.Data;
+import org.springframework.http.HttpHeaders;
+import top.charles7c.continew.starter.core.util.ExceptionUtils;
+import top.charles7c.continew.starter.core.util.IpUtils;
+import top.charles7c.continew.starter.core.util.ServletUtils;
+import top.charles7c.continew.starter.log.common.enums.Include;
+
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * 请求信息
+ *
+ * @author Charles7c
+ * @since 1.1.0
+ */
+@Data
+public class LogRequest {
+
+ /**
+ * 请求方式
+ */
+ private String method;
+
+ /**
+ * 请求 URI
+ */
+ private URI uri;
+
+ /**
+ * IP
+ */
+ private String ip;
+
+ /**
+ * 请求头
+ */
+ private Map
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.common.model;
+
+import lombok.Data;
+import top.charles7c.continew.starter.log.common.enums.Include;
+
+import java.util.*;
+
+/**
+ * 响应信息
+ *
+ * @author Charles7c
+ * @since 1.1.0
+ */
+@Data
+public class LogResponse {
+
+ /**
+ * 状态码
+ */
+ private Integer status;
+
+ /**
+ * 响应头
+ */
+ private Map
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.common.model;
+
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 可记录的 HTTP 请求信息
+ *
+ * @author Andy Wilkinson(Spring Boot Actuator)
+ * @author Phillip Webb(Spring Boot Actuator)
+ * @author Charles7c
+ * @see RecordableHttpResponse
+ * @since 1.1.0
+ */
+public interface RecordableHttpRequest {
+
+ /**
+ * 获取请求方式
+ *
+ * @return 请求方式
+ */
+ String getMethod();
+
+ /**
+ * 获取 URI
+ *
+ * @return URI
+ */
+ URI getUri();
+
+ /**
+ * 获取 IP
+ *
+ * @return IP
+ */
+ String getIp();
+
+ /**
+ * 获取请求头
+ *
+ * @return 请求头
+ */
+ Map
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.common.model;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 可记录的 HTTP 响应信息
+ *
+ * @author Andy Wilkinson(Spring Boot Actuator)
+ * @author Charles7c
+ * @see RecordableHttpRequest
+ * @since 1.1.0
+ */
+public interface RecordableHttpResponse {
+
+ /**
+ * 获取状态码
+ *
+ * @return 状态码
+ */
+ int getStatus();
+
+ /**
+ * 获取响应头
+ *
+ * @return 响应头
+ */
+ Map
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.httptracepro.autoconfigure;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+
+import java.lang.annotation.*;
+
+/**
+ * 是否启用日志记录注解
+ *
+ * @author Charles7c
+ * @since 1.1.0
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ ElementType.TYPE, ElementType.METHOD })
+@Documented
+@ConditionalOnProperty(prefix = "continew-starter.log", name = "enabled", havingValue = "true")
+public @interface ConditionalOnEnabledLog {}
\ No newline at end of file
diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/autoconfigure/LogAutoConfiguration.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/autoconfigure/LogAutoConfiguration.java
new file mode 100644
index 00000000..3f534f1b
--- /dev/null
+++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/autoconfigure/LogAutoConfiguration.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.httptracepro.autoconfigure;
+
+import jakarta.annotation.PostConstruct;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import top.charles7c.continew.starter.log.common.dao.LogDao;
+import top.charles7c.continew.starter.log.common.dao.impl.LogDaoDefaultImpl;
+import top.charles7c.continew.starter.log.httptracepro.handler.LogFilter;
+import top.charles7c.continew.starter.log.httptracepro.handler.LogInterceptor;
+
+/**
+ * 日志自动配置
+ *
+ * @author Charles7c
+ * @since 1.1.0
+ */
+@Slf4j
+@Configuration
+@ConditionalOnEnabledLog
+@RequiredArgsConstructor
+@EnableConfigurationProperties(LogProperties.class)
+@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
+public class LogAutoConfiguration implements WebMvcConfigurer {
+
+ private final LogProperties properties;
+
+ @Override
+ public void addInterceptors(InterceptorRegistry registry) {
+ registry.addInterceptor(new LogInterceptor(logDao(), properties));
+ }
+
+ /**
+ * 日志过滤器
+ */
+ @Bean
+ @ConditionalOnMissingBean
+ public LogFilter logFilter() {
+ return new LogFilter();
+ }
+
+ /**
+ * 日志持久层接口
+ */
+ @Bean
+ @ConditionalOnMissingBean
+ public LogDao logDao() {
+ return new LogDaoDefaultImpl();
+ }
+
+ @PostConstruct
+ public void postConstruct() {
+ log.info("[ContiNew Starter] - Auto Configuration 'Log-HttpTracePro' completed initialization.");
+ }
+}
diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/autoconfigure/LogProperties.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/autoconfigure/LogProperties.java
new file mode 100644
index 00000000..45055fa0
--- /dev/null
+++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/autoconfigure/LogProperties.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.httptracepro.autoconfigure;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import top.charles7c.continew.starter.log.common.enums.Include;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * 日志配置属性
+ *
+ * @author Charles7c
+ * @since 1.1.0
+ */
+@Data
+@ConfigurationProperties(prefix = "continew-starter.log")
+public class LogProperties {
+
+ /**
+ * 是否启用日志
+ */
+ private boolean enabled = false;
+
+ /**
+ * 包含信息
+ */
+ private Set
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.httptracepro.handler;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import lombok.RequiredArgsConstructor;
+import org.springframework.core.Ordered;
+import org.springframework.lang.NonNull;
+import org.springframework.web.filter.OncePerRequestFilter;
+import org.springframework.web.util.ContentCachingRequestWrapper;
+import org.springframework.web.util.ContentCachingResponseWrapper;
+import org.springframework.web.util.WebUtils;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Objects;
+
+/**
+ * 日志过滤器
+ *
+ * @author Dave Syer(Spring Boot Actuator)
+ * @author Wallace Wadge(Spring Boot Actuator)
+ * @author Andy Wilkinson(Spring Boot Actuator)
+ * @author Venil Noronha(Spring Boot Actuator)
+ * @author Madhura Bhave(Spring Boot Actuator)
+ * @author Charles7c
+ * @since 1.1.0
+ */
+@RequiredArgsConstructor
+public class LogFilter extends OncePerRequestFilter implements Ordered {
+
+ @Override
+ public int getOrder() {
+ return Ordered.LOWEST_PRECEDENCE - 10;
+ }
+
+ @Override
+ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
+ @NonNull FilterChain filterChain) throws ServletException, IOException {
+ if (!isRequestValid(request)) {
+ filterChain.doFilter(request, response);
+ return;
+ }
+ // 包装输入、输出流,可重复读取
+ if (!(request instanceof ContentCachingRequestWrapper)) {
+ request = new ContentCachingRequestWrapper(request);
+ }
+ if (!(response instanceof ContentCachingResponseWrapper)) {
+ response = new ContentCachingResponseWrapper(response);
+ }
+ filterChain.doFilter(request, response);
+ // 更新响应(不操作这一步,会导致接口响应空白)
+ updateResponse(response);
+ }
+
+ private boolean isRequestValid(HttpServletRequest request) {
+ try {
+ new URI(request.getRequestURL().toString());
+ return true;
+ } catch (URISyntaxException e) {
+ return false;
+ }
+ }
+
+ private void updateResponse(HttpServletResponse response) throws IOException {
+ ContentCachingResponseWrapper responseWrapper =
+ WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
+ Objects.requireNonNull(responseWrapper).copyBodyToResponse();
+ }
+}
diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java
new file mode 100644
index 00000000..a6f6e421
--- /dev/null
+++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.httptracepro.handler;
+
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.ttl.TransmittableThreadLocal;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.lang.NonNull;
+import org.springframework.web.method.HandlerMethod;
+import org.springframework.web.servlet.HandlerInterceptor;
+import top.charles7c.continew.starter.log.common.annotation.Log;
+import top.charles7c.continew.starter.log.common.dao.LogDao;
+import top.charles7c.continew.starter.log.common.enums.Include;
+import top.charles7c.continew.starter.log.common.model.LogRecord;
+import top.charles7c.continew.starter.log.httptracepro.autoconfigure.LogProperties;
+
+import java.time.Clock;
+import java.util.Set;
+
+/**
+ * 日志拦截器
+ *
+ * @author Charles7c
+ * @since 1.1.0
+ */
+@Slf4j
+@RequiredArgsConstructor
+public class LogInterceptor implements HandlerInterceptor {
+
+ private final LogDao dao;
+ private final LogProperties properties;
+ private final TransmittableThreadLocal
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.httptracepro.handler;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.servlet.JakartaServletUtil;
+import cn.hutool.json.JSONUtil;
+import jakarta.servlet.http.HttpServletRequest;
+import org.springframework.web.util.ContentCachingRequestWrapper;
+import org.springframework.web.util.UriUtils;
+import org.springframework.web.util.WebUtils;
+import top.charles7c.continew.starter.core.constant.StringConstants;
+import top.charles7c.continew.starter.log.common.model.RecordableHttpRequest;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
+import java.util.*;
+
+/**
+ * 可记录的 HTTP 请求信息适配器
+ *
+ * @author Andy Wilkinson(Spring Boot Actuator)
+ * @author Charles7c
+ */
+public final class RecordableServletHttpRequest implements RecordableHttpRequest {
+
+ private final HttpServletRequest request;
+
+ public RecordableServletHttpRequest(HttpServletRequest request) {
+ this.request = request;
+ }
+
+ @Override
+ public String getMethod() {
+ return request.getMethod();
+ }
+
+ @Override
+ public URI getUri() {
+ String queryString = request.getQueryString();
+ if (StrUtil.isBlank(queryString)) {
+ return URI.create(request.getRequestURL().toString());
+ }
+ try {
+ StringBuffer urlBuffer = this.appendQueryString(queryString);
+ return new URI(urlBuffer.toString());
+ } catch (URISyntaxException e) {
+ String encoded = UriUtils.encodeQuery(queryString, StandardCharsets.UTF_8);
+ StringBuffer urlBuffer = this.appendQueryString(encoded);
+ return URI.create(urlBuffer.toString());
+ }
+ }
+
+ @Override
+ public String getIp() {
+ return JakartaServletUtil.getClientIP(request);
+ }
+
+ @Override
+ public Map
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.starter.log.httptracepro.handler;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONUtil;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.web.util.ContentCachingResponseWrapper;
+import org.springframework.web.util.WebUtils;
+import top.charles7c.continew.starter.core.constant.StringConstants;
+import top.charles7c.continew.starter.log.common.model.RecordableHttpResponse;
+
+import java.util.*;
+
+/**
+ * 可记录的 HTTP 响应信息适配器
+ *
+ * @author Andy Wilkinson(Spring Boot Actuator)
+ * @author Charles7c
+ */
+public final class RecordableServletHttpResponse implements RecordableHttpResponse {
+
+ private final HttpServletResponse response;
+
+ private final int status;
+
+ public RecordableServletHttpResponse(HttpServletResponse response, int status) {
+ this.response = response;
+ this.status = status;
+ }
+
+ @Override
+ public int getStatus() {
+ return this.status;
+ }
+
+ @Override
+ public Map