Log all incoming requests and response details in a table

How to log incoming request details and corresponding response in table in cloud gateway.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>String logId = service.logRequest(name.toString(),request.getURI().getRawPath(), String.valueOf(request.getMethod()),remoteAddr,"",new Date());
</code>
<code>String logId = service.logRequest(name.toString(),request.getURI().getRawPath(), String.valueOf(request.getMethod()),remoteAddr,"",new Date()); </code>
String logId = service.logRequest(name.toString(),request.getURI().getRawPath(), String.valueOf(request.getMethod()),remoteAddr,"",new Date());

Able to fetch the request details

Storing the details in table. But issues comes when response details have to be stored in corresponding request row.

Previously in ZUUL used to store the LogId like below.

Request

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>RequestContext ctx = RequestContext.getCurrentContext();
ctx.put("REQ-LOG-ID",LogId);
</code>
<code>RequestContext ctx = RequestContext.getCurrentContext(); ctx.put("REQ-LOG-ID",LogId); </code>
RequestContext ctx = RequestContext.getCurrentContext();
ctx.put("REQ-LOG-ID",LogId); 

Response

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>String requestLogId = (String) ctx.get("REQ-LOG-ID");
//store the response details to corresponding Id
</code>
<code>String requestLogId = (String) ctx.get("REQ-LOG-ID"); //store the response details to corresponding Id </code>
String requestLogId = (String) ctx.get("REQ-LOG-ID");
//store the response details to corresponding Id

Whats the replacement in Cloud gateway? We’re using Spring-Boot 3.2.0.

To log incoming request details and corresponding responses in Spring Cloud Gateway, you can use the GatewayFilter to store the log ID and then retrieve it later for the response. Here’s how you can do it:

  • Create a Custom Gateway Filter:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Component
public class LoggingGatewayFilter extends AbstractGatewayFilterFactory<LoggingGatewayFilter.Config> {
public LoggingGatewayFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
String logId = service.logRequest(/* parameters here */);
exchange.getAttributes().put("REQ-LOG-ID", logId);
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
String requestLogId = exchange.getAttribute("REQ-LOG-ID");
// Store response details using requestLogId
storeResponseDetails(requestLogId, /* response details here */);
}));
};
}
public static class Config {
// Configuration properties can be added here if needed
}
}
</code>
<code>@Component public class LoggingGatewayFilter extends AbstractGatewayFilterFactory<LoggingGatewayFilter.Config> { public LoggingGatewayFilter() { super(Config.class); } @Override public GatewayFilter apply(Config config) { return (exchange, chain) -> { String logId = service.logRequest(/* parameters here */); exchange.getAttributes().put("REQ-LOG-ID", logId); return chain.filter(exchange).then(Mono.fromRunnable(() -> { String requestLogId = exchange.getAttribute("REQ-LOG-ID"); // Store response details using requestLogId storeResponseDetails(requestLogId, /* response details here */); })); }; } public static class Config { // Configuration properties can be added here if needed } } </code>
@Component
public class LoggingGatewayFilter extends AbstractGatewayFilterFactory<LoggingGatewayFilter.Config> {

    public LoggingGatewayFilter() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            String logId = service.logRequest(/* parameters here */);
            exchange.getAttributes().put("REQ-LOG-ID", logId);

            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                String requestLogId = exchange.getAttribute("REQ-LOG-ID");
                // Store response details using requestLogId
                storeResponseDetails(requestLogId, /* response details here */);
            }));
        };
    }

    public static class Config {
        // Configuration properties can be added here if needed
    }
}

  • Register the Filter:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, LoggingGatewayFilter loggingGatewayFilter) {
return builder.routes()
.route("your_route_id", r -> r.path("/your-path/**")
.filters(f -> f.filter(loggingGatewayFilter))
.uri("http://your-backend-service"))
.build();
}
}
</code>
<code>@Configuration public class GatewayConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder, LoggingGatewayFilter loggingGatewayFilter) { return builder.routes() .route("your_route_id", r -> r.path("/your-path/**") .filters(f -> f.filter(loggingGatewayFilter)) .uri("http://your-backend-service")) .build(); } } </code>
@Configuration
public class GatewayConfig {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder, LoggingGatewayFilter loggingGatewayFilter) {
        return builder.routes()
                .route("your_route_id", r -> r.path("/your-path/**")
                        .filters(f -> f.filter(loggingGatewayFilter))
                        .uri("http://your-backend-service"))
                .build();
    }
}
  • Store Response Details:
    In the storeResponseDetails method, implement logic to save the response details corresponding to the requestLogId.

I hope it’ll be helpful to solve problem you’re facing.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật