Skip to content

Lograge custom_options fields not included in Log::Request output #4

@okdas

Description

@okdas

Problem

Custom fields added via config.integrations.lograge_custom_options are collected but not included in the final log output.

Reproduction

LogStruct.configure do |config|
  config.integrations.enable_lograge = true
  config.integrations.lograge_custom_options = ->(event, options) {
    options[:user_id] = event.payload[:user_id]
  }
end

Expected: {"source":"rails","event":"request","user_id":"abc-123",...}
Actual: {"src":"rails","evt":"request",...} (no user_id)

Root Cause

In lib/log_struct/integrations/lograge.rb, the formatter creates Log::Request.new(...) with only hardcoded fields (lines 40-65). Custom options added to the data hash are ignored because Log::Request only extracts specific fields.

Additionally, Log::Request has source_ip, user_agent, and request_id fields, but the formatter does not populate them from data even though lograge_default_options adds them.

Workaround

Disable LogStruct's lograge integration and configure Lograge directly with a dedicated STDOUT logger to bypass LogStruct's Rails.logger interception:

config.integrations.enable_lograge = false

# In a separate initializer:
config.lograge.enabled = true
config.lograge.formatter = Lograge::Formatters::Json.new
config.lograge.logger = ActiveSupport::Logger.new($stdout)
config.lograge.custom_options = ->(event) { { user_id: event.payload[:user_id] }.compact }

Environment

  • logstruct: 0.1.7
  • Rails: 8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions