Search code examples
ruby-on-railsrails-activestorage

Mute ActiveStorage Logs


ActiveStorage floods my dev logs so i'm drowning in requests for images on a page. Is there a way to mute active storage or atleast reduce the log entries so that I can use my logs again?

For example, for each of the images on a page that are accessed via ActiveStorage, I get one of these:

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

Any help would be greatly appreciated. :)


Solution

  • I found this as simplest (maybe but not bulletproof) solution for easier development. It uses standard Rails log and only replaces its default formatter. Works on Rails 6, probably will work on Rails 5 too.

    Place code bellow into 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 [email protected]?(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