Module: RSpecPower::LoggingHelpers
- Defined in:
- lib/rspec_power/logging.rb
Class Method Summary collapse
- .all_loggables ⇒ Object
- .ar_loggables ⇒ Object
- .global_loggables ⇒ Object
- .logger ⇒ Object
- .logger=(new_logger) ⇒ Object
- .restore_logger(old_loggers, targets) ⇒ Object
- .swap_logger(targets) ⇒ Object
Instance Method Summary collapse
Class Method Details
.all_loggables ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rspec_power/logging.rb', line 47 def all_loggables base = [ (::Rails if defined?(::Rails)), (::ActiveSupport::LogSubscriber if defined?(::ActiveSupport::LogSubscriber)), (::ActiveRecord::Base if defined?(::ActiveRecord::Base)), (::ActionController::Base if defined?(::ActionController::Base)), (::ActiveJob::Base if defined?(::ActiveJob::Base)), (::ActionView::Base if defined?(::ActionView::Base)), (::ActionMailer::Base if defined?(::ActionMailer::Base)), (::ActionCable if defined?(::ActionCable)) ].compact # Include all log subscribers for controller/action_view etc. if defined?(::ActiveSupport::LogSubscriber) ObjectSpace.each_object(Class).select { |c| c < ::ActiveSupport::LogSubscriber }.each do |subscriber| base << subscriber if subscriber.respond_to?(:logger) end end base.select { |l| l.respond_to?(:logger) } end |
.ar_loggables ⇒ Object
69 70 71 72 73 74 |
# File 'lib/rspec_power/logging.rb', line 69 def ar_loggables @ar_loggables ||= [ ::ActiveRecord::Base, ::ActiveSupport::LogSubscriber ] end |
.global_loggables ⇒ Object
26 27 28 |
# File 'lib/rspec_power/logging.rb', line 26 def global_loggables @global_loggables ||= [] end |
.logger ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rspec_power/logging.rb', line 15 def logger return @logger if defined?(@logger) @logger = if defined?(ActiveSupport::TaggedLogging) ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new($stdout)) elsif defined?(ActiveSupport::Logger) ActiveSupport::Logger.new($stdout) else Logger.new($stdout) end end |
.logger=(new_logger) ⇒ Object
10 11 12 13 |
# File 'lib/rspec_power/logging.rb', line 10 def logger=(new_logger) @logger = new_logger global_loggables.each { |l| l.logger = new_logger } end |
.restore_logger(old_loggers, targets) ⇒ Object
43 44 45 |
# File 'lib/rspec_power/logging.rb', line 43 def restore_logger(old_loggers, targets) targets.each_with_index { |t, i| t.logger = old_loggers[i] } end |
.swap_logger(targets) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rspec_power/logging.rb', line 30 def swap_logger(targets) targets.map do |target| old_logger = target.logger begin target.logger = logger rescue NoMethodError # Some classes expose .logger= only via class_attribute; try via Rails.logger # but we still continue to next target to avoid halting. end old_logger end end |
Instance Method Details
#with_ar_logging ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/rspec_power/logging.rb', line 85 def with_ar_logging targets = LoggingHelpers.ar_loggables old_loggers = LoggingHelpers.swap_logger(targets) yield ensure LoggingHelpers.restore_logger(old_loggers, targets) end |
#with_logging ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/rspec_power/logging.rb', line 77 def with_logging targets = LoggingHelpers.all_loggables old_loggers = LoggingHelpers.swap_logger(targets) yield ensure LoggingHelpers.restore_logger(old_loggers, targets) end |