logging - Vertx 日志记录:访问请求上下文
问题描述
这看起来很常见:我们想用一些自定义消息记录路由上下文相关数据。对于每线程请求应用程序,我们使用线程本地 MDC。但是对于 Vert.x 应用程序有没有类似的解决方案?可能是一些在事件循环中工作并管理所有请求的 Vertx 记录器。
谢谢你。
解决方案
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!
推荐阅读
- python - 闹钟在我的 tkinter GUI 上不起作用
- javascript - 将 javascript 事件附加到使用 AJAX 加载的内容
- php - 重定向后如何记住CodeIgniter(CI)中的输入表单数据而不是再次进入同一页面
- python - 如何裁剪多张图像使用文件中的列表边界框位置(python)?
- java - 如何解决“无法解析导入 org.mockito”?
- excel - 用于填充工作表的 VBA 循环
- objective-c - 如何在 iOS 上使用 Objective-C 创建 TCP IP 服务器
- c++ - 我需要比较 2 个字符串中的内容
- mariadb - 从同一个表中删除相同的记录会导致死锁
- python - 使用 Python 将 word 文件中的多个表提取到 Excel 文件