可以在Rails应用程序的日志中获取完整的URL吗?目前我得到的东西如下:
Is it possible to get the full URLs in the logs of a Rails application? Currently I'm getting something like:
在2011-08-27 13:13:10启动GET/为127.0.0.1 +0200
Started GET "/" for 127.0.0.1 at 2011-08-27 13:13:10 +0200
但我需要得到:
在2011-08-27 13:13:10 +0200
Started GET "localhost:3000/" for 127.0.0.1 at 2011-08-27 13:13:10 +0200
$ b开始GEThttp:// localhost:3000 / $ b
因为这是域名重要的应用程序。
because this is an app where the domains are important.
推荐答案从Rails 3.2.12开始,而不是monkey-patching before_dispatch或call_app,现在有一种接收请求的方法,并在2012-09-26 14:51:42 -0700返回启动GET/ session / new为127.0.0.1 bit。
As of Rails 3.2.12, instead of monkey-patching before_dispatch or call_app, there is now a method that receives the request and returns the Started GET "/session/new" for 127.0.0.1 at 2012-09-26 14:51:42 -0700 bit.
所以你可以在你自己的记录器或猴子补丁中覆盖那个方法,只是这个方法。
So you can instead override that method in your own logger or monkey patch just that method.
# or "class Rails::Rack::Logger < ActiveSupport::LogSubscriber" for monkey patch class URLLogger < Rails::Rack::Logger def started_request_message(request) 'Started %s "%s%s%s" for %s at %s' % [ request.request_method, request.protocol, request.host_with_port, request.filtered_path, request.ip, Time.now.to_default_s ] end end如果您的子类不要忘记修改 config / application.rb (并确保URLLogger在配置块之前加载):
If you subclass don't forget to modify the middleware chain in config/application.rb (and make sure URLLogger is loaded before the configuration block):
config.middleware.insert_before(Rails::Rack::Logger, URLLogger) config.middleware.delete(Rails::Rack::Logger)更多推荐
Rails日志中的完整URL
发布评论