Module: LogStruct::Integrations::Lograge

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

Overview

Lograge integration for structured request logging

Class Method Summary collapse

Methods included from IntegrationInterface

setup

Class Method Details

.apply_custom_options(event, options) ⇒ void

This method returns an undefined value.

Apply custom options from the application's configuration

Parameters:

  • event (ActiveSupport::Notifications::Event)
  • options (Hash{Symbol => T.untyped})


103
104
105
106
107
108
109
110
# File 'lib/log_struct/integrations/lograge.rb', line 103

def apply_custom_options(event, options)
  custom_options_proc = LogStruct.config.integrations.lograge_custom_options
  return unless custom_options_proc&.respond_to?(:call)

  # Call the proc with the event and options
  # The proc can modify the options hash directly
  custom_options_proc.call(event, options)
end

.configure_lograge(logstruct_config) ⇒ void

This method returns an undefined value.

Parameters:



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
# File 'lib/log_struct/integrations/lograge.rb', line 34

def configure_lograge(logstruct_config)
  ::Rails.application.configure do
    config.lograge.enabled = true
    # Use a raw formatter that just returns the log struct.
    # The struct is converted to JSON by our Formatter (after filtering, etc.)
    config.lograge.formatter = T.let(
      lambda do |data|
        # Convert the data hash to a Log::Request struct
        Log::Request.new(
          source: Source::Rails,
          event: Event::Request,
          timestamp: Time.now,
          http_method: data[:method],
          path: data[:path],
          format: data[:format],
          controller: data[:controller],
          action: data[:action],
          status: data[:status],
          duration: data[:duration],
          view: data[:view],
          db: data[:db],
          params: data[:params]
        )
      end,
      T.proc.params(hash: T::Hash[Symbol, T.untyped]).returns(Log::Request)
    )

    # Add custom options to lograge
    config.lograge.custom_options = lambda do |event|
      Integrations::Lograge.lograge_default_options(event)
    end
  end
end

.lograge_default_options(event) ⇒ Hash{Symbol => T.untyped}

Parameters:

  • event (ActiveSupport::Notifications::Event)

Returns:

  • (Hash{Symbol => T.untyped})


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/log_struct/integrations/lograge.rb', line 69

def lograge_default_options(event)
  # Extract essential fields from the payload
  options = event.payload.slice(
    :request_id,
    :host,
    :source_ip
  ).compact

  if event.payload[:params].present?
    options[:params] = event.payload[:params].except("controller", "action")
  end

  # Process headers if available
  process_headers(event, options)

  # Apply custom options from application if provided
  apply_custom_options(event, options)

  options
end

.process_headers(event, options) ⇒ void

This method returns an undefined value.

Process headers from the event payload

Parameters:

  • event (ActiveSupport::Notifications::Event)
  • options (Hash{Symbol => T.untyped})


92
93
94
95
96
97
98
99
# File 'lib/log_struct/integrations/lograge.rb', line 92

def process_headers(event, options)
  headers = event.payload[:headers]
  return if headers.blank?

  options[:user_agent] = headers["HTTP_USER_AGENT"]
  options[:content_type] = headers["CONTENT_TYPE"]
  options[:accept] = headers["HTTP_ACCEPT"]
end

.setup(logstruct_config) ⇒ Boolean?

Set up lograge for structured request logging

Parameters:

Returns:

  • (Boolean, nil)


21
22
23
24
25
26
27
28
29
# File 'lib/log_struct/integrations/lograge.rb', line 21

def setup(logstruct_config)
  return nil unless defined?(::Lograge)
  return nil unless logstruct_config.enabled
  return nil unless logstruct_config.integrations.enable_lograge

  configure_lograge(logstruct_config)

  true
end