Class: BrainzLab::Rails::Railtie
- Inherits:
-
Rails::Railtie
- Object
- Rails::Railtie
- BrainzLab::Rails::Railtie
- Defined in:
- lib/brainzlab/rails/railtie.rb
Class Method Summary collapse
Class Method Details
.setup_log_formatter ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/brainzlab/rails/railtie.rb', line 93 def setup_log_formatter # Lazy require to ensure Rails is fully loaded require_relative 'log_formatter' require_relative 'log_subscriber' config = BrainzLab.configuration formatter_config = { enabled: config.log_formatter_enabled, colors: config.log_formatter_colors.nil? ? $stdout.tty? : config.log_formatter_colors, hide_assets: config.log_formatter_hide_assets, compact_assets: config.log_formatter_compact_assets, show_params: config.log_formatter_show_params } # Create formatter and attach to subscriber formatter = LogFormatter.new(formatter_config) LogSubscriber.formatter = formatter # Attach our subscribers LogSubscriber.attach_to :action_controller SqlLogSubscriber.attach_to :active_record ViewLogSubscriber.attach_to :action_view CableLogSubscriber.attach_to :action_cable # Silence Rails default ActionController logging silence_rails_logging end |
.silence_rails_logging ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/brainzlab/rails/railtie.rb', line 122 def silence_rails_logging # Create a null logger that discards all output. # # It must respond to #silence because consumers like SolidQueue's poller # call `ActiveRecord::Base.logger.silence { ... }` every cycle. Two traps: # - a plain ::Logger lacks #silence → NoMethodError (crash) # - a fresh ActiveSupport::Logger reintroduces a `@@silencer` class-var # clash with ::Logger inside worker processes → RuntimeError # ("class variable @@silencer of ActiveSupport::Logger is overtaken") # So use a plain ::Logger and give THIS instance a no-op #silence that # just yields — satisfies callers without touching LoggerSilence's class var. null_logger = Logger.new(File::NULL) null_logger.level = Logger::FATAL def null_logger.silence(_severity = nil) block_given? ? yield(self) : self end # Silence ActiveRecord SQL logging ActiveRecord::Base.logger = null_logger if defined?(ActiveRecord::Base) # Silence ActionController logging (the "Completed" message) ActionController::Base.logger = null_logger if defined?(ActionController::Base) # Silence ActionView logging ActionView::Base.logger = null_logger if defined?(ActionView::Base) # Silence the class-level loggers for specific subscribers ActionController::LogSubscriber.logger = null_logger if defined?(ActionController::LogSubscriber) ActionView::LogSubscriber.logger = null_logger if defined?(ActionView::LogSubscriber) ActiveRecord::LogSubscriber.logger = null_logger if defined?(ActiveRecord::LogSubscriber) # Silence ActionCable logging ActionCable.server.config.logger = null_logger if defined?(ActionCable::Server::Base) if defined?(ActionCable::Connection::TaggedLoggerProxy) # ActionCable uses a tagged logger proxy that we need to quiet end # Silence the main Rails logger to remove "Started GET" messages # Wrap the formatter to filter specific messages if defined?(::Rails.logger) && ::Rails.logger.respond_to?(:formatter=) original_formatter = ::Rails.logger.formatter || Logger::Formatter.new ::Rails.logger.formatter = FilteringFormatter.new(original_formatter) end rescue StandardError # Silently fail if we can't silence end |