Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
EasonKaku opened this issue Sep 19, 2024 · 0 comments

Comments

@EasonKaku
Copy link

EasonKaku commented Sep 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant