Skip to content

Instantly share code, notes, and snippets.

@AdrianoJS
Created April 12, 2023 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdrianoJS/c3ecc38e8dac37919a29727e27276889 to your computer and use it in GitHub Desktop.
Save AdrianoJS/c3ecc38e8dac37919a29727e27276889 to your computer and use it in GitHub Desktop.
spring-cloud-gateway-example-stage-3-GlobalLoggingFilter
package no.embriq.api.gateway.filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class GlobalLoggingFilter implements GlobalFilter, Ordered {
private static final Logger LOG = LoggerFactory.getLogger(GlobalLoggingFilter.class);
@Override
public Mono<Void> filter(final ServerWebExchange exchange, final GatewayFilterChain chain) {
var path = exchange.getRequest().getURI().getPath();
var queryParameters = exchange.getRequest().getURI().getQuery();
LOG.info("Received request at '{}'", path);
if(queryParameters != null && !queryParameters.isBlank()) {
LOG.debug("Query parameters were: {}", queryParameters);
}
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> LOG.info("Done processing request for endpoint '{}'", path)));
}
@Override
public int getOrder() {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment