使ActiveStorage日志静音

时间:2018-09-03 10:29:00

标签: 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

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

6 个答案:

答案 0 :(得分:3)

根据EvilMartians我设置的文章

config.active_record.verbose_query_logsfalseapplication.rb的选项。

部分解决了问题。

似乎Active Storage人们认为最冗长的日志记录非常重要。因此,现在有关文件的日志在磁盘上的大小与已存储文件的大小相同。

最令人讨厌的是这些消息的级别是INFO。我不明白为什么它不是调试...

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

我发现这是最简单(也许但不是防弹)的解决方案,可以简化开发。它使用标准的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

答案 3 :(得分:0)

为了使日志稍微安静一些,我要做的一件事是从日志中过滤一些与ActiveStorage相关的参数。我在:encoded_key中的过滤参数中添加了:signed_blob_id:variation_keyconfig/initializers/filter_parameter_logging.rb

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)

大部分更改都在第三行中,参数要短得多。不幸的是,我几乎可以弄清楚

我很好奇是否可以使所有标记为“磁盘存储”的日志静音(“检查文件是否存在于键:”行和“生成的文件存在URL处:”行):

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" = $1 LIMIT $2  [["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功能,但看起来不一样(请参阅source code)。缺少修补猴子的ActiveStorage记录器(这似乎是个坏主意),我不确定是否可以改善这种情况。

答案 4 :(得分:0)

我尝试了上面的答案,但没有一个起作用,并在寻找更多的解决方案,我发现该宝石删除了ActiveStorage日志(至少在Rails 5.2上如此):
https://github.com/igorkasyanchuk/active_storage_silent_logs

如文档中所述,在您的puma.rb中设置:
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 1 }
如果您希望日志100%静音

答案 5 :(得分:-1)

尝试

<!--- 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,它将关闭所有活动的存储日志记录。