Module: TestProf::Rails::LoggingHelpers

Defined in:
lib/test_prof/recipes/logging.rb

Overview

Add ‘with_logging` and `with_ar_logging helpers`

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_loggablesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/test_prof/recipes/logging.rb', line 53

def all_loggables
  return @all_loggables if instance_variable_defined?(:@all_loggables)

  @all_loggables = [
    ::ActiveSupport::LogSubscriber,
    ::Rails,
    defined?(::ActiveRecord::Base) && ::ActiveRecord::Base,
    defined?(::ActiveJob::Base) && ::ActiveJob::Base,
    defined?(::ActionView::Base) && ::ActionView::Base,
    defined?(::ActionMailer::Base) && ::ActionMailer::Base,
    defined?(::ActionCable::Server::Base.config) && ::ActionCable::Server::Base.config,
    defined?(::ActiveStorage) && ::ActiveStorage
  ].compact
end

.ar_loggablesObject



44
45
46
47
48
49
50
51
# File 'lib/test_prof/recipes/logging.rb', line 44

def ar_loggables
  return @ar_loggables if instance_variable_defined?(:@ar_loggables)

  @ar_loggables = [
    ::ActiveRecord::Base,
    ::ActiveSupport::LogSubscriber
  ]
end

.global_loggablesObject



31
32
33
34
35
# File 'lib/test_prof/recipes/logging.rb', line 31

def global_loggables
  return @global_loggables if instance_variable_defined?(:@global_loggables)

  @global_loggables = []
end

.loggerObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/test_prof/recipes/logging.rb', line 19

def logger
  return @logger if instance_variable_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=(logger) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/test_prof/recipes/logging.rb', line 10

def logger=(logger)
  @logger = logger

  # swap global loggers
  global_loggables.each do |loggable|
    loggable.logger = logger
  end
end

.restore_logger(was_loggers, loggables) ⇒ Object



78
79
80
81
82
# File 'lib/test_prof/recipes/logging.rb', line 78

def restore_logger(was_loggers, loggables)
  loggables.each_with_index do |loggable, i|
    loggable.logger = was_loggers[i]
  end
end

.swap_logger(loggables) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



70
71
72
73
74
75
76
# File 'lib/test_prof/recipes/logging.rb', line 70

def swap_logger(loggables)
  loggables.map do |loggable|
    was_logger = loggable.logger
    loggable.logger = logger
    was_logger
  end
end

.swap_logger!(loggables) ⇒ Object



37
38
39
40
41
42
# File 'lib/test_prof/recipes/logging.rb', line 37

def swap_logger!(loggables)
  loggables.each do |loggable|
    loggable.logger = logger
    global_loggables << loggable
  end
end

Instance Method Details

#with_ar_loggingObject



93
94
95
96
97
98
# File 'lib/test_prof/recipes/logging.rb', line 93

def with_ar_logging
  *loggers = LoggingHelpers.swap_logger(LoggingHelpers.ar_loggables)
  yield
ensure
  LoggingHelpers.restore_logger(loggers, LoggingHelpers.ar_loggables)
end

#with_loggingObject

Enable verbose Rails logging within a block



86
87
88
89
90
91
# File 'lib/test_prof/recipes/logging.rb', line 86

def with_logging
  *loggers = LoggingHelpers.swap_logger(LoggingHelpers.all_loggables)
  yield
ensure
  LoggingHelpers.restore_logger(loggers, LoggingHelpers.all_loggables)
end