Browse Source

log_subscriber

main
pvincent 3 months ago
parent
commit
5fa7c80c30
  1. 1
      app/controllers/scores_controller.rb
  2. 1
      config/environments/development.rb
  3. 13
      lib/semantic/dev_loader.rb
  4. 7
      lib/semantic/log_subscriber.rb

1
app/controllers/scores_controller.rb

@ -5,6 +5,7 @@ class ScoresController < ApplicationController
before_action :set_score, only: %i[show edit update destroy]
def index
logger.info('inside controller')
@q = Score.all.ransack(q_params)
@pagy, @scores = pagy(@q.result)
end

1
config/environments/development.rb

@ -88,6 +88,7 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength
end
elsif Rails.application.server?
config.after_initialize do
Semantic::LogSubscriber.new # force loading
Semantic::DevLoader.new
end
end

13
lib/semantic/dev_loader.rb

@ -2,7 +2,10 @@ module Semantic
# use the Zeitwerk autoloader to reattach_appender for development autoreloading feature
class DevLoader
def initialize
Rails.autoloaders.main.on_load('ApplicationController') { append_ansi_formatter }
Rails.autoloaders.main.on_load('ApplicationController') do
append_ansi_formatter
append_subscriber_once
end
append_ansi_formatter
end
@ -15,5 +18,13 @@ module Semantic
formatter:,
filter: ->(log) { !formatter.reject(log) })
end
def append_subscriber_once
return unless @previous_subscribe.nil?
@previous_subscribe = ActiveSupport::Notifications.subscribe 'start_processing.action_controller' do |event|
Semantic::LogSubscriber.new.start_processing(event)
end
end
end
end

7
lib/semantic/log_subscriber.rb

@ -0,0 +1,7 @@
module Semantic
class LogSubscriber < ActiveSupport::LogSubscriber
def start_processing(event)
logger.warn("MyStarted #{event.payload[:controller]} - #{event.payload[:action]}", event.payload[:request])
end
end
end
Loading…
Cancel
Save