我如何登录异步薄+西纳特拉+机架请求? [英] How do I log asynchronous thin+sinatra+rack requests?

查看:195
本文介绍了我如何登录异步薄+西纳特拉+机架请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写我的第一个基于辛纳屈的web应用程序作为一个前端到另一台基于TCP的服务,使用EventMachine的和async_sinatra异步处理传入的HTTP请求。当我测试我的应用程序,为同步路由所有的请求都记录在普通日志格式标准输出,而异步请求则不是。

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.

我已经通过源$ C ​​$ C位读async_sinatra,西纳特拉,瘦,机架,它看起来像同步请求的记录是通过CommonLogger#调用完成。但是,我找不到在async_sinatra或薄,似乎都可以通过日志中间件异步请求异步code的任何地方(我看的西纳特拉::助手#身体在async_sinatra和的这是写进信封薄:: Connection.post_process ['async_callback。']薄的connection.rb: 68和request.rb:132)

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).

我经历了C,但相对较新的红宝石,所以如果我用一些术语或符号不正确,请大家指正。先谢谢了。

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.

编辑:这也影响了错误处理。如果异常是在异步请求中提出,该请求被从未完成并从未记录错误。

推荐答案

我最终发现,使用机架异步与async_sinatra是造成404页,异常处理,和日志记录的问题:

I eventually found that using rack-async with async_sinatra was causing problems with 404 pages, exception handling, and logging:

!! Unexpected error while processing request: undefined method `bytesize' for nil:NilClass

相反,我用左右 aroute 以下包装的记录:

module Sinatra::Async
    alias :oldaroute :aroute
    def aroute verb, path, opts = {}, &block
        # Based on aroute from async_sinatra

        run_method = :"RunA#{verb} #{path} #{opts.hash}"
        define_method run_method, &block

        log_method = :"LogA#{verb} #{path} #{opts.hash}"
        define_method(log_method) { |*a|
            puts "#{request.ip} - #{status} #{verb} #{path}"
        }

        oldaroute verb, path, opts do |*a|
            oldcb = request.env['async.callback']
            request.env['async.callback'] = proc { |*args|
                async_runner(log_method, *a)
                oldcb[*args]
            }
            async_runner(run_method, *a)
        end
    end
end

这是async_sinatra,薄型,齿条,我用我去年问这个问题的版本相同;新版本可允许记录使用通用Rack中间件的。

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.

这篇关于我如何登录异步薄+西纳特拉+机架请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆