Rails日志中的完整URL

编程入门 行业动态 更新时间:2024-10-27 10:19:50
本文介绍了Rails日志中的完整URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可以在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

本文发布于:2023-11-24 05:04:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:完整   日志   Rails   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!