From 59f8d15baf16f66c6980c065de66136faeac5c0f Mon Sep 17 00:00:00 2001 From: Clara Morgeneyer Date: Tue, 1 Aug 2023 11:12:35 -0700 Subject: [PATCH 01/25] Add tests for enqueue event with exception and abort --- test/active_job_test.rb | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/active_job_test.rb b/test/active_job_test.rb index 3f842bb..0547f05 100644 --- a/test/active_job_test.rb +++ b/test/active_job_test.rb @@ -121,6 +121,74 @@ def id end end + describe "#enqueue with exception object" do + let(:event_name) { "enqueue.active_job" } + + let(:payload) do + { + adapter: ActiveJob::QueueAdapters::InlineAdapter.new, + job: job, + exception_object: ArgumentError.new("error") + } + end + + it "logs message" do + messages = semantic_logger_events do + subscriber.enqueue(event) + end + assert_equal 1, messages.count, messages + + assert_semantic_logger_event( + messages[0], + level: :error, + name: "Rails", + message_includes: "Error enqueuing ActiveJobTest::MyJob", + payload_includes: { + job_class: "ActiveJobTest::MyJob", + queue: "my_jobs", + event_name: "enqueue.active_job" + } + ) + assert_includes messages[0].payload, :job_id + + exception = messages[0].exception + assert exception.is_a?(ArgumentError) + assert_equal "error", exception.message + end + end + + describe "#enqueue with throwing :abort" do + let(:event_name) { "enqueue.active_job" } + + let(:payload) do + { + adapter: ActiveJob::QueueAdapters::InlineAdapter.new, + job: job, + aborted: true + } + end + + it "logs message" do + messages = semantic_logger_events do + subscriber.enqueue(event) + end + assert_equal 1, messages.count, messages + + assert_semantic_logger_event( + messages[0], + level: :error, + name: "Rails", + message_includes: "Failed enqueuing ActiveJobTest::MyJob, a before_enqueue callback halted the enqueuing execution.", + payload_includes: { + job_class: "ActiveJobTest::MyJob", + queue: "my_jobs", + event_name: "enqueue.active_job" + } + ) + assert_includes messages[0].payload, :job_id + end + end + describe "ActiveJob::Logging::LogSubscriber::EventFormatter" do let(:formatter) do RailsSemanticLogger::ActiveJob::LogSubscriber::EventFormatter.new(event: event, log_duration: true) From ea9d8d624852b9f306cfcbf03d037199d976156a Mon Sep 17 00:00:00 2001 From: Clara Morgeneyer Date: Tue, 1 Aug 2023 11:23:59 -0700 Subject: [PATCH 02/25] Extend #enqueue in ActvieJobSubscriber to handle errors and aborts --- .../active_job/log_subscriber.rb | 19 +++++++++++++++++-- test/active_job_test.rb | 8 +++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/rails_semantic_logger/active_job/log_subscriber.rb b/lib/rails_semantic_logger/active_job/log_subscriber.rb index c93d334..79a0aae 100644 --- a/lib/rails_semantic_logger/active_job/log_subscriber.rb +++ b/lib/rails_semantic_logger/active_job/log_subscriber.rb @@ -4,8 +4,23 @@ module RailsSemanticLogger module ActiveJob class LogSubscriber < ::ActiveSupport::LogSubscriber def enqueue(event) - log_with_formatter event: event do |fmt| - {message: "Enqueued #{fmt.job_info}"} + ex = event.payload[:exception_object] + + if ex + log_with_formatter level: :error, event: event do |fmt| + { + message: "Failed enqueuing #{fmt.job_info} (#{ex.class} (#{ex.message})", + exception: ex + } + end + elsif event.payload[:aborted] + log_with_formatter level: :info, event: event do |fmt| + { message: "Failed enqueuing #{fmt.job_info}, a before_enqueue callback halted the enqueuing execution." } + end + else + log_with_formatter event: event do |fmt| + { message: "Enqueued #{fmt.job_info}" } + end end end diff --git a/test/active_job_test.rb b/test/active_job_test.rb index 0547f05..7ea0fcf 100644 --- a/test/active_job_test.rb +++ b/test/active_job_test.rb @@ -1,4 +1,5 @@ require_relative "test_helper" +require 'pry' class ActiveJobTest < Minitest::Test if defined?(ActiveJob) @@ -142,7 +143,7 @@ def id messages[0], level: :error, name: "Rails", - message_includes: "Error enqueuing ActiveJobTest::MyJob", + message_includes: "Failed enqueuing ActiveJobTest::MyJob", payload_includes: { job_class: "ActiveJobTest::MyJob", queue: "my_jobs", @@ -176,15 +177,16 @@ def id assert_semantic_logger_event( messages[0], - level: :error, + level: :info, name: "Rails", - message_includes: "Failed enqueuing ActiveJobTest::MyJob, a before_enqueue callback halted the enqueuing execution.", + message_includes: "Failed enqueuing ActiveJobTest::MyJob", payload_includes: { job_class: "ActiveJobTest::MyJob", queue: "my_jobs", event_name: "enqueue.active_job" } ) + assert_match /Failed enqueuing .*, a before_enqueue callback halted the enqueuing execution/, messages[0].message assert_includes messages[0].payload, :job_id end end From eb6446748da8cbcb08e114b2c9fc85386d608855 Mon Sep 17 00:00:00 2001 From: Clara Morgeneyer Date: Tue, 1 Aug 2023 11:31:14 -0700 Subject: [PATCH 03/25] Add error and abort handling messages for #enqueue_at + tests --- .../active_job/log_subscriber.rb | 19 ++++- test/active_job_test.rb | 70 +++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/lib/rails_semantic_logger/active_job/log_subscriber.rb b/lib/rails_semantic_logger/active_job/log_subscriber.rb index 79a0aae..5a8ac3a 100644 --- a/lib/rails_semantic_logger/active_job/log_subscriber.rb +++ b/lib/rails_semantic_logger/active_job/log_subscriber.rb @@ -25,8 +25,23 @@ def enqueue(event) end def enqueue_at(event) - log_with_formatter event: event do |fmt| - {message: "Enqueued #{fmt.job_info} at #{fmt.scheduled_at}"} + ex = event.payload[:exception_object] + + if ex + log_with_formatter level: :error, event: event do |fmt| + { + message: "Failed enqueuing #{fmt.job_info} (#{ex.class} (#{ex.message})", + exception: ex + } + end + elsif event.payload[:aborted] + log_with_formatter level: :info, event: event do |fmt| + { message: "Failed enqueuing #{fmt.job_info}, a before_enqueue callback halted the enqueuing execution." } + end + else + log_with_formatter event: event do |fmt| + {message: "Enqueued #{fmt.job_info} at #{fmt.scheduled_at}"} + end end end diff --git a/test/active_job_test.rb b/test/active_job_test.rb index 7ea0fcf..f3ce0e8 100644 --- a/test/active_job_test.rb +++ b/test/active_job_test.rb @@ -191,6 +191,76 @@ def id end end + describe "#enqueue_at with exception object" do + let(:event_name) { "enqueue.active_job" } + + let(:payload) do + { + adapter: ActiveJob::QueueAdapters::InlineAdapter.new, + job: job, + exception_object: ArgumentError.new("error") + } + end + + it "logs message" do + messages = semantic_logger_events do + subscriber.enqueue_at(event) + end + assert_equal 1, messages.count, messages + + assert_semantic_logger_event( + messages[0], + level: :error, + name: "Rails", + message_includes: "Failed enqueuing ActiveJobTest::MyJob", + payload_includes: { + job_class: "ActiveJobTest::MyJob", + queue: "my_jobs", + event_name: "enqueue.active_job" + } + ) + assert_includes messages[0].payload, :job_id + + exception = messages[0].exception + assert exception.is_a?(ArgumentError) + assert_equal "error", exception.message + end + end + + describe "#enqueue_at with throwing :abort" do + let(:event_name) { "enqueue.active_job" } + + let(:payload) do + { + adapter: ActiveJob::QueueAdapters::InlineAdapter.new, + job: job, + aborted: true + } + end + + it "logs message" do + messages = semantic_logger_events do + subscriber.enqueue_at(event) + end + assert_equal 1, messages.count, messages + + assert_semantic_logger_event( + messages[0], + level: :info, + name: "Rails", + message_includes: "Failed enqueuing ActiveJobTest::MyJob", + payload_includes: { + job_class: "ActiveJobTest::MyJob", + queue: "my_jobs", + event_name: "enqueue.active_job" + } + ) + assert_match /Failed enqueuing .*, a before_enqueue callback halted the enqueuing execution/, messages[0].message + assert_includes messages[0].payload, :job_id + end + end + + describe "ActiveJob::Logging::LogSubscriber::EventFormatter" do let(:formatter) do RailsSemanticLogger::ActiveJob::LogSubscriber::EventFormatter.new(event: event, log_duration: true) From f8ce899de4e1b1b2738429752f16094ffcc9dca2 Mon Sep 17 00:00:00 2001 From: Jens Norrgrann Date: Fri, 18 Aug 2023 10:11:29 +0200 Subject: [PATCH 04/25] Disable Bugsnag api key validation https://github.com/bugsnag/bugsnag-ruby/blob/bdfbf3972f2137b3b9b95194a6bedf9c8edf6555/lib/bugsnag.rb#L61 This will remove the warning "No valid API key has been set, notifications will not be sent" that will be logged in all environments as the api key hasn't been set yet as that happens later in the initialization process. --- lib/rails_semantic_logger/engine.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rails_semantic_logger/engine.rb b/lib/rails_semantic_logger/engine.rb index 42589af..ce85d7e 100644 --- a/lib/rails_semantic_logger/engine.rb +++ b/lib/rails_semantic_logger/engine.rb @@ -127,7 +127,7 @@ class Engine < ::Rails::Engine end # Replace the Bugsnag logger - Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag) + Bugsnag.configure(false) { |config| config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag) # Set the IOStreams PGP logger IOStreams::Pgp.logger = SemanticLogger["IOStreams::Pgp"] if defined?(IOStreams) @@ -138,7 +138,7 @@ class Engine < ::Rails::Engine config = Rails.application.config # Replace the Bugsnag logger - Bugsnag.configure { |bugsnag_config| bugsnag_config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag) + Bugsnag.configure(false) { |bugsnag_config| bugsnag_config.logger = SemanticLogger[Bugsnag] } if defined?(Bugsnag) # Rails Patches require("rails_semantic_logger/extensions/action_cable/tagged_logger_proxy") if defined?(::ActionCable) From dc2d20f3e4578447f4242a31ead4e611ff3f65d3 Mon Sep 17 00:00:00 2001 From: Jonas Schwertfeger Date: Fri, 8 Sep 2023 17:22:06 +0200 Subject: [PATCH 05/25] fix: handle nil logger in `ActiveJob::Logging` --- lib/rails_semantic_logger/extensions/active_job/logging.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/rails_semantic_logger/extensions/active_job/logging.rb b/lib/rails_semantic_logger/extensions/active_job/logging.rb index a811a6e..b634223 100644 --- a/lib/rails_semantic_logger/extensions/active_job/logging.rb +++ b/lib/rails_semantic_logger/extensions/active_job/logging.rb @@ -9,7 +9,11 @@ module Logging undef_method :tag_logger def tag_logger(*tags, &block) - logger.tagged(*tags, &block) + if logger.respond_to?(:tagged) + logger.tagged(*tags, &block) + else + yield + end end end end From 75ddc1607596f36c786fe863ccbd26b1a373969f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Schr=C3=B6der?= Date: Tue, 26 Sep 2023 12:25:26 +0200 Subject: [PATCH 06/25] fix url --- rails_semantic_logger.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rails_semantic_logger.gemspec b/rails_semantic_logger.gemspec index ae38f48..2dcea7b 100644 --- a/rails_semantic_logger.gemspec +++ b/rails_semantic_logger.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |s| s.metadata = { "bug_tracker_uri" => "https://github.com/reidmorrison/rails_semantic_logger/issues", "documentation_uri" => "https://logger.rocketjob.io", - "source_code_uri" => "https://github.com/reidmorrison/rails_semantic_logger/tree/#{RailsSemanticLogger::VERSION}", + "source_code_uri" => "https://github.com/reidmorrison/rails_semantic_logger/tree/v#{RailsSemanticLogger::VERSION}", "rubygems_mfa_required" => "true" } end From d7ba5ad8f48c7ad36538494d0acefdefa58ba011 Mon Sep 17 00:00:00 2001 From: Clara Morgeneyer Date: Thu, 28 Sep 2023 10:55:23 -0700 Subject: [PATCH 07/25] Remove pry import --- test/active_job_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/active_job_test.rb b/test/active_job_test.rb index f3ce0e8..317a13a 100644 --- a/test/active_job_test.rb +++ b/test/active_job_test.rb @@ -1,5 +1,4 @@ require_relative "test_helper" -require 'pry' class ActiveJobTest < Minitest::Test if defined?(ActiveJob) From 2473acbf10a5e3a2d53c940715b9e2215f4ec179 Mon Sep 17 00:00:00 2001 From: Amnesthesia Date: Wed, 4 Oct 2023 12:24:34 +1000 Subject: [PATCH 08/25] fix: remove undef :broadcast since ActiveSupport::Logger dropped it --- lib/rails_semantic_logger/extensions/active_support/logger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rails_semantic_logger/extensions/active_support/logger.rb b/lib/rails_semantic_logger/extensions/active_support/logger.rb index b0aa73e..23c4339 100644 --- a/lib/rails_semantic_logger/extensions/active_support/logger.rb +++ b/lib/rails_semantic_logger/extensions/active_support/logger.rb @@ -4,7 +4,7 @@ module ActiveSupport # More hacks to try and stop Rails from being it's own worst enemy. class Logger class << self - undef :logger_outputs_to?, :broadcast + undef :logger_outputs_to? end # Prevent Console from trying to merge loggers From 3f0b0a0075a82786b4f0cd6ef4bbbda36726b31b Mon Sep 17 00:00:00 2001 From: Amnesthesia Date: Wed, 11 Oct 2023 07:52:50 +1000 Subject: [PATCH 09/25] fix: @camsteffen's comment --- .../extensions/active_support/logger.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/rails_semantic_logger/extensions/active_support/logger.rb b/lib/rails_semantic_logger/extensions/active_support/logger.rb index 23c4339..e0b705a 100644 --- a/lib/rails_semantic_logger/extensions/active_support/logger.rb +++ b/lib/rails_semantic_logger/extensions/active_support/logger.rb @@ -5,6 +5,14 @@ module ActiveSupport class Logger class << self undef :logger_outputs_to? + + # Prevent broadcasting since SemanticLogger already supports multiple loggers + if defined(:broadcast) + undef :broadcast + def broadcast(logger) + Module.new + end + end end # Prevent Console from trying to merge loggers @@ -12,7 +20,6 @@ def self.logger_outputs_to?(*args) true end - # Prevent broadcasting since SemanticLogger already supports multiple loggers def self.broadcast(logger) Module.new end From 85fdedba43ef722b82bb09e7270e970ed8e7a5d1 Mon Sep 17 00:00:00 2001 From: Amnesthesia Date: Wed, 11 Oct 2023 07:54:57 +1000 Subject: [PATCH 10/25] fix: add rails 7.1 specs --- .github/workflows/ci.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4d17062..fad66a6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,7 +41,15 @@ jobs: - rails: "7.0" ruby: 3.2 - rails: "7.0" - ruby: jruby + ruby: jruby + - rails: "7.1" + ruby: "3.0" + - rails: "7.1" + ruby: 3.1 + - rails: "7.1" + ruby: 3.2 + - rails: "7.1" + ruby: jruby env: BUNDLE_GEMFILE: gemfiles/rails_${{ matrix.rails }}.gemfile From e9f816425f93cfca8829018e40fb473f9eb56adb Mon Sep 17 00:00:00 2001 From: Amnesthesia Date: Mon, 16 Oct 2023 11:26:31 +1000 Subject: [PATCH 11/25] fix: typo in defined? --- lib/rails_semantic_logger/extensions/active_support/logger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rails_semantic_logger/extensions/active_support/logger.rb b/lib/rails_semantic_logger/extensions/active_support/logger.rb index e0b705a..5b23dc5 100644 --- a/lib/rails_semantic_logger/extensions/active_support/logger.rb +++ b/lib/rails_semantic_logger/extensions/active_support/logger.rb @@ -7,7 +7,7 @@ class << self undef :logger_outputs_to? # Prevent broadcasting since SemanticLogger already supports multiple loggers - if defined(:broadcast) + if method_defined?(:broadcast) undef :broadcast def broadcast(logger) Module.new From cba6f1b7f2c311ec2c7f47b1bbe4448dbd2abf86 Mon Sep 17 00:00:00 2001 From: Amnesthesia Date: Mon, 16 Oct 2023 11:27:46 +1000 Subject: [PATCH 12/25] fix: remove override when override not needed --- lib/rails_semantic_logger/extensions/active_support/logger.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/rails_semantic_logger/extensions/active_support/logger.rb b/lib/rails_semantic_logger/extensions/active_support/logger.rb index 5b23dc5..e4e8221 100644 --- a/lib/rails_semantic_logger/extensions/active_support/logger.rb +++ b/lib/rails_semantic_logger/extensions/active_support/logger.rb @@ -20,10 +20,6 @@ def self.logger_outputs_to?(*args) true end - def self.broadcast(logger) - Module.new - end - def self.new(*args, **kwargs) SemanticLogger[self] end From 89370d285f81f119504ac2695c1d68f308da4312 Mon Sep 17 00:00:00 2001 From: Naoki Hosoya Date: Mon, 6 Nov 2023 19:39:01 +0900 Subject: [PATCH 13/25] Fix: `log_arguments` of Mailer does not work properly --- lib/rails_semantic_logger/action_mailer/log_subscriber.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rails_semantic_logger/action_mailer/log_subscriber.rb b/lib/rails_semantic_logger/action_mailer/log_subscriber.rb index 423fb8b..5d21f84 100644 --- a/lib/rails_semantic_logger/action_mailer/log_subscriber.rb +++ b/lib/rails_semantic_logger/action_mailer/log_subscriber.rb @@ -92,7 +92,7 @@ def action end def formatted_args - if defined?(mailer.contantize.log_arguments?) && !mailer.contantize.log_arguments? + if defined?(mailer.constantize.log_arguments?) && !mailer.constantize.log_arguments? "" else JSON.pretty_generate(event.payload[:args].map { |arg| format(arg) }) if event.payload[:args].present? From ee5e80d95e99cf45a267a3882f5a734e8326f494 Mon Sep 17 00:00:00 2001 From: Reid Morrison Date: Wed, 8 Nov 2023 22:24:34 -0500 Subject: [PATCH 14/25] Give Rails 7.1 a try --- .github/workflows/ci.yml | 29 ++------- Appraisals | 24 ++----- Gemfile | 2 +- gemfiles/rails_5.2.gemfile | 22 ------- gemfiles/rails_6.0.gemfile | 7 +-- gemfiles/rails_6.1.gemfile | 7 +-- gemfiles/rails_7.0.gemfile | 8 +-- .../{rails_5.1.gemfile => rails_7.1.gemfile} | 11 +--- lib/rails_semantic_logger.rb | 1 + .../action_view/log_subscriber.rb | 62 ++++++++++++++++--- .../active_support/log_subscriber.rb | 11 ++++ lib/rails_semantic_logger/version.rb | 2 +- 12 files changed, 83 insertions(+), 103 deletions(-) delete mode 100644 gemfiles/rails_5.2.gemfile rename gemfiles/{rails_5.1.gemfile => rails_7.1.gemfile} (50%) create mode 100644 lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fad66a6..7294ab7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,44 +12,27 @@ jobs: fail-fast: false matrix: include: - - rails: 5.1 - ruby: 2.5 - - - rails: 5.2 - ruby: 2.5 - - rails: 5.2 - ruby: 2.6 - - rails: 5.2 - ruby: jruby-9.3 - - - rails: "6.0" - ruby: 2.6 - rails: "6.0" ruby: 2.7 - - rails: "6.0" - ruby: jruby - rails: "6.1" ruby: "2.7" - rails: "6.1" - ruby: jruby - - - rails: "7.0" ruby: "3.0" + - rails: "6.1" + ruby: "3.1" + - rails: "6.1" + ruby: "3.2" + - rails: "7.0" ruby: 3.1 - rails: "7.0" ruby: 3.2 - - rails: "7.0" - ruby: jruby - - rails: "7.1" - ruby: "3.0" + - rails: "7.1" ruby: 3.1 - rails: "7.1" ruby: 3.2 - - rails: "7.1" - ruby: jruby env: BUNDLE_GEMFILE: gemfiles/rails_${{ matrix.rails }}.gemfile diff --git a/Appraisals b/Appraisals index 75860a2..6225606 100644 --- a/Appraisals +++ b/Appraisals @@ -1,33 +1,19 @@ -appraise "rails_5.1" do - gem "rails", "~> 5.1.5" - gem "sqlite3", "~> 1.3.0", platform: :ruby - gem "activerecord-jdbcsqlite3-adapter", "~> 51.0", platform: :jruby -end - -appraise "rails_5.2" do - gem "rails", "~> 5.2.0" - gem "sqlite3", "~> 1.3.0", platform: :ruby - gem "activerecord-jdbcsqlite3-adapter", "~> 52.0", platform: :jruby -end - appraise "rails_6.0" do gem "rails", "~> 6.0.0" - gem "activerecord-jdbcsqlite3-adapter", "~> 60.0", platform: :jruby gem "sqlite3", "~> 1.4.0", platform: :ruby end appraise "rails_6.1" do gem "rails", "~> 6.1.0" - gem "activerecord-jdbcsqlite3-adapter", "~> 61.0", platform: :jruby gem "sqlite3", "~> 1.4.0", platform: :ruby end appraise "rails_7.0" do - # Remove this deprecated gem once the following patch is released - # https://github.com/rails-api/active_model_serializers/pull/2428 - gem "thread_safe", "~> 0.3.6" - gem "rails", "~> 7.0.0" - gem "activerecord-jdbcsqlite3-adapter", "~> 70.0", platform: :jruby + gem "sqlite3", "~> 1.4.0", platform: :ruby +end + +appraise "rails_7.1" do + gem "rails", "~> 7.1.0" gem "sqlite3", "~> 1.4.0", platform: :ruby end diff --git a/Gemfile b/Gemfile index b72a386..054e2bf 100644 --- a/Gemfile +++ b/Gemfile @@ -12,7 +12,7 @@ gem "minitest-rails" gem "rake" gem "sprockets", "< 4.0" -gem "rails", "~> 7.0.0" +gem "rails", "~> 7.1.0" gem "sqlite3", "~> 1.4.0", platform: :ruby gem "rubocop" diff --git a/gemfiles/rails_5.2.gemfile b/gemfiles/rails_5.2.gemfile deleted file mode 100644 index 7cdf14a..0000000 --- a/gemfiles/rails_5.2.gemfile +++ /dev/null @@ -1,22 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "appraisal" -gem "puma" -gem "active_model_serializers" -gem "amazing_print" -gem "minitest" -gem "minitest-rails" -gem "rake" -gem "sprockets", "< 4.0" -gem "semantic_logger", github: "reidmorrison/semantic_logger" -gem "rails", "~> 5.2.0" -gem "sqlite3", "~> 1.3.0", platform: :ruby -gem "activerecord-jdbcsqlite3-adapter", "~> 52.0", platform: :jruby - -group :development do - gem "rubocop" -end - -gemspec path: "../" diff --git a/gemfiles/rails_6.0.gemfile b/gemfiles/rails_6.0.gemfile index 2f4d738..29fbc06 100644 --- a/gemfiles/rails_6.0.gemfile +++ b/gemfiles/rails_6.0.gemfile @@ -10,13 +10,8 @@ gem "minitest" gem "minitest-rails" gem "rake" gem "sprockets", "< 4.0" -gem "semantic_logger", github: "reidmorrison/semantic_logger" gem "rails", "~> 6.0.0" gem "sqlite3", "~> 1.4.0", platform: :ruby -gem "activerecord-jdbcsqlite3-adapter", "~> 60.0", platform: :jruby - -group :development do - gem "rubocop" -end +gem "rubocop" gemspec path: "../" diff --git a/gemfiles/rails_6.1.gemfile b/gemfiles/rails_6.1.gemfile index 37c0074..16fc598 100644 --- a/gemfiles/rails_6.1.gemfile +++ b/gemfiles/rails_6.1.gemfile @@ -10,13 +10,8 @@ gem "minitest" gem "minitest-rails" gem "rake" gem "sprockets", "< 4.0" -gem "semantic_logger", github: "reidmorrison/semantic_logger" gem "rails", "~> 6.1.0" gem "sqlite3", "~> 1.4.0", platform: :ruby -gem "activerecord-jdbcsqlite3-adapter", "~> 61.0", platform: :jruby - -group :development do - gem "rubocop" -end +gem "rubocop" gemspec path: "../" diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile index fe819a1..3d38acd 100644 --- a/gemfiles/rails_7.0.gemfile +++ b/gemfiles/rails_7.0.gemfile @@ -10,14 +10,8 @@ gem "minitest" gem "minitest-rails" gem "rake" gem "sprockets", "< 4.0" -gem "semantic_logger", github: "reidmorrison/semantic_logger" gem "rails", "~> 7.0.0" gem "sqlite3", "~> 1.4.0", platform: :ruby -gem "thread_safe", "~> 0.3.6" -gem "activerecord-jdbcsqlite3-adapter", "~> 70.0", platform: :jruby - -group :development do - gem "rubocop" -end +gem "rubocop" gemspec path: "../" diff --git a/gemfiles/rails_5.1.gemfile b/gemfiles/rails_7.1.gemfile similarity index 50% rename from gemfiles/rails_5.1.gemfile rename to gemfiles/rails_7.1.gemfile index 6001b83..739d96c 100644 --- a/gemfiles/rails_5.1.gemfile +++ b/gemfiles/rails_7.1.gemfile @@ -10,13 +10,8 @@ gem "minitest" gem "minitest-rails" gem "rake" gem "sprockets", "< 4.0" -gem "semantic_logger", github: "reidmorrison/semantic_logger" -gem "rails", "~> 5.1.5" -gem "sqlite3", "~> 1.3.0", platform: :ruby -gem "activerecord-jdbcsqlite3-adapter", "~> 51.0", platform: :jruby - -group :development do - gem "rubocop" -end +gem "rails", "~> 7.1.0" +gem "sqlite3", "~> 1.4.0", platform: :ruby +gem "rubocop" gemspec path: "../" diff --git a/lib/rails_semantic_logger.rb b/lib/rails_semantic_logger.rb index e930fb2..28df0f5 100644 --- a/lib/rails_semantic_logger.rb +++ b/lib/rails_semantic_logger.rb @@ -58,4 +58,5 @@ def self.subscriber_patterns(subscriber) require("rails_semantic_logger/extensions/mongoid/config") if defined?(Mongoid) require("rails_semantic_logger/extensions/active_support/logger") if defined?(ActiveSupport::Logger) +require("rails_semantic_logger/extensions/active_support/log_subscriber") if defined?(ActiveSupport::LogSubscriber) require("rails_semantic_logger/extensions/rack/server") if defined?(Rack::Server) diff --git a/lib/rails_semantic_logger/action_view/log_subscriber.rb b/lib/rails_semantic_logger/action_view/log_subscriber.rb index 2b0ade0..0ad2d8e 100644 --- a/lib/rails_semantic_logger/action_view/log_subscriber.rb +++ b/lib/rails_semantic_logger/action_view/log_subscriber.rb @@ -19,10 +19,10 @@ def initialize def render_template(event) return unless should_log? - payload = { + payload = { template: from_rails_root(event.payload[:identifier]) } - payload[:within] = from_rails_root(event.payload[:layout]) if event.payload[:layout] + payload[:within] = from_rails_root(event.payload[:layout]) if event.payload[:layout] payload[:allocations] = event.allocations if event.respond_to?(:allocations) logger.measure( @@ -36,11 +36,11 @@ def render_template(event) def render_partial(event) return unless should_log? - payload = { + payload = { partial: from_rails_root(event.payload[:identifier]) } - payload[:within] = from_rails_root(event.payload[:layout]) if event.payload[:layout] - payload[:cache] = event.payload[:cache_hit] unless event.payload[:cache_hit].nil? + payload[:within] = from_rails_root(event.payload[:layout]) if event.payload[:layout] + payload[:cache] = event.payload[:cache_hit] unless event.payload[:cache_hit].nil? payload[:allocations] = event.allocations if event.respond_to?(:allocations) logger.measure( @@ -56,11 +56,11 @@ def render_collection(event) identifier = event.payload[:identifier] || "templates" - payload = { + payload = { template: from_rails_root(identifier), count: event.payload[:count] } - payload[:cache_hits] = event.payload[:cache_hits] if event.payload[:cache_hits] + payload[:cache_hits] = event.payload[:cache_hits] if event.payload[:cache_hits] payload[:allocations] = event.allocations if event.respond_to?(:allocations) logger.measure( @@ -72,16 +72,58 @@ def render_collection(event) end def start(name, id, payload) - if (name == "render_template.action_view") && should_log? - payload = {template: from_rails_root(payload[:identifier])} + if (name == "render_template.action_view" || name == "render_layout.action_view") && should_log? + qualifier = " layout" if name == "render_layout.action_view" + payload = { template: from_rails_root(payload[:identifier]) } payload[:within] = from_rails_root(payload[:layout]) if payload[:layout] - logger.send(self.class.rendered_log_level, message: "Rendering", payload: payload) + logger.send(self.class.rendered_log_level, message: "Rendering#{qualifier}", payload: payload) end super end + if (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 7 + class Start # :nodoc: + def start(name, id, payload) + return unless %w[render_template.action_view render_layout.action_view].include?(name) + + qualifier = " layout" if name == "render_layout.action_view" + payload = { template: from_rails_root(payload[:identifier]) } + payload[:within] = from_rails_root(payload[:layout]) if payload[:layout] + + logger.debug(message: "Rendering#{qualifier}", payload: payload) + end + + def finish(name, id, payload) end + + private + + def from_rails_root(string) + string = string.sub(rails_root, "") + string.sub!(VIEWS_PATTERN, "") + string + end + + def rails_root # :doc: + @root ||= "#{Rails.root}/" + end + + def logger + @logger ||= SemanticLogger["ActionView"] + end + end + + def self.attach_to(*) + ActiveSupport::Notifications.unsubscribe("render_template.action_view") + ActiveSupport::Notifications.unsubscribe("render_layout.action_view") + ActiveSupport::Notifications.subscribe("render_template.action_view", RailsSemanticLogger::ActionView::LogSubscriber::Start.new) + ActiveSupport::Notifications.subscribe("render_layout.action_view", RailsSemanticLogger::ActionView::LogSubscriber::Start.new) + + super + end + end + private @logger = SemanticLogger["ActionView"] diff --git a/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb b/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb new file mode 100644 index 0000000..1d3332d --- /dev/null +++ b/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb @@ -0,0 +1,11 @@ +require "active_support/log_subscriber" + +module ActiveSupport + class LogSubscriber + # @override Rails 7.1 + def silenced?(event) + native_log_level = @event_levels.fetch(event, ::Logger::Severity::FATAL) + logger.nil? || SemanticLogger::Levels.index(logger.level) > SemanticLogger::Levels.index(native_log_level) + end + end +end diff --git a/lib/rails_semantic_logger/version.rb b/lib/rails_semantic_logger/version.rb index 3ec7e6a..a58ac19 100644 --- a/lib/rails_semantic_logger/version.rb +++ b/lib/rails_semantic_logger/version.rb @@ -1,3 +1,3 @@ module RailsSemanticLogger - VERSION = "4.12.0".freeze + VERSION = "4.13.0".freeze end From 6f327368728b72c99ec0f79335af598327ce450d Mon Sep 17 00:00:00 2001 From: Reid Morrison Date: Wed, 8 Nov 2023 22:29:10 -0500 Subject: [PATCH 15/25] v4.13.0 --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7294ab7..4598834 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,8 +21,6 @@ jobs: ruby: "3.0" - rails: "6.1" ruby: "3.1" - - rails: "6.1" - ruby: "3.2" - rails: "7.0" ruby: 3.1 From a43a74e8187b9d170f80853bbe7ce467f713e4eb Mon Sep 17 00:00:00 2001 From: Nick Stanish <1869157+nickstanish@users.noreply.github.com> Date: Sat, 11 Nov 2023 16:36:15 -0600 Subject: [PATCH 16/25] fix: rack server deprecated in rack 3 --- lib/rails_semantic_logger.rb | 6 +++++- .../extensions/rackup/server.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 lib/rails_semantic_logger/extensions/rackup/server.rb diff --git a/lib/rails_semantic_logger.rb b/lib/rails_semantic_logger.rb index 28df0f5..395ea08 100644 --- a/lib/rails_semantic_logger.rb +++ b/lib/rails_semantic_logger.rb @@ -59,4 +59,8 @@ def self.subscriber_patterns(subscriber) require("rails_semantic_logger/extensions/mongoid/config") if defined?(Mongoid) require("rails_semantic_logger/extensions/active_support/logger") if defined?(ActiveSupport::Logger) require("rails_semantic_logger/extensions/active_support/log_subscriber") if defined?(ActiveSupport::LogSubscriber) -require("rails_semantic_logger/extensions/rack/server") if defined?(Rack::Server) +if defined?(Rackup::Server) + require("rails_semantic_logger/extensions/rackup/server") +elsif defined?(Rack::Server) + require("rails_semantic_logger/extensions/rack/server") +end diff --git a/lib/rails_semantic_logger/extensions/rackup/server.rb b/lib/rails_semantic_logger/extensions/rackup/server.rb new file mode 100644 index 0000000..bcffedc --- /dev/null +++ b/lib/rails_semantic_logger/extensions/rackup/server.rb @@ -0,0 +1,12 @@ +module RailsSemanticLogger + module Rackup + module Server + def daemonize_app + super + SemanticLogger.reopen + end + end + end +end + +Rackup::Server.prepend(RailsSemanticLogger::Rackup::Server) From b0df3382067fc7b0aeeb499afa6ef32d2f83b4d2 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Mon, 13 Nov 2023 12:08:44 +0100 Subject: [PATCH 17/25] Enable log subscriber silenced? patch only for 7.1.1 --- .../extensions/active_support/log_subscriber.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb b/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb index 1d3332d..ff1d942 100644 --- a/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb +++ b/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb @@ -1,11 +1,13 @@ -require "active_support/log_subscriber" +if ActiveSupport::VERSION::STRING == '7.1.1' + require "active_support/log_subscriber" -module ActiveSupport - class LogSubscriber - # @override Rails 7.1 - def silenced?(event) - native_log_level = @event_levels.fetch(event, ::Logger::Severity::FATAL) - logger.nil? || SemanticLogger::Levels.index(logger.level) > SemanticLogger::Levels.index(native_log_level) + module ActiveSupport + class LogSubscriber + # @override Rails 7.1 + def silenced?(event) + native_log_level = @event_levels.fetch(event, ::Logger::Severity::FATAL) + logger.nil? || SemanticLogger::Levels.index(logger.level) > SemanticLogger::Levels.index(native_log_level) + end end end end From b6c36b481f69bfc765fc0d135d1e64f8f8c1eebf Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Mon, 13 Nov 2023 12:10:16 +0100 Subject: [PATCH 18/25] Remove print --- test/active_record_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/active_record_test.rb b/test/active_record_test.rb index e43a491..47adc2d 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -96,7 +96,6 @@ class ActiveRecordTest < Minitest::Test Sample.where(age: 2..21).first end assert_equal 1, messages.count, messages - ap messages assert_semantic_logger_event( messages[0], From 07737071125a54a5d9699a8c3fe97e799bea909c Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Mon, 13 Nov 2023 12:16:20 +0100 Subject: [PATCH 19/25] Fix ruby 3.2.2 and Rails 6.1 compatibility in test suite --- .github/workflows/ci.yml | 2 ++ test/action_mailer_test.rb | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4598834..7294ab7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,8 @@ jobs: ruby: "3.0" - rails: "6.1" ruby: "3.1" + - rails: "6.1" + ruby: "3.2" - rails: "7.0" ruby: 3.1 diff --git a/test/action_mailer_test.rb b/test/action_mailer_test.rb index 8349947..be42aec 100644 --- a/test/action_mailer_test.rb +++ b/test/action_mailer_test.rb @@ -2,8 +2,8 @@ class ActionMailerTest < Minitest::Test class MyMailer < ActionMailer::Base - def some_email(to:, from:, subject:) - mail(to: to, from: from, subject: subject, body: "Hello") + def some_email(opts) + mail(to: opts[:to], from: opts[:from], subject: opts[:subject], body: "Hello") end end From 196316f00d4172722fdb4d8e9365f9f4ad7cb5d1 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Mon, 13 Nov 2023 12:19:46 +0100 Subject: [PATCH 20/25] Add test matrix for 7.1.1 for ActiveSupport::LogSubscriber#silenced? patch --- .github/workflows/ci.yml | 4 ++++ Appraisals | 5 +++++ gemfiles/rails_7.1.1.gemfile | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 gemfiles/rails_7.1.1.gemfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7294ab7..9f616d0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,8 +29,12 @@ jobs: - rails: "7.0" ruby: 3.2 + - rails: "7.1.1" + ruby: "3.2" - rails: "7.1" ruby: 3.1 + - rails: "7.1.1" + ruby: 3.2 - rails: "7.1" ruby: 3.2 diff --git a/Appraisals b/Appraisals index 6225606..ce550fc 100644 --- a/Appraisals +++ b/Appraisals @@ -13,6 +13,11 @@ appraise "rails_7.0" do gem "sqlite3", "~> 1.4.0", platform: :ruby end +appraise "rails_7.1.1" do + gem "rails", "7.1.1" + gem "sqlite3", "~> 1.4.0", platform: :ruby +end + appraise "rails_7.1" do gem "rails", "~> 7.1.0" gem "sqlite3", "~> 1.4.0", platform: :ruby diff --git a/gemfiles/rails_7.1.1.gemfile b/gemfiles/rails_7.1.1.gemfile new file mode 100644 index 0000000..12c543a --- /dev/null +++ b/gemfiles/rails_7.1.1.gemfile @@ -0,0 +1,17 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "appraisal" +gem "puma" +gem "active_model_serializers" +gem "amazing_print" +gem "minitest" +gem "minitest-rails" +gem "rake" +gem "sprockets", "< 4.0" +gem "rails", "7.1.1" +gem "sqlite3", "~> 1.4.0", platform: :ruby +gem "rubocop" + +gemspec path: "../" From b73f4229457e71646057fc04800e9ec4f4c9fd51 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Mon, 13 Nov 2023 13:49:37 +0100 Subject: [PATCH 21/25] Consistent matrix versions naming in ci workflow --- .github/workflows/ci.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9f616d0..f0e7e95 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: matrix: include: - rails: "6.0" - ruby: 2.7 + ruby: "2.7" - rails: "6.1" ruby: "2.7" @@ -25,18 +25,18 @@ jobs: ruby: "3.2" - rails: "7.0" - ruby: 3.1 + ruby: "3.1" - rails: "7.0" - ruby: 3.2 + ruby: "3.2" - rails: "7.1.1" ruby: "3.2" - rails: "7.1" - ruby: 3.1 - - rails: "7.1.1" - ruby: 3.2 + ruby: "3.1" + - rails: "7.1" + ruby: "3.2" - rails: "7.1" - ruby: 3.2 + ruby: "3.2" env: BUNDLE_GEMFILE: gemfiles/rails_${{ matrix.rails }}.gemfile From 93ebb0c01d4bec0a31f24338d8fc780f06c021d0 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Mon, 13 Nov 2023 13:49:52 +0100 Subject: [PATCH 22/25] Add test/dummy/db/test.sqlite3-* to gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7342460..81e76a9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.log pkg/ test/dummy/tmp/ +test/dummy/db/test.sqlite3-* test/dummy/.sass-cache *.gem /.idea @@ -12,6 +13,5 @@ Gemfile.lock *.sqlite3 .rakeTasks -*.sqlite3 TODO.md From fa03020af4ba6961e15593221ac0ddac05fbf1db Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Tue, 14 Nov 2023 23:25:02 +0100 Subject: [PATCH 23/25] Remove duplicate matrix in ci --- .github/workflows/ci.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f0e7e95..2255bcb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,11 +29,9 @@ jobs: - rails: "7.0" ruby: "3.2" - - rails: "7.1.1" - ruby: "3.2" - rails: "7.1" ruby: "3.1" - - rails: "7.1" + - rails: "7.1.1" ruby: "3.2" - rails: "7.1" ruby: "3.2" From 9e97777c04898bae4503daefa35168f2a16e96f3 Mon Sep 17 00:00:00 2001 From: Reid Morrison Date: Thu, 16 Nov 2023 13:59:17 -0500 Subject: [PATCH 24/25] RuboCop Automated Reformatting --- .rubocop.yml | 27 ++++++++-------- lib/rails_semantic_logger.rb | 12 +++++-- .../action_controller/log_subscriber.rb | 8 +++-- .../action_mailer/log_subscriber.rb | 22 ++++++------- .../action_view/log_subscriber.rb | 27 +++++++++------- .../active_job/log_subscriber.rb | 12 +++---- .../active_record/log_subscriber.rb | 4 +-- lib/rails_semantic_logger/engine.rb | 10 +++--- .../active_support/log_subscriber.rb | 2 +- .../extensions/active_support/logger.rb | 6 ++-- .../extensions/rails/server.rb | 2 +- lib/rails_semantic_logger/rack/logger.rb | 2 +- test/action_mailer_test.rb | 32 +++++++++---------- test/active_job_test.rb | 5 ++- test/active_record_test.rb | 14 ++++---- test/controllers/articles_controller_test.rb | 2 +- test/controllers/dashboard_controller_test.rb | 2 +- test/dummy/config.ru | 2 +- test/dummy/config/application.rb | 4 +-- test/test_helper.rb | 2 +- 20 files changed, 104 insertions(+), 93 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 6c7f1ee..3465c71 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,9 +1,10 @@ AllCops: - TargetRubyVersion: 2.4 Exclude: - - '.git/**/*' - - '.github/**/*' - - 'gemfiles/**/*' + - ".git/**/*" + - "docs/**/*" + - "gemfiles/*" + NewCops: enable + TargetRubyVersion: 2.5 # # RuboCop built-in settings. @@ -31,7 +32,7 @@ Layout/HashAlignment: Layout/LineLength: Max: 128 Exclude: - - '**/test/**/*' + - "**/test/**/*" # Match existing layout Layout/SpaceInsideHashLiteralBraces: @@ -44,17 +45,17 @@ Metrics/AbcSize: # Support long block lengths for tests Metrics/BlockLength: Exclude: - - 'test/**/*' - - '**/*/cli.rb' - ExcludedMethods: - - 'aasm' - - 'included' + - "test/**/*" + - "**/*/cli.rb" + AllowedMethods: + - "aasm" + - "included" # Soften limits Metrics/ClassLength: Max: 250 Exclude: - - 'test/**/*' + - "test/**/*" # TODO: Soften Limits for phase 1 only Metrics/CyclomaticComplexity: @@ -77,7 +78,7 @@ Metrics/PerceivedComplexity: # Initialization Vector abbreviation Naming/MethodParameterName: - AllowedNames: ['iv', '_', 'io', 'ap'] + AllowedNames: [ "iv", "_", "io", "ap", "id", "_id" ] # Does not allow Symbols to load Security/YAMLLoad: @@ -85,7 +86,7 @@ Security/YAMLLoad: # Needed for testing DateTime Style/DateTime: - Exclude: ['test/**/*'] + Exclude: [ "test/**/*" ] # TODO: Soften Limits for phase 1 only Style/Documentation: diff --git a/lib/rails_semantic_logger.rb b/lib/rails_semantic_logger.rb index 395ea08..5943cb1 100644 --- a/lib/rails_semantic_logger.rb +++ b/lib/rails_semantic_logger.rb @@ -6,21 +6,27 @@ module RailsSemanticLogger module ActionController autoload :LogSubscriber, "rails_semantic_logger/action_controller/log_subscriber" end + module ActionMailer autoload :LogSubscriber, "rails_semantic_logger/action_mailer/log_subscriber" end + module ActionView autoload :LogSubscriber, "rails_semantic_logger/action_view/log_subscriber" end + module ActiveJob autoload :LogSubscriber, "rails_semantic_logger/active_job/log_subscriber" end + module ActiveRecord autoload :LogSubscriber, "rails_semantic_logger/active_record/log_subscriber" end + module Rack autoload :Logger, "rails_semantic_logger/rack/logger" end + module DelayedJob autoload :Plugin, "rails_semantic_logger/delayed_job/plugin" end @@ -48,9 +54,11 @@ def self.unattach(subscriber) end def self.subscriber_patterns(subscriber) - subscriber.patterns.respond_to?(:keys) ? - subscriber.patterns.keys : + if subscriber.patterns.respond_to?(:keys) + subscriber.patterns.keys + else subscriber.patterns + end end private_class_method :subscriber_patterns, :unattach diff --git a/lib/rails_semantic_logger/action_controller/log_subscriber.rb b/lib/rails_semantic_logger/action_controller/log_subscriber.rb index 4aa26f9..80b9b5f 100644 --- a/lib/rails_semantic_logger/action_controller/log_subscriber.rb +++ b/lib/rails_semantic_logger/action_controller/log_subscriber.rb @@ -14,10 +14,10 @@ def process_action(event) # Unused, but needed for Devise 401 status code monkey patch to still work. ::ActionController::Base.log_process_action(payload) - + params = payload[:params] - if params.kind_of?(Hash) || params.kind_of?(::ActionController::Parameters) + if params.is_a?(Hash) || params.is_a?(::ActionController::Parameters) # According to PR https://github.com/reidmorrison/rails_semantic_logger/pull/37/files # params is not always a Hash. payload[:params] = params.to_unsafe_h unless params.is_a?(Hash) @@ -79,7 +79,9 @@ def redirect_to(event) end def send_data(event) - controller_logger(event).info(message: "Sent data", payload: {file_name: event.payload[:filename]}, duration: event.duration) + controller_logger(event).info(message: "Sent data", + payload: {file_name: event.payload[:filename]}, + duration: event.duration) end def unpermitted_parameters(event) diff --git a/lib/rails_semantic_logger/action_mailer/log_subscriber.rb b/lib/rails_semantic_logger/action_mailer/log_subscriber.rb index 5d21f84..be35b70 100644 --- a/lib/rails_semantic_logger/action_mailer/log_subscriber.rb +++ b/lib/rails_semantic_logger/action_mailer/log_subscriber.rb @@ -9,22 +9,22 @@ def deliver(event) message_id = event.payload[:message_id] duration = event.duration.round(1) if ex - log_with_formatter event: event, log_duration: true, level: :error do |fmt| + log_with_formatter event: event, log_duration: true, level: :error do |_fmt| { - message: "Error delivering mail #{message_id} (#{duration}ms)", + message: "Error delivering mail #{message_id} (#{duration}ms)", exception: ex } end else - message = begin + message = if event.payload[:perform_deliveries] "Delivered mail #{message_id} (#{duration}ms)" else "Skipped delivery of mail #{message_id} as `perform_deliveries` is false" end - end - log_with_formatter event: event, log_duration: true do |fmt| - { message: message } + + log_with_formatter event: event, log_duration: true do |_fmt| + {message: message} end end end @@ -34,8 +34,8 @@ def process(event) mailer = event.payload[:mailer] action = event.payload[:action] duration = event.duration.round(1) - log_with_formatter event: event do |fmt| - { message: "#{mailer}##{action}: processed outbound mail in #{duration}ms" } + log_with_formatter event: event do |_fmt| + {message: "#{mailer}##{action}: processed outbound mail in #{duration}ms"} end end @@ -74,8 +74,6 @@ def date event.payload[:date].to_time.utc elsif event.payload[:date].is_a?(String) Time.parse(date).utc - else - nil end end @@ -94,8 +92,8 @@ def action def formatted_args if defined?(mailer.constantize.log_arguments?) && !mailer.constantize.log_arguments? "" - else - JSON.pretty_generate(event.payload[:args].map { |arg| format(arg) }) if event.payload[:args].present? + elsif event.payload[:args].present? + JSON.pretty_generate(event.payload[:args].map { |arg| format(arg) }) end end diff --git a/lib/rails_semantic_logger/action_view/log_subscriber.rb b/lib/rails_semantic_logger/action_view/log_subscriber.rb index 0ad2d8e..9a91577 100644 --- a/lib/rails_semantic_logger/action_view/log_subscriber.rb +++ b/lib/rails_semantic_logger/action_view/log_subscriber.rb @@ -19,7 +19,7 @@ def initialize def render_template(event) return unless should_log? - payload = { + payload = { template: from_rails_root(event.payload[:identifier]) } payload[:within] = from_rails_root(event.payload[:layout]) if event.payload[:layout] @@ -36,7 +36,7 @@ def render_template(event) def render_partial(event) return unless should_log? - payload = { + payload = { partial: from_rails_root(event.payload[:identifier]) } payload[:within] = from_rails_root(event.payload[:layout]) if event.payload[:layout] @@ -56,7 +56,7 @@ def render_collection(event) identifier = event.payload[:identifier] || "templates" - payload = { + payload = { template: from_rails_root(identifier), count: event.payload[:count] } @@ -72,9 +72,9 @@ def render_collection(event) end def start(name, id, payload) - if (name == "render_template.action_view" || name == "render_layout.action_view") && should_log? + if ["render_template.action_view", "render_layout.action_view"].include?(name) && should_log? qualifier = " layout" if name == "render_layout.action_view" - payload = { template: from_rails_root(payload[:identifier]) } + payload = {template: from_rails_root(payload[:identifier])} payload[:within] = from_rails_root(payload[:layout]) if payload[:layout] logger.send(self.class.rendered_log_level, message: "Rendering#{qualifier}", payload: payload) @@ -84,18 +84,19 @@ def start(name, id, payload) end if (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 7 - class Start # :nodoc: - def start(name, id, payload) + class Start + def start(name, _id, payload) return unless %w[render_template.action_view render_layout.action_view].include?(name) qualifier = " layout" if name == "render_layout.action_view" - payload = { template: from_rails_root(payload[:identifier]) } + payload = {template: from_rails_root(payload[:identifier])} payload[:within] = from_rails_root(payload[:layout]) if payload[:layout] logger.debug(message: "Rendering#{qualifier}", payload: payload) end - def finish(name, id, payload) end + def finish(name, id, payload) + end private @@ -105,7 +106,7 @@ def from_rails_root(string) string end - def rails_root # :doc: + def rails_root @root ||= "#{Rails.root}/" end @@ -117,8 +118,10 @@ def logger def self.attach_to(*) ActiveSupport::Notifications.unsubscribe("render_template.action_view") ActiveSupport::Notifications.unsubscribe("render_layout.action_view") - ActiveSupport::Notifications.subscribe("render_template.action_view", RailsSemanticLogger::ActionView::LogSubscriber::Start.new) - ActiveSupport::Notifications.subscribe("render_layout.action_view", RailsSemanticLogger::ActionView::LogSubscriber::Start.new) + ActiveSupport::Notifications.subscribe("render_template.action_view", + RailsSemanticLogger::ActionView::LogSubscriber::Start.new) + ActiveSupport::Notifications.subscribe("render_layout.action_view", + RailsSemanticLogger::ActionView::LogSubscriber::Start.new) super end diff --git a/lib/rails_semantic_logger/active_job/log_subscriber.rb b/lib/rails_semantic_logger/active_job/log_subscriber.rb index 5a8ac3a..0f411d2 100644 --- a/lib/rails_semantic_logger/active_job/log_subscriber.rb +++ b/lib/rails_semantic_logger/active_job/log_subscriber.rb @@ -9,17 +9,17 @@ def enqueue(event) if ex log_with_formatter level: :error, event: event do |fmt| { - message: "Failed enqueuing #{fmt.job_info} (#{ex.class} (#{ex.message})", + message: "Failed enqueuing #{fmt.job_info} (#{ex.class} (#{ex.message})", exception: ex } end elsif event.payload[:aborted] log_with_formatter level: :info, event: event do |fmt| - { message: "Failed enqueuing #{fmt.job_info}, a before_enqueue callback halted the enqueuing execution." } + {message: "Failed enqueuing #{fmt.job_info}, a before_enqueue callback halted the enqueuing execution."} end else log_with_formatter event: event do |fmt| - { message: "Enqueued #{fmt.job_info}" } + {message: "Enqueued #{fmt.job_info}"} end end end @@ -30,13 +30,13 @@ def enqueue_at(event) if ex log_with_formatter level: :error, event: event do |fmt| { - message: "Failed enqueuing #{fmt.job_info} (#{ex.class} (#{ex.message})", + message: "Failed enqueuing #{fmt.job_info} (#{ex.class} (#{ex.message})", exception: ex } end elsif event.payload[:aborted] log_with_formatter level: :info, event: event do |fmt| - { message: "Failed enqueuing #{fmt.job_info}, a before_enqueue callback halted the enqueuing execution." } + {message: "Failed enqueuing #{fmt.job_info}, a before_enqueue callback halted the enqueuing execution."} end else log_with_formatter event: event do |fmt| @@ -56,7 +56,7 @@ def perform(event) if ex log_with_formatter event: event, log_duration: true, level: :error do |fmt| { - message: "Error performing #{fmt.job_info} in #{event.duration.round(2)}ms", + message: "Error performing #{fmt.job_info} in #{event.duration.round(2)}ms", exception: ex } end diff --git a/lib/rails_semantic_logger/active_record/log_subscriber.rb b/lib/rails_semantic_logger/active_record/log_subscriber.rb index c54a334..357303b 100644 --- a/lib/rails_semantic_logger/active_record/log_subscriber.rb +++ b/lib/rails_semantic_logger/active_record/log_subscriber.rb @@ -169,9 +169,7 @@ def render_bind_v5_0_3(attr, value) def render_bind_v6_1(attr, value) case attr when ActiveModel::Attribute - if attr.type.binary? && attr.value - value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>" - end + value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>" if attr.type.binary? && attr.value when Array attr = attr.first else diff --git a/lib/rails_semantic_logger/engine.rb b/lib/rails_semantic_logger/engine.rb index ce85d7e..37fe21a 100644 --- a/lib/rails_semantic_logger/engine.rb +++ b/lib/rails_semantic_logger/engine.rb @@ -77,7 +77,7 @@ class Engine < ::Rails::Engine logger = SemanticLogger[Rails] logger.warn( "Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " \ - "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.", + "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.", e ) logger @@ -111,14 +111,14 @@ class Engine < ::Rails::Engine if defined?(Sidekiq) if Sidekiq.respond_to?(:logger=) Sidekiq.logger = SemanticLogger[Sidekiq] - elsif Sidekiq::VERSION[0..1] == '7.' + elsif Sidekiq::VERSION[0..1] == "7." method = Sidekiq.server? ? :configure_server : :configure_client Sidekiq.public_send(method) { |cfg| cfg.logger = SemanticLogger[Sidekiq] } end end # Replace the Sidetiq logger - Sidetiq.logger = SemanticLogger[Sidetiq] if defined?(Sidetiq) && Sidetiq.respond_to?(:logger=) + Sidetiq.logger = SemanticLogger[Sidetiq] if defined?(Sidetiq) && Sidetiq.respond_to?(:logger=) # Replace the DelayedJob logger if defined?(Delayed::Worker) @@ -143,7 +143,9 @@ class Engine < ::Rails::Engine # Rails Patches require("rails_semantic_logger/extensions/action_cable/tagged_logger_proxy") if defined?(::ActionCable) require("rails_semantic_logger/extensions/action_controller/live") if defined?(::ActionController::Live) - require("rails_semantic_logger/extensions/action_dispatch/debug_exceptions") if defined?(::ActionDispatch::DebugExceptions) + if defined?(::ActionDispatch::DebugExceptions) + require("rails_semantic_logger/extensions/action_dispatch/debug_exceptions") + end if defined?(::ActionView::StreamingTemplateRenderer::Body) require("rails_semantic_logger/extensions/action_view/streaming_template_renderer") end diff --git a/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb b/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb index ff1d942..c6b7566 100644 --- a/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb +++ b/lib/rails_semantic_logger/extensions/active_support/log_subscriber.rb @@ -1,4 +1,4 @@ -if ActiveSupport::VERSION::STRING == '7.1.1' +if ActiveSupport::VERSION::STRING == "7.1.1" require "active_support/log_subscriber" module ActiveSupport diff --git a/lib/rails_semantic_logger/extensions/active_support/logger.rb b/lib/rails_semantic_logger/extensions/active_support/logger.rb index e4e8221..d096f51 100644 --- a/lib/rails_semantic_logger/extensions/active_support/logger.rb +++ b/lib/rails_semantic_logger/extensions/active_support/logger.rb @@ -9,18 +9,18 @@ class << self # Prevent broadcasting since SemanticLogger already supports multiple loggers if method_defined?(:broadcast) undef :broadcast - def broadcast(logger) + def broadcast(_logger) Module.new end end end # Prevent Console from trying to merge loggers - def self.logger_outputs_to?(*args) + def self.logger_outputs_to?(*_args) true end - def self.new(*args, **kwargs) + def self.new(*_args, **_kwargs) SemanticLogger[self] end end diff --git a/lib/rails_semantic_logger/extensions/rails/server.rb b/lib/rails_semantic_logger/extensions/rails/server.rb index f21323b..9aa79b5 100644 --- a/lib/rails_semantic_logger/extensions/rails/server.rb +++ b/lib/rails_semantic_logger/extensions/rails/server.rb @@ -4,7 +4,7 @@ module Rails class Server private - + undef_method :log_to_stdout if method_defined?(:log_to_stdout) def log_to_stdout wrapped_app # touch the app so the logger is set up diff --git a/lib/rails_semantic_logger/rack/logger.rb b/lib/rails_semantic_logger/rack/logger.rb index 6f90f1a..20dea0e 100644 --- a/lib/rails_semantic_logger/rack/logger.rb +++ b/lib/rails_semantic_logger/rack/logger.rb @@ -37,7 +37,7 @@ def call(env) def call_app(request, env) instrumenter = ActiveSupport::Notifications.instrumenter instrumenter_state = instrumenter.start "request.action_dispatch", request: request - instrumenter_finish = -> () { + instrumenter_finish = lambda { instrumenter.finish_with_state(instrumenter_state, "request.action_dispatch", request: request) } diff --git a/test/action_mailer_test.rb b/test/action_mailer_test.rb index be42aec..760bca6 100644 --- a/test/action_mailer_test.rb +++ b/test/action_mailer_test.rb @@ -14,39 +14,39 @@ def some_email(opts) end it "sends the email" do - MyMailer.some_email(to: 'test@test.com', from: 'test@test.com', subject: 'test').deliver_now + MyMailer.some_email(to: "test@test.com", from: "test@test.com", subject: "test").deliver_now end it "writes log messages" do messages = semantic_logger_events do - MyMailer.some_email(to: 'test@test.com', from: 'test@test.com', subject: 'test').deliver_now + MyMailer.some_email(to: "test@test.com", from: "test@test.com", subject: "test").deliver_now end assert_equal 2, messages.count, messages assert_semantic_logger_event( messages[0], - level: :info, - name: "ActionMailer::Base", + level: :info, + name: "ActionMailer::Base", message_includes: "ActionMailerTest::MyMailer#some_email: processed outbound mail", payload_includes: { event_name: "process.action_mailer", - mailer: "ActionMailerTest::MyMailer", - action: :some_email, + mailer: "ActionMailerTest::MyMailer", + action: :some_email } ) assert_semantic_logger_event( messages[1], - level: :info, - name: "ActionMailer::Base", + level: :info, + name: "ActionMailer::Base", message_includes: Rails::VERSION::MAJOR >= 6 ? "Delivered mail" : "Skipped delivery", payload_includes: { - event_name: "deliver.action_mailer", - mailer: "ActionMailerTest::MyMailer", + event_name: "deliver.action_mailer", + mailer: "ActionMailerTest::MyMailer", perform_deliveries: Rails::VERSION::MAJOR >= 6 ? true : nil, - subject: "test", - to: ["test@test.com"], - from: ["test@test.com"], + subject: "test", + to: ["test@test.com"], + from: ["test@test.com"] } ) end @@ -65,15 +65,15 @@ def some_email(opts) let(:payload) do { - mailer: 'MyMailer', - action: :some_email, + mailer: "MyMailer", + action: :some_email } end let(:event_name) { "deliver.action_mailer" } let(:mailer) do - MyMailer.some_email(to: 'test@test.com', from: 'test@test.com', subject: 'test') + MyMailer.some_email(to: "test@test.com", from: "test@test.com", subject: "test") end %i[deliver process].each do |method| diff --git a/test/active_job_test.rb b/test/active_job_test.rb index 317a13a..2ca6ad2 100644 --- a/test/active_job_test.rb +++ b/test/active_job_test.rb @@ -185,7 +185,7 @@ def id event_name: "enqueue.active_job" } ) - assert_match /Failed enqueuing .*, a before_enqueue callback halted the enqueuing execution/, messages[0].message + assert_match(/Failed enqueuing .*, a before_enqueue callback halted the enqueuing execution/, messages[0].message) assert_includes messages[0].payload, :job_id end end @@ -254,12 +254,11 @@ def id event_name: "enqueue.active_job" } ) - assert_match /Failed enqueuing .*, a before_enqueue callback halted the enqueuing execution/, messages[0].message + assert_match(/Failed enqueuing .*, a before_enqueue callback halted the enqueuing execution/, messages[0].message) assert_includes messages[0].payload, :job_id end end - describe "ActiveJob::Logging::LogSubscriber::EventFormatter" do let(:formatter) do RailsSemanticLogger::ActiveJob::LogSubscriber::EventFormatter.new(event: event, log_duration: true) diff --git a/test/active_record_test.rb b/test/active_record_test.rb index 47adc2d..0fc3866 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -3,7 +3,7 @@ class ActiveRecordTest < Minitest::Test describe "ActiveRecord" do # Rails 5 has an extra space - let(:extra_space) { Rails::VERSION::MAJOR >= 6 ? "" : " "} + let(:extra_space) { Rails::VERSION::MAJOR >= 6 ? "" : " " } describe "logs" do it "sql" do @@ -21,7 +21,7 @@ class ActiveRecordTest < Minitest::Test message: "Sample Load", payload_includes: { sql: expected_sql, - binds: { limit: 1 } + binds: {limit: 1} } ) assert_instance_of Integer, messages[0].payload[:allocations] if Rails.version.to_i >= 6 @@ -42,7 +42,7 @@ class ActiveRecordTest < Minitest::Test message: "Sample Load", payload_includes: { sql: expected_sql, - binds: { name: "foo", limit: 1 } + binds: {name: "foo", limit: 1} } ) assert_instance_of Integer, messages[0].payload[:allocations] if Rails.version.to_i >= 6 @@ -54,7 +54,7 @@ class ActiveRecordTest < Minitest::Test message: "Sample Load", payload_includes: { sql: expected_sql, - binds: { name: "foo", limit: 1 }, + binds: {name: "foo", limit: 1}, cached: true } ) @@ -81,7 +81,7 @@ class ActiveRecordTest < Minitest::Test message: "Sample Load", payload_includes: { sql: expected_sql, - binds: { name: "Jack", limit: 1 } + binds: {name: "Jack", limit: 1} } ) assert_instance_of Integer, messages[0].payload[:allocations] if Rails.version.to_i >= 6 @@ -104,7 +104,7 @@ class ActiveRecordTest < Minitest::Test message: "Sample Load", payload_includes: { sql: expected_sql, - binds: { age: [2, 21], limit: 1 } + binds: {age: [2, 21], limit: 1} } ) assert_instance_of Integer, messages[0].payload[:allocations] if Rails.version.to_i >= 6 @@ -127,7 +127,7 @@ class ActiveRecordTest < Minitest::Test message: "Sample Load", payload_includes: { sql: expected_sql, - binds: { age: [2, 3], limit: 1 } + binds: {age: [2, 3], limit: 1} } ) end diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb index c79ccb5..f6ed780 100644 --- a/test/controllers/articles_controller_test.rb +++ b/test/controllers/articles_controller_test.rb @@ -2,7 +2,7 @@ class ArticlesControllerTest < ActionDispatch::IntegrationTest describe ArticlesController do - let(:params) { { article: { text: "Text1", title: "Title1" } } } + let(:params) { {article: {text: "Text1", title: "Title1"}} } describe "#new" do it "shows new article" do diff --git a/test/controllers/dashboard_controller_test.rb b/test/controllers/dashboard_controller_test.rb index f68e69d..b9bea58 100644 --- a/test/controllers/dashboard_controller_test.rb +++ b/test/controllers/dashboard_controller_test.rb @@ -58,7 +58,7 @@ class DashboardControllerTest < ActionDispatch::IntegrationTest end payload = PayloadCollector.last - assert_equal payload[:params], { "controller" => "dashboard", "action" => "show" } + assert_equal payload[:params], {"controller" => "dashboard", "action" => "show"} end end end diff --git a/test/dummy/config.ru b/test/dummy/config.ru index 08dd501..9d6e524 100644 --- a/test/dummy/config.ru +++ b/test/dummy/config.ru @@ -1,4 +1,4 @@ # This file is used by Rack-based servers to start the application. -require ::File.expand_path("../config/environment", __FILE__) +require File.expand_path("config/environment", __dir__) run Dummy::Application diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb index 260d4e7..15d2e1b 100644 --- a/test/dummy/config/application.rb +++ b/test/dummy/config/application.rb @@ -7,7 +7,7 @@ module Dummy class Application < Rails::Application # Configure sensitive parameters which will be filtered from the log file. - config.filter_parameters += [:password] + config.filter_parameters += [:password] config.active_record.sqlite3.represent_boolean_as_integer = true if config.active_record.sqlite3 # Settings in config/environments/* take precedence over those specified here. @@ -30,6 +30,6 @@ class Application < Rails::Application config.semantic_logger.backtrace_level = :trace # Test out Amazing Print - config.rails_semantic_logger.ap_options = { multiline: false, ruby19_syntax: true } + config.rails_semantic_logger.ap_options = {multiline: false, ruby19_syntax: true} end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 91495b7..cac6b76 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,4 +16,4 @@ # Add Semantic Logger helpers for Minitest Minitest::Test.include SemanticLogger::Test::Minitest -::ActionMailer::Base.delivery_method = :test +ActionMailer::Base.delivery_method = :test From 93fed9edd0ba21490044e5fc7474c7b83eb2676c Mon Sep 17 00:00:00 2001 From: Reid Morrison Date: Thu, 16 Nov 2023 14:40:26 -0500 Subject: [PATCH 25/25] v4.14.0 --- lib/rails_semantic_logger/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rails_semantic_logger/version.rb b/lib/rails_semantic_logger/version.rb index a58ac19..18f8d38 100644 --- a/lib/rails_semantic_logger/version.rb +++ b/lib/rails_semantic_logger/version.rb @@ -1,3 +1,3 @@ module RailsSemanticLogger - VERSION = "4.13.0".freeze + VERSION = "4.14.0".freeze end