静音 ActiveStorage 日志

Mute ActiveStorage Logs

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

如有任何帮助,我们将不胜感激。 :)

尝试

<!--- turn off logger --->
old_logger = ActiveStorage.logger
ActiveStorage.logger = nil

<!--- turn on logger --->
ActiveStorage.logger = old_logger

此代码可用于将 sql 绘制到日志中的代码执行位置。首先关闭记录器,然后在代码运行后淹没您的日志,您可以重新打开记录器。或者您可以将 ActiveStorage.logger = nil 加载到初始化程序中(例如 config/initializers/active_storage_logger.rb,它将关闭所有活动存储日志记录。

根据EvilMartians的文章我设置了

config.active_record.verbose_query_logs 选项 false inside application.rb.

部分解决了问题。

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

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

我为使日志稍微安静一些而做的一件事是从日志中过滤一些与 ActiveStorage 相关的参数。我在 config/initializers/filter_parameter_logging.rb:

中的过滤参数中添加了 :encoded_key:signed_blob_id:variation_key
Rails.application.config.filter_parameters += [
  :password,
  # Filter ActiveStorage blob keys so that the logs aren't full of as much stuff.
  :encoded_key,
  :signed_blob_id,
  :variation_key
]

之前:

Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSm5kbUZ5YVdGdWRITXZiMDVvUjNCblJIQm9RVWd6YWtGeFFWUlpPV0Z3U2tWSUwyRTBZemd3WVRCallUUTJZalJtWmpObE16Vm1aV1l5TVRFelltWmxPREptWlRsalpUbGhNalE0WmpWaE9UZ3hZVEk1WkdVek5XUmxNemt4Wm1VNE1HUUdPZ1pGVkRvUVpHbHpjRzl6YVhScGIyNUpJazFwYm14cGJtVTdJR1pwYkdWdVlXMWxQU0kzWDJaaGEyVnlYMk52ZG1WeUxtcHdaeUk3SUdacGJHVnVZVzFsS2oxVlZFWXRPQ2NuTjE5bVlXdGxjbDlqYjNabGNpNXFjR2NHT3daVU9oRmpiMjUwWlc1MFgzUjVjR1ZKSWc5cGJXRm5aUzlxY0dWbkJqc0dWQT09IiwiZXhwIjoiMjAxOS0wMi0yNFQyMDoxNTo1MS45OTFaIiwicHVyIjoiYmxvYl9rZXkifX0=--c8b11cdaa201d28d585f62aaa7c8cf6af75e7cab/7_faker_cover.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%227_faker_cover.jpg%22%3B+filename%2A%3DUTF-8%27%277_faker_cover.jpg" for 127.0.0.1 at 2019-02-24 13:10:52 -0700
Processing by ActiveStorage::DiskController#show as JPEG
  Parameters: {"content_type"=>"image/jpeg", "disposition"=>"inline; filename=\"7_faker_cover.jpg\"; filename*=UTF-8''7_faker_cover.jpg", "encoded_key"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSm5kbUZ5YVdGdWRITXZiMDVvUjNCblJIQm9RVWd6YWtGeFFWUlpPV0Z3U2tWSUwyRTBZemd3WVRCallUUTJZalJtWmpObE16Vm1aV1l5TVRFelltWmxPREptWlRsalpUbGhNalE0WmpWaE9UZ3hZVEk1WkdVek5XUmxNemt4Wm1VNE1HUUdPZ1pGVkRvUVpHbHpjRzl6YVhScGIyNUpJazFwYm14cGJtVTdJR1pwYkdWdVlXMWxQU0kzWDJaaGEyVnlYMk52ZG1WeUxtcHdaeUk3SUdacGJHVnVZVzFsS2oxVlZFWXRPQ2NuTjE5bVlXdGxjbDlqYjNabGNpNXFjR2NHT3daVU9oRmpiMjUwWlc1MFgzUjVjR1ZKSWc5cGJXRm5aUzlxY0dWbkJqc0dWQT09IiwiZXhwIjoiMjAxOS0wMi0yNFQyMDoxNTo1MS45OTFaIiwicHVyIjoiYmxvYl9rZXkifX0=--c8b11cdaa201d28d585f62aaa7c8cf6af75e7cab", "filename"=>"7_faker_cover"}
Completed 200 OK in 2ms (ActiveRecord: 0.0ms)

之后:

Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSm5kbUZ5YVdGdWRITXZiMDVvUjNCblJIQm9RVWd6YWtGeFFWUlpPV0Z3U2tWSUwyRTBZemd3WVRCallUUTJZalJtWmpObE16Vm1aV1l5TVRFelltWmxPREptWlRsalpUbGhNalE0WmpWaE9UZ3hZVEk1WkdVek5XUmxNemt4Wm1VNE1HUUdPZ1pGVkRvUVpHbHpjRzl6YVhScGIyNUpJazFwYm14cGJtVTdJR1pwYkdWdVlXMWxQU0kzWDJaaGEyVnlYMk52ZG1WeUxtcHdaeUk3SUdacGJHVnVZVzFsS2oxVlZFWXRPQ2NuTjE5bVlXdGxjbDlqYjNabGNpNXFjR2NHT3daVU9oRmpiMjUwWlc1MFgzUjVjR1ZKSWc5cGJXRm5aUzlxY0dWbkJqc0dWQT09IiwiZXhwIjoiMjAxOS0wMi0yNFQyMDoxNjozNy43MTlaIiwicHVyIjoiYmxvYl9rZXkifX0=--223b806011854805e1e50ca952099bcdbc4d8bdd/7_faker_cover.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%227_faker_cover.jpg%22%3B+filename%2A%3DUTF-8%27%277_faker_cover.jpg" for 127.0.0.1 at 2019-02-24 13:11:37 -0700
Processing by ActiveStorage::DiskController#show as JPEG
  Parameters: {"content_type"=>"image/jpeg", "disposition"=>"inline; filename=\"7_faker_cover.jpg\"; filename*=UTF-8''7_faker_cover.jpg", "encoded_key"=>"[FILTERED]", "filename"=>"7_faker_cover"}
Completed 200 OK in 1ms (ActiveRecord: 0.0ms)

大部分变化在第三行,参数短了很多。不幸的是,这几乎是我能弄清楚的全部

我很好奇是否可以将任何标记为磁盘存储的日志静音('Checked if file exists at key:' 和 'Generated URL for file at key:' 行):

Started GET "/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBGQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6b1335aa96f07d0e8a19178e8d4c65d4a6b6ec60/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9MY21WemFYcGxTU0lOTXpBd2VEVXdNRDRHT2daRlZBPT0iLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--aca35120fd4bdbafe23b34690fa45f96280194c4/7_faker_cover.jpg" for 127.0.0.1 at 2019-02-24 13:11:37 -0700
Processing by ActiveStorage::RepresentationsController#show as JPEG
  Parameters: {"signed_blob_id"=>"[FILTERED]", "variation_key"=>"[FILTERED]", "filename"=>"7_faker_cover"}
  ActiveStorage::Blob Load (1.1ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" =  LIMIT   [["id", 15], ["LIMIT", 1]]
  ↳ /Users/connorshea/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
  Disk Storage (0.1ms) Checked if file exists at key: variants/oNhGpgDphAH3jAqATY9apJEH/a4c80a0ca46b4ff3e35fef2113bfe82fe9ce9a248f5a981a29de35de391fe80d (yes)
  Disk Storage (276.0ms) Generated URL for file at key: variants/oNhGpgDphAH3jAqATY9apJEH/a4c80a0ca46b4ff3e35fef2113bfe82fe9ce9a248f5a981a29de35de391fe80d (http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSm5kbUZ5YVdGdWRITXZiMDVvUjNCblJIQm9RVWd6YWtGeFFWUlpPV0Z3U2tWSUwyRTBZemd3WVRCallUUTJZalJtWmpObE16Vm1aV1l5TVRFelltWmxPREptWlRsalpUbGhNalE0WmpWaE9UZ3hZVEk1WkdVek5XUmxNemt4Wm1VNE1HUUdPZ1pGVkRvUVpHbHpjRzl6YVhScGIyNUpJazFwYm14cGJtVTdJR1pwYkdWdVlXMWxQU0kzWDJaaGEyVnlYMk52ZG1WeUxtcHdaeUk3SUdacGJHVnVZVzFsS2oxVlZFWXRPQ2NuTjE5bVlXdGxjbDlqYjNabGNpNXFjR2NHT3daVU9oRmpiMjUwWlc1MFgzUjVjR1ZKSWc5cGJXRm5aUzlxY0dWbkJqc0dWQT09IiwiZXhwIjoiMjAxOS0wMi0yNFQyMDoxNjozNy43MTlaIiwicHVyIjoiYmxvYl9rZXkifX0=--223b806011854805e1e50ca952099bcdbc4d8bdd/7_faker_cover.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%227_faker_cover.jpg%22%3B+filename%2A%3DUTF-8%27%277_faker_cover.jpg)
Redirected to http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSm5kbUZ5YVdGdWRITXZiMDVvUjNCblJIQm9RVWd6YWtGeFFWUlpPV0Z3U2tWSUwyRTBZemd3WVRCallUUTJZalJtWmpObE16Vm1aV1l5TVRFelltWmxPREptWlRsalpUbGhNalE0WmpWaE9UZ3hZVEk1WkdVek5XUmxNemt4Wm1VNE1HUUdPZ1pGVkRvUVpHbHpjRzl6YVhScGIyNUpJazFwYm14cGJtVTdJR1pwYkdWdVlXMWxQU0kzWDJaaGEyVnlYMk52ZG1WeUxtcHdaeUk3SUdacGJHVnVZVzFsS2oxVlZFWXRPQ2NuTjE5bVlXdGxjbDlqYjNabGNpNXFjR2NHT3daVU9oRmpiMjUwWlc1MFgzUjVjR1ZKSWc5cGJXRm5aUzlxY0dWbkJqc0dWQT09IiwiZXhwIjoiMjAxOS0wMi0yNFQyMDoxNjozNy43MTlaIiwicHVyIjoiYmxvYl9rZXkifX0=--223b806011854805e1e50ca952099bcdbc4d8bdd/7_faker_cover.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%227_faker_cover.jpg%22%3B+filename%2A%3DUTF-8%27%277_faker_cover.jpg
Completed 302 Found in 300ms (ActiveRecord: 16.7ms)

我假设这些使用的是 Rails' TaggedLogging functionality, but it doesn't look like they are (see the source code)。缺少猴子修补 ActiveStorage 的记录器(这似乎是一个非常糟糕的主意)我不确定改善这种情况的好方法。

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

config.lograge.ignore_actions = [
  "ActiveStorage::DiskController#show",
  "ActiveStorage::RepresentationsController#show"
]

我发现这是最简单(也许但不是万无一失)的解决方案,可以更轻松地进行开发。它使用标准 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

我尝试了上面的答案,其中 none 有效并寻找更多解决方案,我发现这个 gem 删除了 ActiveStorage 日志(至少在 Rails 5.2 上) :
https://github.com/igorkasyanchuk/active_storage_silent_logs

如文档中所述,在您的 puma.rb 集合中:
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 1 }
如果你想让日志 100% 静音