如何记录异步 Thin+sinatra+rack 请求?
我正在编写第一个基于 Sinatra 的 Web 应用程序,作为另一个基于 TCP 的服务的前端,使用 EventMachine 和 async_sinatra 异步处理传入的 HTTP 请求。当我测试我的应用程序时,对同步路由的所有请求都会以通用日志格式记录到标准输出,但异步请求则不会。
我已经阅读了 async_sinatra、Sinatra、Thin 和 Rack 的源代码,看起来同步请求的日志记录是通过 CommonLogger#call 完成的。但是,我在 async_sinatra 或 Thin 中的异步代码中找不到似乎通过日志记录中间件传递异步请求的任何地方(我正在查看 Sinatra::Helpers#body 在 async_sinatra 和 Thin::Connection.post_process 写入 env Thin 的connection.rb:68 和request.rb:132 中的['.async_callback'])。
我对 C 有经验,但对 Ruby 比较陌生,所以如果我使用了一些术语或符号不正确,请纠正我。提前致谢。
编辑:这也会影响错误处理。如果异步请求中引发异常,则该请求永远不会完成,并且永远不会记录错误。
I'm writing my first Sinatra-based web app as a frontend to another TCP-based service, using EventMachine and async_sinatra to process incoming HTTP requests asynchronously. When I'm testing my app, all requests to synchronous routes are logged to stdout in common log format, but asynchronous requests are not.
I've read through bits of the source code to async_sinatra, Sinatra, Thin, and Rack, and it looks like logging of synchronous requests is done through CommonLogger#call. However, I can't find anywhere in the asynchronous code in async_sinatra or Thin that seems to pass asynchronous requests through the logging middleware (I'm looking at Sinatra::Helpers#body in async_sinatra and at Thin::Connection.post_process which is written into env['.async_callback'] in Thin's connection.rb:68 and request.rb:132).
I'm experienced with C but relatively new to Ruby, so if I've used some terminology or notation incorrectly, please correct me. Thanks in advance.
Edit: this also affects error handling. If an exception is raised in an asynchronous request, the request is never finished and the error is never logged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终发现将rack-async与async_sinatra一起使用会导致404页面、异常处理和日志记录方面的问题:
相反,我使用以下围绕
aroute
的包装器进行日志记录:这适用于相同版本的async_sinatra, Thin,还有我去年问这个问题时正在使用的Rack;较新的版本可能允许使用通用的 Rack 中间件进行日志记录。
I eventually found that using rack-async with async_sinatra was causing problems with 404 pages, exception handling, and logging:
Instead I used the following wrapper around
aroute
for logging:This is for the same versions of async_sinatra, Thin, and Rack that I was using when I asked this question last year; newer versions may allow the use of common Rack middleware for logging.
我在
sinatra-synchrony
上运行,因此我的核心与您略有不同。但基本上我解决了同样的问题。
以下是解决方案的摘要:
sinatra-synchrony
应用程序中,我正在运行以下中间件进行日志记录:
在应用程序中的任何位置,我都可以使用
Logger.log("message")
记录。I am running on
sinatra-synchrony
and therefore I have a slightly different core than you.But basically I solved the same problem.
Here is an abstract of the solution:
Rack::CommonLogger
, I use my own LoggerIn my
sinatra-synchrony
application I am running the following middleware for logging:Everywhere in the application I am able to use
Logger.log("message")
for logging.