lograge icon indicating copy to clipboard operation
lograge copied to clipboard

It seems that after upgrading to Rails 6, custom logs are no longer displayed as expected

Open EasonKaku opened this issue 5 months ago • 0 comments

Environment: Rails 5

Code:

config/environments/production.rb

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger = ActiveSupport::TaggedLogging.new(logger)
  end

  # loggly settings - lograge
  config.lograge.enabled = true
  config.lograge.formatter = Lograge::Formatters::Json.new
  config.lograge.custom_options = lambda do |event|
    exceptions = %w(controller action format id)
    {
      params: event.payload[:params].except(*exceptions),
      remote_ip: event.payload[:remote_ip],
      uid: event.payload[:uid],
      request_headers: event.payload[:request_headers],
      time: event.time
    }
  end
  
 config/initializers/lograge.rb
Rails.application.configure do
  config.lograge.base_controller_class = 'ActionController::API'
end

Result: 截圖 2024-09-19 下午4 21 03 截圖 2024-09-19 晚上9 13 05

The above is the result I saw in the Rails 5 environment on google cloud logs explorer. However, after upgrading to Rails 6.1.7.2, the code remains unchanged, but the logs no longer appear on google cloud logs explorer as shown in the image. How should I adjust it?"

But when I configure it like this, the logs appear again. However, these logs are different from what I originally had. How can I adjust them to match my previous logs?

Code:

config/environments/production.rb

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger = ActiveSupport::Logger.new(STDOUT)
    config.logger = ActiveSupport::TaggedLogging.new(logger)
  
    config.lograge.enabled = true
    config.lograge.formatter = Lograge::Formatters::Json.new
    
    config.lograge.custom_options = lambda do |event|
      exceptions = %w(controller action format id)
      {
        method: event.payload[:method],
        path: event.payload[:path],
        controller: event.payload[:controller],
        action: event.payload[:action],
        status: event.payload[:status],
        duration: event.duration.round(2),
        view: event.payload[:view].to_f.round(2),
        db: event.payload[:db].to_f.round(2),
        params: event.payload[:params].except(*exceptions),
        remote_ip: event.payload[:remote_ip],
        uid: event.payload[:uid],
        request_headers: event.payload[:request_headers],
        time: event.time
      }
    end
  end

Result: 截圖 2024-09-19 晚上9 18 58 截圖 2024-09-19 晚上9 19 34

Thank you for taking the time to read this, much appreciated

EasonKaku avatar Sep 19 '24 13:09 EasonKaku