【问题标题】:How to print current username to log file in Rails.如何将当前用户名打印到 Rails 中的日志文件。
【发布时间】:2015-11-27 02:33:29
【问题描述】:

最近,我发现了标记的记录器。这是一个非常有用的选项,我在生产环境中进行配置。

# config/environments/production.rb

...

config.log_tags = [ :uuid,:remote_ip ]

...

# log/production.log

[4d23e817-eca8-4db1-ba5b-7456d3af7f65] [127.0.0.1] Started GET "/resources/id" for 127.0.0.1 at 2015-11-26 21:09:11 +0900

我还想将当前登录用户名打印到日志文件中,所以我尝试找到了一个链接How to log user_name in Rails?

但这对我不起作用。

我正在使用 devise 和 rails4。如何打印?

提前致谢。

【问题讨论】:

    标签: ruby-on-rails logging devise


    【解决方案1】:

    您可以使用不同的方法。我用过以下一个:

    application_controller.rb 中:

    before_action: print_current_user
    
    def print_current_user
       if user_signed_in?
          Rails.logger.debug "UserId: #{current_user.id}"    
       end
    end 
    

    或者,您可以使用lograge gem。

    【讨论】:

    • 感谢您的回答。然而,这不是我想要的。我想在所有日志中添加当前登录用户名 [4d23e817-eca8-4db1-ba5b-7456d3af7f65] [127.0.0.1] [chris] Started GET ...
    猜你喜欢
    • 2022-11-23
    • 2015-05-05
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2015-10-28
    • 2011-01-31
    相关资源
    最近更新 更多