Module: LogStruct::Integrations::Shrine

Extended by:
IntegrationInterface, T::Sig
Defined in:
lib/log_struct/integrations/shrine.rb

Overview

Shrine integration for structured logging

Class Method Summary collapse

Methods included from IntegrationInterface

setup

Class Method Details

.setup(config) ⇒ Boolean?

Set up Shrine structured logging

Parameters:

Returns:

  • (Boolean, nil)


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/log_struct/integrations/shrine.rb', line 19

def self.setup(config)
  return nil unless defined?(::Shrine)
  return nil unless config.enabled
  return nil unless config.integrations.enable_shrine

  # Create a structured log subscriber for Shrine
  # ActiveSupport::Notifications::Event has name, time, end, transaction_id, payload, and duration
  shrine_log_subscriber = T.unsafe(lambda do |event|
    payload = event.payload.except(:io, :metadata, :name).dup

    # Map event name to Event type
    event_type = case event.name
    when :upload then Event::Upload
    when :download then Event::Download
    when :delete then Event::Delete
    when :metadata then Event::Metadata
    when :exists then Event::Exist # ActiveStorage uses 'exist', may as well use that
    else Event::Unknown
    end

    # Create structured log data
    log_data = Log::Shrine.new(
      source: Source::Shrine,
      event: event_type,
      duration: event.duration,
      storage: payload[:storage],
      location: payload[:location],
      uploader: payload[:uploader],
      upload_options: payload[:upload_options],
      download_options: payload[:download_options],
      options: payload[:options],
      # Data is flattened by the JSON formatter
      additional_data: payload.except(
        :storage,
        :location,
        :uploader,
        :upload_options,
        :download_options,
        :options
      )
    )

    # Pass the structured hash to the logger
    # If Rails.logger has our Formatter, it will handle JSON conversion
    ::Shrine.logger.info log_data
  end)

  # Configure Shrine to use our structured log subscriber
  ::Shrine.plugin :instrumentation,
    events: %i[upload exists download delete],
    log_subscriber: shrine_log_subscriber

  true
end