首页 > 解决方案 > Vertx 日志记录:访问请求上下文

问题描述

这看起来很常见:我们想用一些自定义消息记录路由上下文相关数据。对于每线程请求应用程序,我们使用线程本地 MDC。但是对于 Vert.x 应用程序有没有类似的解决方案?可能是一些在事件循环中工作并管理所有请求的 Vertx 记录器。

谢谢你。

标签: loggingvert.x

解决方案


assuming this is Vert.x-Web you're working with... one approach you could take is to add a Handler<RoutingContext> to the Router and configure it to run on every request. in Java, that might look something like this:

// the Handler with your custom logging
final Handler<RoutingContext> loggingHandler = routingContext -> {
    final HttpServerRequest request = routingContext.request();

    System.out.println(request.getParam("foo"));

    routingContext.next();
};

// use Router.route() to configure a handler that runs on every
// request regardless of method or path
final Router router = Router.router(Vertx.vertx());
router.route().handler(loggingHandler);

hope that helps!


推荐阅读