Unique id for logging in Vertx

739 views
Skip to first unread message

akshay kumar

unread,
Jun 15, 2017, 3:35:34 AM6/15/17
to vert.x
Hi,
I have few web services deployed on production. I want to fetch all the logs for any particular API request. For that, I want to prepend/append a unique request id.
However, I don't want to make changes at all the places we have logged something in out current system.
What would be the best way of making changes at single/few points and have unique request id logged in each request?

Alexander Lehmann

unread,
Jun 15, 2017, 2:33:05 PM6/15/17
to vert.x
We had a thread about MDC type logging and the current result is that this is not easy to do without code changes.

You can use a web context if you are using vertx-web, but you will have to pass the id everythere

javadevmtl

unread,
Jun 15, 2017, 5:01:42 PM6/15/17
to vert.x
Yep, unfortunately you have no choice...

But here's a something you can do...

1- Attach a custom log handler to the router. You can copy the one from vertx and modify it to your needs. We modified it by generating a uuid/request-id from headers and put into the context as rc.put("uuid/requestid", ...)

 router.route().handler(new MyCustomLogHandler());

2- In your web handlers you can access it with rc.get("uuid/or what ever you called it."
3- If you are using eventbus or services, you need to pass the uuid to your service function.
4- Optional, but good to have. Using logstash-logback-encoder (Good way to create json logs, DOES NOT really require logstash). It allows you to do stuff like this... 

logger.info("UUID: {}, Message: {}",
value("uuid", uuid), <-- static method from logstash-logback-encoder. Basically injects key/value into the log and then outputs as Json.
message, 
value("tag", "general"));

Wu JianWei

unread,
Dec 8, 2017, 5:40:07 AM12/8/17
to vert.x

you can use the thread local value to save request id.

i used this method in my project

Alexander Lehmann

unread,
Dec 8, 2017, 7:10:32 AM12/8/17
to vert.x
ThreadLocal will not work quite as you would expect since there are only a few threads that get used for different requests
Reply all
Reply to author
Forward
0 new messages