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
-
.apply_custom_options(event, options) ⇒ void
Apply custom options from the application's configuration.
- .configure_lograge(logstruct_config) ⇒ void
- .lograge_default_options(event) ⇒ Hash{Symbol => T.untyped}
-
.process_headers(event, options) ⇒ void
Process headers from the event payload.
-
.setup(logstruct_config) ⇒ Boolean?
Set up lograge for structured request logging.
Methods included from IntegrationInterface
Class Method Details
.apply_custom_options(event, options) ⇒ void
This method returns an undefined value.
Apply custom options from the application's configuration
103 104 105 106 107 108 109 110 |
# File 'lib/log_struct/integrations/lograge.rb', line 103 def (event, ) = LogStruct.config.integrations. return unless &.respond_to?(:call) # Call the proc with the event and options # The proc can modify the options hash directly .call(event, ) end |
.configure_lograge(logstruct_config) ⇒ void
This method returns an undefined value.
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. = lambda do |event| Integrations::Lograge.(event) end end end |
.lograge_default_options(event) ⇒ 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 (event) # Extract essential fields from the payload = event.payload.slice( :request_id, :host, :source_ip ).compact if event.payload[:params].present? [:params] = event.payload[:params].except("controller", "action") end # Process headers if available process_headers(event, ) # Apply custom options from application if provided (event, ) end |
.process_headers(event, options) ⇒ void
This method returns an undefined value.
Process headers from the event payload
92 93 94 95 96 97 98 99 |
# File 'lib/log_struct/integrations/lograge.rb', line 92 def process_headers(event, ) headers = event.payload[:headers] return if headers.blank? [:user_agent] = headers["HTTP_USER_AGENT"] [:content_type] = headers["CONTENT_TYPE"] [:accept] = headers["HTTP_ACCEPT"] end |
.setup(logstruct_config) ⇒ Boolean?
Set up lograge for structured request logging
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 |