From e0bf2bd9d420067e77dc4d9fee04888921692c8f Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 1 Oct 2024 16:53:27 -0700 Subject: [PATCH] fix: Lograge issue on non api pages (#10193) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR addresses several issues related to logging: - Enabling Lograge broke certain non-API URLs, such as password reset. This occurred due to the user ID tagging we had in Lograge, which has now been limited to API pages only. - Disabled the start and done logs in Sidekiq. - Investigated why Sidekiq logs weren’t being output as JSON. This is due to the use of ActiveJob instead of Sidekiq for the job base classes. **Potential Options for Converting ActiveJob Logs to JSON:** - https://glozer.rocks/ojb - https://learnedreverie.medium.com/activejob-logs-as-json-6912403d8c81 - https://github.com/roidrage/lograge/pull/226 --- config/initializers/lograge.rb | 5 +++-- config/initializers/sidekiq.rb | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb index 6d9ad831c..d0f8ac335 100644 --- a/config/initializers/lograge.rb +++ b/config/initializers/lograge.rb @@ -5,8 +5,9 @@ if ActiveModel::Type::Boolean.new.cast(ENV.fetch('LOGRAGE_ENABLED', false)).pres config.lograge.enabled = true config.lograge.formatter = Lograge::Formatters::Json.new config.lograge.custom_payload do |controller| - # Fixes https://github.com/chatwoot/chatwoot/issues/6922 - user_id = controller.try(:current_user).try(:id) unless controller.is_a?(SuperAdmin::Devise::SessionsController) + # We only need user_id for API requests + # might error out for other controller - ref: https://github.com/chatwoot/chatwoot/issues/6922 + user_id = controller&.try(:current_user)&.id if controller.is_a?(Api::BaseController) && controller&.try(:current_user).is_a?(User) { host: controller.request.host, remote_ip: controller.request.remote_ip, diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 610917379..4c86c2a31 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -9,6 +9,8 @@ end Sidekiq.configure_server do |config| config.logger.formatter = Sidekiq::Logger::Formatters::JSON.new config.redis = Redis::Config.app + # skip the default start stop logging + config[:skip_default_job_logging] = true config.logger.level = Logger.const_get(ENV.fetch('LOG_LEVEL', 'info').upcase.to_s) end