Rails5:如何显示对 public 目录的访问日志

Rails5: How to show access log to public directory

我想查看 public 目录访问的日志,我该怎么做? 例如,我有一个名为 public/uploads/image/thumbnail.png 的文件,然后我想查看对该文件的访问日志。

我在本地开发环境中使用 Rails5 和 puma。

我通过编写一个中间件解决了我的问题。

class StaticLog
  def initialize(app)
    @app = app
  end

  def call(env)
    puts 'STATIC FILE ACCESS: ' + env['REQUEST_PATH']
    res = @app.call(env)
    res
  end
end

然后在 config/application.rb 中:

config.middleware.insert_before ActionDispatch::Static, 'StaticLog'

我在 application.rb 的末尾附加了我的中间件 class,因为它是临时的。