2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-09 11:04:28 -04:00
|
|
|
require "active_support/log_subscriber"
|
|
|
|
|
2017-08-04 19:33:40 -04:00
|
|
|
module ActiveStorage
|
|
|
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
|
|
|
def service_upload(event)
|
|
|
|
message = "Uploaded file to key: #{key_in(event)}"
|
2017-08-12 08:32:15 -04:00
|
|
|
message += " (checksum: #{event.payload[:checksum]})" if event.payload[:checksum]
|
2017-08-04 19:33:40 -04:00
|
|
|
info event, color(message, GREEN)
|
|
|
|
end
|
2017-07-09 11:04:28 -04:00
|
|
|
|
2017-08-04 19:33:40 -04:00
|
|
|
def service_download(event)
|
|
|
|
info event, color("Downloaded file from key: #{key_in(event)}", BLUE)
|
|
|
|
end
|
2017-07-09 11:04:28 -04:00
|
|
|
|
2017-08-04 19:33:40 -04:00
|
|
|
def service_delete(event)
|
|
|
|
info event, color("Deleted file from key: #{key_in(event)}", RED)
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
|
|
|
|
2017-12-02 22:43:28 -05:00
|
|
|
def service_delete_prefixed(event)
|
|
|
|
info event, color("Deleted files by key prefix: #{event.payload[:prefix]}", RED)
|
|
|
|
end
|
|
|
|
|
2017-08-04 19:33:40 -04:00
|
|
|
def service_exist(event)
|
2017-08-15 12:48:19 -04:00
|
|
|
debug event, color("Checked if file exists at key: #{key_in(event)} (#{event.payload[:exist] ? "yes" : "no"})", BLUE)
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
|
|
|
|
2017-08-04 19:33:40 -04:00
|
|
|
def service_url(event)
|
|
|
|
debug event, color("Generated URL for file at key: #{key_in(event)} (#{event.payload[:url]})", BLUE)
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
|
|
|
|
2017-08-04 19:33:40 -04:00
|
|
|
def logger
|
2017-10-22 13:16:59 -04:00
|
|
|
ActiveStorage.logger
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
2017-08-04 19:33:40 -04:00
|
|
|
|
|
|
|
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
|
2017-07-09 11:04:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
ActiveStorage::LogSubscriber.attach_to :active_storage
|