Browse Source

dev_loader

main
pvincent 1 month ago
parent
commit
6c6d56e71f
  1. 1
      app/controllers/scores_controller.rb
  2. 27
      config/environments/development.rb
  3. 2
      lib/semantic/ansi_formatter.rb
  4. 19
      lib/semantic/dev_loader.rb

1
app/controllers/scores_controller.rb

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

27
config/environments/development.rb

@ -87,33 +87,8 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength
# filter: ->(log) { !formatter.reject(log) })
end
elsif Rails.application.server?
config.after_initialize do
source_location = Semantic::AnsiFormatter.instance_method(:initialize).source_location[0]
previous_mtime = File.stat(source_location).mtime
formatter = Semantic::AnsiFormatter.new
# SemanticLogger.clear_appenders!
SemanticLogger.add_appender(io: $stdout,
formatter:,
filter: ->(log) { !formatter.reject(log) })
[Semantic::AnsiFormatter, ApplicationController].each do |class_name|
Rails.autoloaders.main.on_load(class_name.to_s) do |klass, abspath|
if klass == Semantic::AnsiFormatter
mtime = File.stat(source_location).mtime
if previous_mtime != mtime
previous_mtime = mtime
SemanticLogger.clear_appenders!
formatter = Semantic::AnsiFormatter.new
SemanticLogger.add_appender(io: $stdout,
formatter:,
filter: lambda { |log|
!formatter.reject(log)
})
end
end
end
end
Semantic::DevLoader.new
end
end
end

2
lib/semantic/ansi_formatter.rb

@ -28,7 +28,7 @@ module Semantic
end
def call(log, logger)
# puts 'ok1'
# puts 'ok2'
log = alter(log)
self.log = log

19
lib/semantic/dev_loader.rb

@ -0,0 +1,19 @@
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 }
append_ansi_formatter
end
private
def append_ansi_formatter
SemanticLogger.clear_appenders!
formatter = Semantic::AnsiFormatter.new
SemanticLogger.add_appender(io: $stdout,
formatter:,
filter: ->(log) { !formatter.reject(log) })
end
end
end
Loading…
Cancel
Save