我现在使用的是quarkus3.0.3响应式框架,针对你上述回答,请给出具体示例
时间: 2024-03-11 18:50:30 浏览: 129
好的,我可以为你提供一个在Quarkus 3.0.3中记录HTTP请求日志的示例代码。
1. 首先,在`application.properties`文件中添加以下配置来开启HTTP请求日志:
```
quarkus.http.log.enabled=true
```
2. 创建一个拦截器类`HttpLoggingInterceptor`来记录请求日志:
```java
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import org.jboss.logging.Logger;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import java.time.Duration;
import java.time.Instant;
@Provider
@ApplicationScoped
public class HttpLoggingInterceptor implements ContainerRequestFilter, ContainerResponseFilter {
private static final Logger LOG = Logger.getLogger(HttpLoggingInterceptor.class);
@Context
private RoutingContext routingContext;
private Instant requestStartTime;
@Override
public void filter(ContainerRequestContext requestContext) {
this.requestStartTime = Instant.now();
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
HttpServerRequest request = routingContext.request();
HttpServerResponse response = routingContext.response();
String method = request.method().name();
String uri = request.uri();
int status = response.getStatusCode();
String remoteAddress = request.remoteAddress().toString();
Instant requestEndTime = Instant.now();
Duration duration = Duration.between(requestStartTime, requestEndTime);
long durationMillis = duration.toMillis();
LOG.infof("[%s] %s - %s [%d] (%d ms)", method, uri, remoteAddress, status, durationMillis);
}
}
```
在上述代码中,我们在请求开始时记录了请求的开始时间,然后在请求结束时计算处理时间,并将请求日志输出到日志文件中。
3. 然后在资源类中注册这个拦截器:
```java
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/example")
public class ExampleResource {
@Inject
HttpLoggingInterceptor httpLoggingInterceptor;
@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Operation(summary = "Test endpoint")
@APIResponse(responseCode = "200", description = "Success")
public String test(@RequestBody String requestBody) {
return "{ \"message\": \"Hello, World!\" }";
}
}
```
在上述代码中,我们使用了`@Inject`注入了我们之前创建的`HttpLoggingInterceptor`拦截器,然后在资源方法上使用`@POST`注解标记了一个POST请求的处理方法,同时使用`@Operation`和`@APIResponse`注解来为这个方法生成OpenAPI规范。
这样,在每次发起POST请求时,我们都会在日志文件中看到类似如下的输出:
```
[POST] /example/test - /127.0.0.1:12345 [200] (10 ms)
```
这就是一个基于Quarkus 3.0.3的HTTP请求日志记录的示例。
阅读全文