File: logging.rb

package info (click to toggle)
ruby-test-prof 1.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 660 kB
  • sloc: ruby: 6,064; makefile: 4
file content (153 lines) | stat: -rw-r--r-- 4,827 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
121
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
# frozen_string_literal: true

require "test_prof/core"

module TestProf
  module Rails
    # Add `with_logging` and `with_ar_logging helpers`
    module LoggingHelpers
      class << self
        def logger=(logger)
          @logger = logger

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

        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

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

          @global_loggables = []
        end

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

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

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

        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
        # rubocop:enable Metrics/CyclomaticComplexity
        # rubocop:enable Metrics/PerceivedComplexity

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

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

      # Enable verbose Rails logging within a block
      def with_logging
        if ::ActiveSupport.respond_to?(:event_reporter)
          @was_events_debug_mode = ActiveSupport.event_reporter.debug_mode?
          ::ActiveSupport.event_reporter.debug_mode = true
        end
        *loggers = LoggingHelpers.swap_logger(LoggingHelpers.all_loggables)
        yield
      ensure
        if ::ActiveSupport.respond_to?(:event_reporter)
          ::ActiveSupport.event_reporter.debug_mode = @was_events_debug_mode
        end
        LoggingHelpers.restore_logger(loggers, LoggingHelpers.all_loggables)
      end

      def with_ar_logging
        if ::ActiveSupport.respond_to?(:event_reporter)
          @was_events_debug_mode = ActiveSupport.event_reporter.debug_mode?
          ::ActiveSupport.event_reporter.debug_mode = true
        end
        *loggers = LoggingHelpers.swap_logger(LoggingHelpers.ar_loggables)
        yield
      ensure
        if ::ActiveSupport.respond_to?(:event_reporter)
          ::ActiveSupport.event_reporter.debug_mode = @was_events_debug_mode
        end
        LoggingHelpers.restore_logger(loggers, LoggingHelpers.ar_loggables)
      end
    end
  end
end

if TestProf.rspec?
  RSpec.shared_context "logging:verbose" do
    around(:each) do |ex|
      next with_logging(&ex) if ex.metadata[:log] == true || ex.metadata[:log] == :all

      ex.call
    end
  end

  RSpec.shared_context "logging:active_record" do
    around(:each) do |ex|
      with_ar_logging(&ex)
    end
  end

  RSpec.configure do |config|
    config.include TestProf::Rails::LoggingHelpers
    config.include_context "logging:active_record", log: :ar
    config.include_context "logging:verbose", log: true
  end
end

TestProf.activate("LOG", "all") do
  TestProf.log :info, "Rails verbose logging enabled"
  if ::ActiveSupport.respond_to?(:event_reporter)
    ::ActiveSupport.event_reporter.debug_mode = true
  end
  TestProf::Rails::LoggingHelpers.swap_logger!(TestProf::Rails::LoggingHelpers.all_loggables)
end

TestProf.activate("LOG", "ar") do
  TestProf.log :info, "Active Record verbose logging enabled"
  if ::ActiveSupport.respond_to?(:event_reporter)
    ::ActiveSupport.event_reporter.debug_mode = true
  end
  TestProf::Rails::LoggingHelpers.swap_logger!(TestProf::Rails::LoggingHelpers.ar_loggables)
end