From 5e54992987d72df3d31921d674c43bfb58143780 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Mon, 12 Sep 2022 16:14:56 +0530 Subject: [PATCH 1/9] feat: add lograge to improve logging --- Gemfile | 2 ++ Gemfile.lock | 8 ++++++++ config/environments/development.rb | 10 +++++++++- config/environments/production.rb | 9 +++++++++ config/environments/staging.rb | 9 +++++++++ config/environments/test.rb | 9 +++++++++ 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index ca6c7856dc564..bb31ea63a322f 100644 --- a/Gemfile +++ b/Gemfile @@ -185,3 +185,5 @@ group :development, :test do gem 'spring' gem 'spring-watcher-listen' end + +gem "lograge", "~> 0.12.0" diff --git a/Gemfile.lock b/Gemfile.lock index 8cf640103d015..7255ae4c12915 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -398,6 +398,11 @@ GEM llhttp-ffi (0.4.0) ffi-compiler (~> 1.0) rake (~> 13.0) + lograge (0.12.0) + actionpack (>= 4) + activesupport (>= 4) + railties (>= 4) + request_store (~> 1.0) loofah (2.18.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -509,6 +514,8 @@ GEM declarative (< 0.1.0) trailblazer-option (>= 0.1.1, < 0.2.0) uber (< 0.2.0) + request_store (1.5.1) + rack (>= 1.4) responders (3.0.1) actionpack (>= 5.0) railties (>= 5.0) @@ -751,6 +758,7 @@ DEPENDENCIES line-bot-api liquid listen + lograge (~> 0.12.0) maxminddb mock_redis newrelic_rpm diff --git a/config/environments/development.rb b/config/environments/development.rb index df991758b093a..cc7fb74a8cbcd 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -76,5 +76,13 @@ Bullet.bullet_logger = true Bullet.rails_logger = true end - + + config.lograge.enabled = true + config.lograge.custom_payload do |controller| + { + host: controller.request.host, + user_id: controller.current_user.try(:id) + } + end + end diff --git a/config/environments/production.rb b/config/environments/production.rb index a9504fc5cc400..724f698285e10 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -103,4 +103,13 @@ config.action_mailbox.ingress = ENV.fetch('RAILS_INBOUND_EMAIL_SERVICE', 'relay').to_sym Rails.application.routes.default_url_options = { host: ENV['FRONTEND_URL'] } + + config.lograge.enabled = true + config.lograge.custom_payload do |controller| + { + host: controller.request.host, + user_id: controller.current_user.try(:id) + } + end + end diff --git a/config/environments/staging.rb b/config/environments/staging.rb index ed7cc0e842cd7..09cdaacaa21b1 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -75,4 +75,13 @@ # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + config.lograge.enabled = true + config.lograge.custom_payload do |controller| + { + host: controller.request.host, + user_id: controller.current_user.try(:id) + } + end + end diff --git a/config/environments/test.rb b/config/environments/test.rb index 4d9e77c8ed44e..51cd9be2b76ae 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -51,4 +51,13 @@ # Raises error for missing translations. # config.action_view.raise_on_missing_translations = true config.log_level = ENV.fetch('LOG_LEVEL', 'debug').to_sym + + config.lograge.enabled = true + config.lograge.custom_payload do |controller| + { + host: controller.request.host, + user_id: controller.current_user.try(:id) + } + end + end From 904cfbc3163c87aeff7c3f4a3a1b3cc07dc3b243 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Mon, 12 Sep 2022 19:15:56 +0530 Subject: [PATCH 2/9] chore: remove rails default log --- config/environments/development.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index cc7fb74a8cbcd..b80cc64248379 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -84,5 +84,5 @@ user_id: controller.current_user.try(:id) } end - + config.lograge.keep_original_rails_log = false end From 1ba21997128cb1e87eda7a883a56bfe9f7635bb9 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Wed, 14 Sep 2022 21:17:00 +0530 Subject: [PATCH 3/9] feat: configure lograge --- app/controllers/application_controller.rb | 13 +++++++++++ config/environments/development.rb | 8 ------- config/environments/production.rb | 8 ------- config/environments/staging.rb | 8 ------- config/environments/test.rb | 8 ------- config/initializers/lograge.rb | 28 +++++++++++++++++++++++ config/initializers/sidekiq.rb | 1 + 7 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 config/initializers/lograge.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d2960a6990eae..8bab35591b541 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -24,4 +24,17 @@ def pundit_user account_user: Current.account_user } end + + def append_info_to_payload(payload) + super + case + when payload[:status] == 200 + payload[:level] = "INFO" + when payload[:status] == 302 + payload[:level] = "WARN" + else + payload[:level] = "ERROR" + end + end + end diff --git a/config/environments/development.rb b/config/environments/development.rb index b80cc64248379..6e034755e1eb9 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -77,12 +77,4 @@ Bullet.rails_logger = true end - config.lograge.enabled = true - config.lograge.custom_payload do |controller| - { - host: controller.request.host, - user_id: controller.current_user.try(:id) - } - end - config.lograge.keep_original_rails_log = false end diff --git a/config/environments/production.rb b/config/environments/production.rb index 724f698285e10..f1bcd2a0b38c4 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -104,12 +104,4 @@ Rails.application.routes.default_url_options = { host: ENV['FRONTEND_URL'] } - config.lograge.enabled = true - config.lograge.custom_payload do |controller| - { - host: controller.request.host, - user_id: controller.current_user.try(:id) - } - end - end diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 09cdaacaa21b1..4b7b71ae0061c 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -76,12 +76,4 @@ # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false - config.lograge.enabled = true - config.lograge.custom_payload do |controller| - { - host: controller.request.host, - user_id: controller.current_user.try(:id) - } - end - end diff --git a/config/environments/test.rb b/config/environments/test.rb index 51cd9be2b76ae..ae375d80d342e 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -52,12 +52,4 @@ # config.action_view.raise_on_missing_translations = true config.log_level = ENV.fetch('LOG_LEVEL', 'debug').to_sym - config.lograge.enabled = true - config.lograge.custom_payload do |controller| - { - host: controller.request.host, - user_id: controller.current_user.try(:id) - } - end - end diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb new file mode 100644 index 0000000000000..bf0a44922dbd1 --- /dev/null +++ b/config/initializers/lograge.rb @@ -0,0 +1,28 @@ +Rails.application.configure do + + config.lograge.enabled = true + config.lograge.keep_original_rails_log = true + config.lograge.formatter = Lograge::Formatters::Json.new + config.colorize_logging = false + config.lograge.custom_payload do |controller| + { + host: controller.request.host, + user_id: controller.current_user.try(:id) + } + end + + config.lograge.custom_options = lambda do |event| + { + :level => event.payload[:level] + } + end + + config.lograge.ignore_custom = lambda do |event| + #ignore update_presence events in log + if event.payload[:channel_class] == "RoomChannel" + return true + end + end + +end + diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 66262ea430aa8..1acd827186fa7 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -7,6 +7,7 @@ end Sidekiq.configure_server do |config| + config.logger.formatter = Sidekiq::Logger::Formatters::JSON.new config.redis = Redis::Config.app config.logger.level = Logger.const_get(ENV.fetch('LOG_LEVEL', 'info').upcase.to_s) end From 3a04a8af36657b3a328c2a39518d3591bb6782d7 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Wed, 14 Sep 2022 21:18:26 +0530 Subject: [PATCH 4/9] feat: configure lograge --- Gemfile | 2 +- app/controllers/application_controller.rb | 17 ++++++++--------- config/initializers/lograge.rb | 15 +++++---------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Gemfile b/Gemfile index bb31ea63a322f..06d4da43c8119 100644 --- a/Gemfile +++ b/Gemfile @@ -186,4 +186,4 @@ group :development, :test do gem 'spring-watcher-listen' end -gem "lograge", "~> 0.12.0" +gem 'lograge', '~> 0.12.0' diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8bab35591b541..63416e0ca9467 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -27,14 +27,13 @@ def pundit_user def append_info_to_payload(payload) super - case - when payload[:status] == 200 - payload[:level] = "INFO" - when payload[:status] == 302 - payload[:level] = "WARN" - else - payload[:level] = "ERROR" - end + payload[:level] = case payload[:status] + when 200 + 'INFO' + when 302 + 'WARN' + else + 'ERROR' + end end - end diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb index bf0a44922dbd1..94b03595a4b23 100644 --- a/config/initializers/lograge.rb +++ b/config/initializers/lograge.rb @@ -1,8 +1,7 @@ Rails.application.configure do - config.lograge.enabled = true config.lograge.keep_original_rails_log = true - config.lograge.formatter = Lograge::Formatters::Json.new + config.lograge.formatter = Lograge::Formatters::Json.new config.colorize_logging = false config.lograge.custom_payload do |controller| { @@ -12,17 +11,13 @@ end config.lograge.custom_options = lambda do |event| - { - :level => event.payload[:level] + { + level: event.payload[:level] } end config.lograge.ignore_custom = lambda do |event| - #ignore update_presence events in log - if event.payload[:channel_class] == "RoomChannel" - return true - end + # ignore update_presence events in log + return true if event.payload[:channel_class] == 'RoomChannel' end - end - From c0857f329ef845e3fdfea5805837b18663ed1d75 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Thu, 15 Sep 2022 16:47:50 +0530 Subject: [PATCH 5/9] chore: add remote_ip to lograge --- config/initializers/lograge.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb index 94b03595a4b23..4afad22ad90c9 100644 --- a/config/initializers/lograge.rb +++ b/config/initializers/lograge.rb @@ -6,6 +6,7 @@ config.lograge.custom_payload do |controller| { host: controller.request.host, + remote_ip: controller.request.remote_ip, user_id: controller.current_user.try(:id) } end From c741c4cead8c5bfce7f6871ab5e3542513e28326 Mon Sep 17 00:00:00 2001 From: Sojan Date: Fri, 7 Apr 2023 11:23:05 +0530 Subject: [PATCH 6/9] chore: remove whitespace --- Gemfile | 2 +- config/environments/development.rb | 1 - config/environments/production.rb | 1 - config/environments/test.rb | 1 - 4 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 4740b768fd46d..98e7cd7e8bb4c 100644 --- a/Gemfile +++ b/Gemfile @@ -220,4 +220,4 @@ group :development, :test do gem 'simplecov', '0.17.1', require: false gem 'spring' gem 'spring-watcher-listen' -end \ No newline at end of file +end diff --git a/config/environments/development.rb b/config/environments/development.rb index 6e034755e1eb9..55700006503cf 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -76,5 +76,4 @@ Bullet.bullet_logger = true Bullet.rails_logger = true end - end diff --git a/config/environments/production.rb b/config/environments/production.rb index f1bcd2a0b38c4..a9504fc5cc400 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -103,5 +103,4 @@ config.action_mailbox.ingress = ENV.fetch('RAILS_INBOUND_EMAIL_SERVICE', 'relay').to_sym Rails.application.routes.default_url_options = { host: ENV['FRONTEND_URL'] } - end diff --git a/config/environments/test.rb b/config/environments/test.rb index ae375d80d342e..4d9e77c8ed44e 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -51,5 +51,4 @@ # Raises error for missing translations. # config.action_view.raise_on_missing_translations = true config.log_level = ENV.fetch('LOG_LEVEL', 'debug').to_sym - end From 6ec24bf1317f7d8a4d5b09aa48af447d8913997a Mon Sep 17 00:00:00 2001 From: Sojan Date: Fri, 7 Apr 2023 13:14:57 +0530 Subject: [PATCH 7/9] chore: refactor --- .env.example | 2 ++ Gemfile | 3 ++- config/initializers/lograge.rb | 44 ++++++++++++++++++---------------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/.env.example b/.env.example index ac4a2c0e8e841..f098e1a5b75a5 100644 --- a/.env.example +++ b/.env.example @@ -110,6 +110,8 @@ AWS_REGION= RAILS_LOG_TO_STDOUT=true LOG_LEVEL=info LOG_SIZE=500 +# Configure this environment variable if you want to use lograge instead of rails logger +#LOGRAGE_ENABLED=true ### This environment variables are only required if you are setting up social media channels diff --git a/Gemfile b/Gemfile index 98e7cd7e8bb4c..c6cadd26a03dd 100644 --- a/Gemfile +++ b/Gemfile @@ -149,7 +149,8 @@ gem 'net-imap', require: false gem 'net-pop', require: false gem 'net-smtp', require: false -gem 'lograge', '~> 0.12.0' +# Include logrange conditionally in intializer using env variable +gem 'lograge', '~> 0.12.0', require: false # worked with microsoft refresh token gem 'omniauth-oauth2' diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb index 4afad22ad90c9..98e77622e2874 100644 --- a/config/initializers/lograge.rb +++ b/config/initializers/lograge.rb @@ -1,24 +1,28 @@ -Rails.application.configure do - config.lograge.enabled = true - config.lograge.keep_original_rails_log = true - config.lograge.formatter = Lograge::Formatters::Json.new - config.colorize_logging = false - config.lograge.custom_payload do |controller| - { - host: controller.request.host, - remote_ip: controller.request.remote_ip, - user_id: controller.current_user.try(:id) - } - end +if ActiveModel::Type::Boolean.new.cast(ENV.fetch('LOGRAGE_ENABLED', false)).present? + require 'lograge' - config.lograge.custom_options = lambda do |event| - { - level: event.payload[:level] - } - end + Rails.application.configure do + config.lograge.enabled = true + config.lograge.formatter = Lograge::Formatters::Json.new + + config.lograge.custom_payload do |controller| + { + host: controller.request.host, + remote_ip: controller.request.remote_ip, + user_id: controller.current_user.try(:id) + } + end + + config.lograge.custom_options = lambda do |event| + param_exceptions = %w[controller action format id] + { + params: event.payload[:params]&.except(*param_exceptions) + } + end - config.lograge.ignore_custom = lambda do |event| - # ignore update_presence events in log - return true if event.payload[:channel_class] == 'RoomChannel' + config.lograge.ignore_custom = lambda do |event| + # ignore update_presence events in log + return true if event.payload[:channel_class] == 'RoomChannel' + end end end From ba6e6903879b42bdc4f40e9fd4d24ea36a7172f4 Mon Sep 17 00:00:00 2001 From: Sojan Date: Fri, 7 Apr 2023 13:19:50 +0530 Subject: [PATCH 8/9] chore: beautify --- app/controllers/application_controller.rb | 12 ------------ config/environments/staging.rb | 1 - 2 files changed, 13 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 63416e0ca9467..d2960a6990eae 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -24,16 +24,4 @@ def pundit_user account_user: Current.account_user } end - - def append_info_to_payload(payload) - super - payload[:level] = case payload[:status] - when 200 - 'INFO' - when 302 - 'WARN' - else - 'ERROR' - end - end end diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 4b7b71ae0061c..ed7cc0e842cd7 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -75,5 +75,4 @@ # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false - end From a10838813a8def8046552569b5c6a58b5d9da1a9 Mon Sep 17 00:00:00 2001 From: Sojan Date: Fri, 7 Apr 2023 13:24:33 +0530 Subject: [PATCH 9/9] chore: fix rubocop --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index c6cadd26a03dd..f7eaba34a3c6c 100644 --- a/Gemfile +++ b/Gemfile @@ -150,7 +150,7 @@ gem 'net-pop', require: false gem 'net-smtp', require: false # Include logrange conditionally in intializer using env variable -gem 'lograge', '~> 0.12.0', require: false +gem 'lograge', '~> 0.12.0', require: false # worked with microsoft refresh token gem 'omniauth-oauth2'