Class: BrainzLab::Rails::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/brainzlab/rails/railtie.rb

Class Method Summary collapse

Class Method Details

.setup_log_formatterObject



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_loggingObject



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
# File 'lib/brainzlab/rails/railtie.rb', line 122

def silence_rails_logging
  # Create a null logger that discards all output.
  # Use ActiveSupport::Logger (not a plain ::Logger) so the logger
  # retains LoggerSilence#silence — SolidQueue's poller calls
  # `ActiveRecord::Base.logger.silence { ... }` every cycle, and a
  # plain Logger lacks `silence` (NoMethodError → workers crash-loop).
  logger_class = defined?(ActiveSupport::Logger) ? ActiveSupport::Logger : Logger
  null_logger = logger_class.new(File::NULL)
  null_logger.level = Logger::FATAL

  # 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