打印日志-2
This commit is contained in:
parent
4a436f490d
commit
2becab420d
@ -1,99 +0,0 @@
|
|||||||
package com.qiuguo.iot.base.filter;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.UUID;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.apache.logging.log4j.ThreadContext;
|
|
||||||
import org.reactivestreams.Publisher;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.core.io.buffer.DataBuffer;
|
|
||||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
|
||||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
|
||||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
|
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
|
||||||
import org.springframework.web.server.WebFilter;
|
|
||||||
import org.springframework.web.server.WebFilterChain;
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@Slf4j
|
|
||||||
public class LogWebFilter implements WebFilter {
|
|
||||||
@Override
|
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
ThreadContext.put("logid", UUID.randomUUID().toString());
|
|
||||||
ServerHttpRequest request = exchange.getRequest();
|
|
||||||
String ip = request.getRemoteAddress().getHostName();
|
|
||||||
String m = request.getMethod().toString();
|
|
||||||
|
|
||||||
log.info("api start time:{} ip:{} method:{} url:{} param:{} headers:{}",
|
|
||||||
startTime,
|
|
||||||
ip,
|
|
||||||
m,
|
|
||||||
request.getPath(),
|
|
||||||
request.getQueryParams(),
|
|
||||||
request.getHeaders());
|
|
||||||
|
|
||||||
ServerWebExchange.Builder ex = exchange.mutate();
|
|
||||||
ex.response(getResponse(exchange));
|
|
||||||
|
|
||||||
if(!request.getMethod().equals(HttpMethod.GET) && !request.getMethod().equals(HttpMethod.DELETE)){
|
|
||||||
ex.request(getRequest(exchange));
|
|
||||||
}
|
|
||||||
return chain.filter(ex.build()).doFinally(signalType -> {
|
|
||||||
long endTime = System.currentTimeMillis();
|
|
||||||
log.info("api end time:{}, total time:{}", endTime, endTime - startTime);
|
|
||||||
ThreadContext.clearAll();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private ServerHttpRequest getRequest(ServerWebExchange exchange){
|
|
||||||
ServerHttpRequest request = exchange.getRequest();
|
|
||||||
return new ServerHttpRequestDecorator(request){
|
|
||||||
@Override
|
|
||||||
public Flux<DataBuffer> getBody() {
|
|
||||||
Flux<DataBuffer> body = this.getDelegate().getBody();
|
|
||||||
return body.map(dataBuffer -> {
|
|
||||||
log.info("request:{}", dataBuffer.toString(StandardCharsets.UTF_8));
|
|
||||||
return dataBuffer;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private ServerHttpResponse getResponse(ServerWebExchange exchange){
|
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
|
||||||
return new ServerHttpResponseDecorator(response){
|
|
||||||
@Override
|
|
||||||
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body){
|
|
||||||
if (body instanceof Flux) {
|
|
||||||
Flux<? extends DataBuffer> fluxBody = Flux.from(body);
|
|
||||||
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
|
|
||||||
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
|
|
||||||
DataBuffer joinBuffer = dataBufferFactory.join(dataBuffers);
|
|
||||||
byte[] returnContent = new byte[joinBuffer.readableByteCount()];
|
|
||||||
joinBuffer.read(returnContent);
|
|
||||||
DataBufferUtils.release(joinBuffer);
|
|
||||||
String returnStr = new String(returnContent, StandardCharsets.UTF_8);
|
|
||||||
log.info("response:{}", returnStr);
|
|
||||||
return response.bufferFactory().wrap(returnContent);
|
|
||||||
}));
|
|
||||||
}else if(body instanceof Mono){
|
|
||||||
Mono<DataBuffer> monoBody = Mono.from(body);
|
|
||||||
return super.writeWith(monoBody.map(dataBuffer -> {
|
|
||||||
log.info("response:{}", dataBuffer.toString(StandardCharsets.UTF_8));
|
|
||||||
return response.bufferFactory().wrap(dataBuffer.toString(StandardCharsets.UTF_8).getBytes());
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
return super.writeWith(body);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user