Module: LogStruct::Integrations::Lograge

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

Overview

Lograge integration for structured request logging

Constant Summary collapse

LOGRAGE_KNOWN_KEYS =
T.let(
  [
    :method,
    :path,
    :format,
    :controller,
    :action,
    :status,
    :duration,
    :view,
    :db,
    :params,
    :request_id,
    :source_ip,
    :user_agent,
    :referer,
    :host,
    :content_type,
    :accept
  ].freeze,
  T::Array[Symbol]
)

Class Method Summary collapse

Methods included from IntegrationInterface

setup

Class Method Details

.apply_custom_options(event, options) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/log_struct/integrations/lograge.rb', line 118

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

.build_request_log(data) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/log_struct/integrations/lograge.rb', line 128

def build_request_log(data)
  normalized_data = normalize_lograge_data(data)

  # Coerce common fields to expected types
  status = ((s = normalized_data[:status]) && s.respond_to?(:to_i)) ? s.to_i : s
  duration_ms = ((d = normalized_data[:duration]) && d.respond_to?(:to_f)) ? d.to_f : d
  view = ((v = normalized_data[:view]) && v.respond_to?(:to_f)) ? v.to_f : v
  db = ((b = normalized_data[:db]) && b.respond_to?(:to_f)) ? b.to_f : b

  params = normalized_data[:params]
  params = params.deep_symbolize_keys if params&.respond_to?(:deep_symbolize_keys)

  additional_data = extract_additional_data(normalized_data)

  Log::Request.new(
    http_method: normalized_data[:method]&.to_s,
    path: normalized_data[:path]&.to_s,
    format: normalized_data[:format]&.to_sym,
    controller: normalized_data[:controller]&.to_s,
    action: normalized_data[:action]&.to_s,
    status: status,
    duration_ms: duration_ms,
    view: view,
    database: db,
    params: params,
    source_ip: normalized_data[:source_ip]&.to_s,
    user_agent: normalized_data[:user_agent]&.to_s,
    referer: normalized_data[:referer]&.to_s,
    host: normalized_data[:host]&.to_s,
    content_type: normalized_data[:content_type]&.to_s,
    accept: normalized_data[:accept]&.to_s,
    additional_data: additional_data,
    timestamp: Time.now
  )
end

.configure_lograge(logstruct_config) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/log_struct/integrations/lograge.rb', line 57

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|
        LogStruct::Integrations::Lograge.build_request_log(data)
      end,
      T.proc.params(hash: T::Hash[T.any(Symbol, String), 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

.extract_additional_data(data) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/log_struct/integrations/lograge.rb', line 172

def extract_additional_data(data)
  extras = T.let({}, T::Hash[Symbol, T.untyped])
  data.each do |key, value|
    next if LOGRAGE_KNOWN_KEYS.include?(key)
    next if value.nil?

    extras[key] = value
  end

  return nil if extras.empty?

  extras
end

.lograge_default_options(event) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/log_struct/integrations/lograge.rb', line 77

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

.normalize_lograge_data(data) ⇒ Object



165
166
167
168
169
# File 'lib/log_struct/integrations/lograge.rb', line 165

def normalize_lograge_data(data)
  data.each_with_object({}) do |(key, value), normalized|
    normalized[key.to_s.to_sym] = value
  end
end

.process_headers(event, options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/log_struct/integrations/lograge.rb', line 100

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

  # Rails' ActionDispatch::RequestId middleware stores request_id in headers
  # Only set if not already present in payload (payload takes precedence)
  if options[:request_id].blank? && headers["action_dispatch.request_id"].present?
    options[:request_id] = headers["action_dispatch.request_id"]
  end

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

.setup(logstruct_config) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/log_struct/integrations/lograge.rb', line 44

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