Skip to content

Commit

Permalink
perf($ReactiveStarter): set the order of access log filter as -500
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Miller (锺俊) committed Dec 24, 2020
1 parent 1c27903 commit 1cb0600
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
val request = exchange.getRequest();
log.error("Exception occurred when [{}] requested access. Exception message: {}. Request URL: [{}] {}",
RequestUtil.getRequestIpAndPort(request), ex.getMessage(), request.getMethod(), request.getURI(), ex);
RequestUtil.getRequesterIpAndPort(request), ex.getMessage(), request.getMethod(), request.getURI(), ex);
ServerHttpResponse response = exchange.getResponse();
if (response.isCommitted()) {
return Mono.error(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jmsoftware.maf.reactivespringbootstarter.util.RequestUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
Expand All @@ -13,17 +14,16 @@
import reactor.core.publisher.Mono;

/**
* <h1>RequestFilter</h1>
* <h1>AccessLogFilter</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com
* @date 2/15/20 7:42 PM
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 12/24/2020 10:56 AM
**/
@Slf4j
@Component
@RequiredArgsConstructor
public class AccessLogFilter implements WebFilter {
public class AccessLogFilter implements WebFilter, Ordered {
private final MafConfiguration mafConfiguration;
private final AntPathMatcher antPathMatcher = new AntPathMatcher();

Expand All @@ -38,13 +38,18 @@ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
}
// Only record non-ignored request log
log.info("{} (pre). Requester: {}, request URL: [{}] {}",
this.getClass().getSimpleName(), RequestUtil.getRequestIpAndPort(request), request.getMethod(),
this.getClass().getSimpleName(), RequestUtil.getRequesterIpAndPort(request), request.getMethod(),
request.getURI());
return chain.filter(exchange).then(
Mono.fromRunnable(() -> log.info("{} (post). Requester: {}, request URL: [{}] {}",
this.getClass().getSimpleName(),
RequestUtil.getRequestIpAndPort(request), request.getMethod(),
RequestUtil.getRequesterIpAndPort(request), request.getMethod(),
request.getURI()))
);
}

@Override
public int getOrder() {
return -500;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public class RequestUtil {
* @author Johnny Miller (锺俊), e-mail: johnnysviva@outlook.com
* @date 2/15/20 9:52 PM
*/
public static String getRequestIpAndPort(ServerHttpRequest request) {
public static String getRequesterIpAndPort(ServerHttpRequest request) {
var requestRemoteAddress = request.getRemoteAddress();
if (ObjectUtil.isNotNull(requestRemoteAddress)) {
return requestRemoteAddress.toString().substring(1);
}
return "Unknown Request";
return "Unknown Requester";
}
}

0 comments on commit 1cb0600

Please sign in to comment.