加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何在Rails中记录user_name?

发布时间:2020-12-16 20:04:05 所属栏目:百科 来源:网络整理
导读:我在Rails 3中使用Devise.我想在production.log中看到current_user的名字. 我想像这样配置rails: config.log_tags = [:user_name] 解决方法 我发现这个小巧棘手但工作的解决方案. 监督员将用户信息存储在会话中,但req.session不可用于记录. 所以你必须添加
我在Rails 3中使用Devise.我想在production.log中看到current_user的名字.

我想像这样配置rails:

config.log_tags = [:user_name]

解决方法

我发现这个小巧棘手但工作的解决方案.

监督员将用户信息存储在会话中,但req.session不可用于记录.

所以你必须添加到config / application.rb

config.middleware.delete(ActionDispatch::Cookies)
config.middleware.delete(ActionDispatch::Session::CookieStore)
config.middleware.insert_before(Rails::Rack::Logger,ActionDispatch::Session::CookieStore)
config.middleware.insert_before(ActionDispatch::Session::CookieStore,ActionDispatch::Cookies)

然后创建文件config / initializers / logging.rb

Rails.configuration.log_tags = [
  proc do |req|
    if req.session["warden.user.user.key"].nil?
      "Anonym"
    else
      "user_id:#{req.session["warden.user.user.key"][0][0]}"
    end
  end
]

现在我看到这个匿名:

[Anonym] Served asset ...

这为用户:

[user_id:1] Served asset ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读