aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib/active_storage/log_subscriber.rb
blob: 5c1b8d23ef3431fff9d14f830111af6ec453ebb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require "active_support/log_subscriber"

module ActiveStorage
  class LogSubscriber < ActiveSupport::LogSubscriber
    def service_upload(event)
      message = "Uploaded file to key: #{key_in(event)}"
      message << " (checksum: #{event.payload[:checksum]})" if event.payload[:checksum]
      info event, color(message, GREEN)
    end

    def service_download(event)
      info event, color("Downloaded file from key: #{key_in(event)}", BLUE)
    end

    def service_delete(event)
      info event, color("Deleted file from key: #{key_in(event)}", RED)
    end

    def service_exist(event)
      debug event, color("Checked if file exist at key: #{key_in(event)} (#{event.payload[:exist] ? "yes" : "no"})", BLUE)
    end

    def service_url(event)
      debug event, color("Generated URL for file at key: #{key_in(event)} (#{event.payload[:url]})", BLUE)
    end

    def logger
      ActiveStorage::Service.logger
    end

    private
      def info(event, colored_message)
        super log_prefix_for_service(event) + colored_message
      end

      def debug(event, colored_message)
        super log_prefix_for_service(event) + colored_message
      end

      def log_prefix_for_service(event)
        color "  #{event.payload[:service]} Storage (#{event.duration.round(1)}ms) ", CYAN
      end

      def key_in(event)
        event.payload[:key]
      end
  end
end

ActiveStorage::LogSubscriber.attach_to :active_storage