katrin
katrin

Reputation: 1156

Vertx logging: access to request context

It looks like common issue: we want to log routing context related data with some custom messages. For request-per-thread apps we used thread-local MDC. But is there any similar solution for Vert.x apps? May be some Vertx logger that works inside event loop and manage all requests.

Thank you.

Upvotes: 2

Views: 2415

Answers (1)

homerman
homerman

Reputation: 3569

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!

Upvotes: 4

Related Questions