Browse Source

register_log_subscriber

main
pvincent 2 months ago
parent
commit
bd6a5c6fde
  1. 4
      app/views/scores/_score.html.erb
  2. 4
      config/environments/development.rb
  3. 27
      lib/semantic/dev_loader.rb
  4. 5
      lib/semantic/log_subscriber.rb

4
app/views/scores/_score.html.erb

@ -9,8 +9,8 @@
<%= score.created_at %>
</td>
<td class="p-2 m-5 flex justify-end">
<%= render partial: 'common/button', locals: {path: score, icon: :eye, tooltip: 'open score'} %>
<%= render partial: 'common/button', locals: {path: edit_score_path(score), icon: :edit, tooltip: 'edit score'} %>
<%= render partial: 'common/button', locals: {path: score, icon: :eye, tooltip: 'open score',data:{ turbo_frame: '_top'}} %>
<%= render partial: 'common/button', locals: {path: edit_score_path(score), icon: :edit, tooltip: 'edit score',data:{ turbo_frame: '_top'}} %>
<%= render partial: 'common/button', locals: {path: score, icon: :trash, tooltip: 'delete score',data:{ turbo_method: :delete}} %>
</td>
</tr>

4
config/environments/development.rb

@ -88,10 +88,6 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength
end
elsif Rails.application.server?
config.after_initialize do
# force module preloading
Semantic::AnsiColors.class
Semantic::AnsiDimensions.class
Semantic::DevLoader.new
end
end

27
lib/semantic/dev_loader.rb

@ -2,16 +2,26 @@ module Semantic
# use the Zeitwerk autoloader to reattach_appender for development autoreloading feature
class DevLoader
def initialize
@subscribers = []
force_preload_module
Rails.autoloaders.main.on_load('ApplicationController') do
append_ansi_formatter
append_subscriber_once
register_log_subscriber
end
append_ansi_formatter
# FIXME: when final
# Rails.logger.level = :error
end
private
def force_preload_module
self.class.module_parent.constants.each do |const|
self.class.module_parent.const_get(const)
end
end
def append_ansi_formatter
SemanticLogger.clear_appenders!
formatter = Semantic::AnsiFormatter.new
@ -20,15 +30,16 @@ module Semantic
filter: ->(log) { !formatter.reject(log) })
end
def append_subscriber_once
return unless @previous_subscribe.nil?
def register_log_subscriber
@subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }
@subscribers.clear
subscriber = Semantic::LogSubscriber.new('toto1')
@previous_subscribe = ActiveSupport::Notifications.subscribe 'start_processing.action_controller' do |event|
subscriber.start_processing(event)
log_subscriber = Semantic::LogSubscriber.new('toto1')
@subscribers << ActiveSupport::Notifications.subscribe('start_processing.action_controller') do |event|
log_subscriber.start_processing(event)
end
ActiveSupport::Notifications.subscribe 'process_action.action_controller' do |event|
subscriber.finish_processing(event)
@subscribers << ActiveSupport::Notifications.subscribe('process_action.action_controller') do |event|
log_subscriber.finish_processing(event)
end
end
end

5
lib/semantic/log_subscriber.rb

@ -38,9 +38,8 @@ module Semantic
end
additions << "GC: #{event.gc_time.round(1)}ms"
logger.info("Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round}ms " \
"(#{additions.join(' | ')})")
logger.debug("Processed in #{event.duration.round}ms (#{additions.join(' | ')})")
logger.info("Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]}")
end
end

Loading…
Cancel
Save