Class: ActiveRecordMigrationUi::Logger

Inherits:
StringIO
  • Object
show all
Defined in:
lib/active_record_migration_ui/logger.rb

Overview

Captures the Rails.logger logs, looking for the one from ActiveRecord::Base and broadcast them to the front app through ActionCable.

This allows this gem to show the migration output in real-time.

Instance Method Summary collapse

Instance Method Details

#add(_, message = nil, progname = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_record_migration_ui/logger.rb', line 20

def add(_, message = nil, progname = nil)
  final_message = message || progname

  return unless ActiveRecordMigrationUi.running_migration?

  # Ignore empty logs
  return unless final_message

  # Avoids infinit loop from ActionCable logging that it is broadcasting.
  return if action_cable_is_speaking_in?(final_message)

  # Replace the actual logger with the MutedLogger
  old_logger = ActionCable.server.config.logger
  ActionCable.server.config.logger = MutedLogger.new

  # Broadcast the message to the front
  ActionCable.server.broadcast ActiveRecordMigrationUi.ac_channel_name,
                               command: 'log',
                               message: final_message.gsub(/(\[\d+m)/, '')

  # Revert the logger
  ActionCable.server.config.logger = old_logger
end