将ActiveStorage日志静音

PR *_*ead 10 ruby-on-rails rails-activestorage

ActiveStorage充斥我的开发日志,所以我淹没在页面上的图像请求.有没有办法静音活动存储或至少减少日志条目,以便我可以再次使用我的日志?

例如,对于通过ActiveStorage访问的页面上的每个图像,我得到以下其中一个:

2018-09-03 11:07:42.181697 I [75455:70130232359340 log_subscriber.rb:12] (2.365ms) ActiveStorage::DiskController -- Completed #show -- {
        :controller => "ActiveStorage::DiskController",
            :action => "show",
            :params => {
        "content_type" => "image/jpeg",
         "disposition" => "inline; filename=\"faded-flip.jpg\"; filename*=UTF-8''faded-flip.jpg",
         "encoded_key" => "eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJbk4yWVhKcFlXNTBjeTh6TnpRMVlqSmtZaTB3WlRCakxUUTVaRFV0WW1Ga01DMWxNRFl4TWpFd09Ua3dOMkl2TkdRME1qQTBNR1EzTjJaaE5UZ3pOVFU1WXpSbVpqaGlOVFpoWVdVd01ESmhabVJqWW1GaE5HTmxPRFV3WXpneU1UUmhPVEpsWlRVNVl6bGlPRGs0WVFZNkJrVlUiLCJleHAiOiIyMDE4LTA5LTAzVDEwOjEyOjMyLjUwN1oiLCJwdXIiOiJibG9iX2tleSJ9fQ==--b9eed7f4cf3d71fcb697429188e1a1a74ba88bec",
            "filename" => "faded-flip"
    },
            :format => "JPEG",
            :method => "GET",
              :path => "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJbk4yWVhKcFlXNTBjeTh6TnpRMVlqSmtZaTB3WlRCakxUUTVaRFV0WW1Ga01DMWxNRFl4TWpFd09Ua3dOMkl2TkdRME1qQTBNR1EzTjJaaE5UZ3pOVFU1WXpSbVpqaGlOVFpoWVdVd01ESmhabVJqWW1GaE5HTmxPRFV3WXpneU1UUmhPVEpsWlRVNVl6bGlPRGs0WVFZNkJrVlUiLCJleHAiOiIyMDE4LTA5LTAzVDEwOjEyOjMyLjUwN1oiLCJwdXIiOiJibG9iX2tleSJ9fQ==--b9eed7f4cf3d71fcb697429188e1a1a74ba88bec/faded-flip.jpg",
            :status => 200,
      :view_runtime => 0.69,
        :db_runtime => 0.0,
    :status_message => "OK"
}
127.0.0.1 - - [03/Sep/2018:11:07:41 BST] "GET /rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJbk4yWVhKcFlXNTBjeTh6TnpRMVlqSmtZaTB3WlRCakxUUTVaRFV0WW1Ga01DMWxNRFl4TWpFd09Ua3dOMkl2TkdRME1qQTBNR1EzTjJaaE5UZ3pOVFU1WXpSbVpqaGlOVFpoWVdVd01ESmhabVJqWW1GaE5HTmxPRFV3WXpneU1UUmhPVEpsWlRVNVl6bGlPRGs0WVFZNkJrVlUiLCJleHAiOiIyMDE4LTA5LTAzVDEwOjEyOjMyLjUwN1oiLCJwdXIiOiJibG9iX2tleSJ9fQ==--b9eed7f4cf3d71fcb697429188e1a1a74ba88bec/faded-flip.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22faded-flip.jpg%22%3B+filename%2A%3DUTF-8%27%27faded-flip.jpg HTTP/1.1" 200 21345
http://localhost:3000/users/password/new -> /rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJbk4yWVhKcFlXNTBjeTh6TnpRMVlqSmtZaTB3WlRCakxUUTVaRFV0WW1Ga01DMWxNRFl4TWpFd09Ua3dOMkl2TkdRME1qQTBNR1EzTjJaaE5UZ3pOVFU1WXpSbVpqaGlOVFpoWVdVd01ESmhabVJqWW1GaE5HTmxPRFV3WXpneU1UUmhPVEpsWlRVNVl6bGlPRGs0WVFZNkJrVlUiLCJleHAiOiIyMDE4LTA5LTAzVDEwOjEyOjMyLjUwN1oiLCJwdXIiOiJibG9iX2tleSJ9fQ==--b9eed7f4cf3d71fcb697429188e1a1a74ba88bec/faded-flip.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22faded-flip.jpg%22%3B+filename%2A%3DUTF-8%27%27faded-flip.jpg
2018-09-03 11:07:42.234412 D [75455:70130203086520 log_subscriber.rb:8] ActiveStorage::DiskController -- Processing #show
2018-09-03 11:07:42.239153 I [75455:70130203086520 log_subscriber.rb:96] Rails --   FlatDisk Storage (0.1ms) Downloaded file from key: variants/ef6bca1d-bfd1-485e-b7cd-88a0e1e95404/4d42040d77fa583559c4ff8b56aae002afdcbaa4ce850c8214a92ee59c9b898a
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.:)

Arc*_*her 8

我正在使用Lograge Gem来隐藏这些请求。

config.lograge.ignore_actions = [
  "ActiveStorage::DiskController#show",
  "ActiveStorage::RepresentationsController#show"
]
Run Code Online (Sandbox Code Playgroud)


Ale*_*org 5

根据我设定的EvilMartians的文章

config.active_record.verbose_query_logsfalse里面的选项application.rb

它部分解决了问题。

似乎 Active Storage 人员认为最详细的日志记录非常重要。所以现在关于文件的日志与存储的文件在磁盘上的大小相同。

而最让人恼火的是这些消息的级别是INFO。我不明白为什么它不是调试...


小智 4

我发现这是最简单的(也许但不是万无一失的)解决方案,可以更轻松地开发。它使用标准 Rails 日志,仅替换其默认格式化程序。适用于 Rails 6,可能也适用于 Rails 5。

将以下代码放入config/environments/development.rb

Rails.application.configure do
    .
    .
    .

  class MyLogFormatter
    def initialize
      # Suppress is an array of request uuids. Each listed uuid means no messages from this request.
      @suppress = []
    end

    def call(severity, datetime, progname, message)
      # Get uuid, which we need to properly distinguish between parallel requests.
      # Also remove uuid information from log (that's why we match the rest of message)
      matches = /\[([0-9a-zA-Z\-_]+)\] (.*)/m.match(message)

      if matches
        uuid = matches[1]
        message = matches[2]

        if @suppress.include?(uuid) && message.start_with?("Completed ")
          # Each request in Rails log ends with "Completed ..." message, so do suppressed messages.
          @suppress.delete(uuid)
          return nil

        elsif message.start_with?("Processing by ActiveStorage::DiskController#show", "Processing by ActiveStorage::BlobsController#show", "Processing by ActiveStorage::RepresentationsController#show", "Started GET \"/rails/active_storage/disk/", "Started GET \"/rails/active_storage/blobs/", "Started GET \"/rails/active_storage/representations/")
          # When we use ActiveStorage disk provider, there are three types of request: Disk requests, Blobs requests and Representation requests.
          # These three types we would like to hide.
          @suppress << uuid
          return nil

        elsif !@suppress.include?(uuid)
          # All messages, which are not suppressed, print. New line must be added here.
          return "#{message}\n"
        end

      else
        # Return message as it is (including new line at the end)
        return "#{message}\n"
      end

    end
  end

  config.log_tags = [:uuid]
  config.log_formatter = MyLogFormatter.new
    .
    .
    .
end
Run Code Online (Sandbox Code Playgroud)