debug_logging 1.0.15 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "debug_logging"
4
+ require 'bundler/setup'
5
+ require 'debug_logging'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "debug_logging"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
@@ -1,35 +1,39 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'debug_logging/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "debug_logging"
8
+ spec.name = 'debug_logging'
8
9
  spec.version = DebugLogging::VERSION
9
- spec.authors = ["Peter Boling"]
10
- spec.email = ["peter.boling@gmail.com"]
10
+ spec.authors = ['Peter Boling']
11
+ spec.email = ['peter.boling@gmail.com']
11
12
 
12
- spec.summary = %q{Drop-in debug logging useful when a call stack gets unruly}
13
- spec.description = %q{
13
+ spec.summary = 'Drop-in debug logging useful when a call stack gets unruly'
14
+ spec.description = '
14
15
  Unobtrusive debug logging for Ruby. NO LITTERING.
15
16
  Automatically log selected methods and their arguments as they are called at runtime!
16
- }
17
- spec.license = "MIT"
18
- spec.homepage = "https://github.com/pboling/debug_logging"
17
+ '
18
+ spec.license = 'MIT'
19
+ spec.homepage = 'https://github.com/pboling/debug_logging'
19
20
 
20
21
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
22
  f.match(%r{^(test|spec|features)/})
22
23
  end
23
- spec.bindir = "exe"
24
+ spec.bindir = 'exe'
24
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
- spec.require_paths = ["lib"]
26
- spec.required_ruby_version = ">= 2.0.0" # Uses named parameters
26
+ spec.require_paths = ['lib']
27
+ spec.required_ruby_version = '>= 2.4.0' # Uses magic comments
27
28
 
28
- spec.add_runtime_dependency "colorize", "~> 0.8"
29
- spec.add_development_dependency "rspec-pending_for"
30
- spec.add_development_dependency "bundler", "~> 1.14"
31
- spec.add_development_dependency "coveralls", "~> 0.8"
32
- spec.add_development_dependency "rake", "~> 10.0"
33
- spec.add_development_dependency "rspec", "~> 3.0"
34
- spec.add_development_dependency "activesupport", "~> 5.1"
29
+ spec.add_runtime_dependency 'colorize', '>= 0'
30
+ spec.add_development_dependency 'activesupport', '~> 5.2', '>= 5.2.4.4'
31
+ spec.add_development_dependency 'bundler', '>= 2'
32
+ spec.add_development_dependency 'byebug', '>= 11'
33
+ spec.add_development_dependency 'rake', '>= 13'
34
+ spec.add_development_dependency 'rspec', '>= 3'
35
+ spec.add_development_dependency 'rspec-pending_for', '>= 0'
36
+ spec.add_development_dependency 'rubocop', '>= 0.92'
37
+ spec.add_development_dependency 'rubocop-rspec', '>= 1'
38
+ spec.add_development_dependency 'silent_stream', '>= 1'
35
39
  end
@@ -1,13 +1,15 @@
1
- require "logger"
2
- require "colorized_string"
3
- require "digest"
1
+ # frozen_string_literal: true
4
2
 
5
- require "debug_logging/version"
6
- require "debug_logging/configuration"
7
- require "debug_logging/argument_printer"
8
- require "debug_logging/instance_logger_modulizer"
9
- require "debug_logging/instance_logger"
10
- require "debug_logging/class_logger"
3
+ require 'logger'
4
+ require 'colorized_string'
5
+ require 'digest'
6
+
7
+ require 'debug_logging/version'
8
+ require 'debug_logging/configuration'
9
+ require 'debug_logging/argument_printer'
10
+ require 'debug_logging/instance_logger_modulizer'
11
+ require 'debug_logging/instance_logger'
12
+ require 'debug_logging/class_logger'
11
13
 
12
14
  ####################
13
15
  # #
@@ -62,12 +64,12 @@ module DebugLogging
62
64
  end
63
65
 
64
66
  #### API ####
65
- # Not used by this gem internally, but provides an external interface for
67
+ # Not used by this gem internally, but provides an external interface for
66
68
  # classes to also use this logging tool directly,
67
69
  # with configured options like benchmarking, colors, or leg level.
68
70
  def debug_log(message = nil, config_proxy = nil, &block)
69
71
  # If a, instance-method-level, or class-method-level custom config is not
70
- # passed in, then fall back to the class' default config, which is a
72
+ # passed in, then fall back to the class' default config, which is a
71
73
  # potentially customized copy of the default config for the whole app.
72
74
  config_proxy ||= debug_config
73
75
  config_proxy.log(message, &block)
@@ -103,81 +105,115 @@ module DebugLogging
103
105
  def debug_config_reset(config = Configuration.new)
104
106
  @debug_logging_configuration = config
105
107
  end
108
+
109
+ def debug_enabled
110
+ @debug_logging_configuration.enabled
111
+ end
112
+
113
+ def debug_enabled=(value)
114
+ @debug_logging_configuration.enabled = value
115
+ end
116
+
106
117
  def debug_logger
107
118
  @debug_logging_configuration.logger
108
119
  end
120
+
109
121
  def debug_logger=(logger)
110
122
  @debug_logging_configuration.logger = logger
111
123
  end
124
+
112
125
  def debug_log_level
113
126
  @debug_logging_configuration.log_level
114
127
  end
128
+
115
129
  def debug_log_level=(log_level)
116
130
  @debug_logging_configuration.log_level = log_level
117
131
  end
132
+
118
133
  def debug_multiple_last_hashes
119
134
  @debug_logging_configuration.multiple_last_hashes
120
135
  end
136
+
121
137
  def debug_multiple_last_hashes=(multiple_last_hashes)
122
138
  @debug_logging_configuration.multiple_last_hashes = multiple_last_hashes
123
139
  end
140
+
124
141
  def debug_last_hash_to_s_proc
125
142
  @debug_logging_configuration.last_hash_to_s_proc
126
143
  end
144
+
127
145
  def debug_last_hash_to_s_proc=(last_hash_to_s_proc)
128
146
  @debug_logging_configuration.last_hash_to_s_proc = last_hash_to_s_proc
129
147
  end
148
+
130
149
  def debug_last_hash_max_length
131
150
  @debug_logging_configuration.last_hash_max_length
132
151
  end
152
+
133
153
  def debug_last_hash_max_length=(last_hash_max_length)
134
154
  @debug_logging_configuration.last_hash_max_length = last_hash_max_length
135
155
  end
156
+
136
157
  def debug_args_max_length
137
158
  @debug_logging_configuration.args_max_length
138
159
  end
160
+
139
161
  def debug_args_max_length=(args_max_length)
140
162
  @debug_logging_configuration.args_max_length = args_max_length
141
163
  end
164
+
142
165
  def debug_instance_benchmarks
143
166
  @debug_logging_configuration.instance_benchmarks
144
167
  end
168
+
145
169
  def debug_instance_benchmarks=(instance_benchmarks)
146
170
  @debug_logging_configuration.instance_benchmarks = instance_benchmarks
147
171
  end
172
+
148
173
  def debug_class_benchmarks
149
174
  @debug_logging_configuration.class_benchmarks
150
175
  end
176
+
151
177
  def debug_class_benchmarks=(class_benchmarks)
152
178
  @debug_logging_configuration.class_benchmarks = class_benchmarks
153
179
  end
180
+
154
181
  def debug_colorized_chain_for_method
155
182
  @debug_logging_configuration.colorized_chain_for_method
156
183
  end
184
+
157
185
  def debug_colorized_chain_for_method=(colorized_chain_for_method)
158
186
  @debug_logging_configuration.colorized_chain_for_method = colorized_chain_for_method
159
187
  end
188
+
160
189
  def debug_colorized_chain_for_class
161
190
  @debug_logging_configuration.colorized_chain_for_class
162
191
  end
192
+
163
193
  def debug_colorized_chain_for_class=(colorized_chain_for_class)
164
194
  @debug_logging_configuration.colorized_chain_for_class = colorized_chain_for_class
165
195
  end
196
+
166
197
  def debug_add_invocation_id
167
198
  @debug_logging_configuration.add_invocation_id
168
199
  end
200
+
169
201
  def debug_add_invocation_id=(add_invocation_id)
170
202
  @debug_logging_configuration.add_invocation_id = add_invocation_id
171
203
  end
204
+
172
205
  def debug_mark_scope_exit
173
206
  @debug_logging_configuration.mark_scope_exit
174
207
  end
208
+
175
209
  def debug_mark_scope_exit=(mark_scope_exit)
176
210
  @debug_logging_configuration.mark_scope_exit = mark_scope_exit
177
211
  end
212
+
178
213
  def debug_ellipsis
179
214
  @debug_logging_configuration.ellipsis
180
215
  end
216
+
181
217
  def debug_ellipsis=(ellipsis)
182
218
  @debug_logging_configuration.ellipsis = ellipsis
183
219
  end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'debug_logging/log_subscriber'
4
+ require 'debug_logging/class_notifier'
5
+ require 'debug_logging/instance_notifier_modulizer'
6
+ require 'debug_logging/instance_notifier'
@@ -1,21 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DebugLogging
2
4
  module ArgumentPrinter
3
5
  def debug_benchmark_to_s(tms: nil)
4
- "completed in #{sprintf("%f", tms.real)}s (#{sprintf("%f", tms.total)}s CPU)"
6
+ "completed in #{format('%f', tms.real)}s (#{format('%f', tms.total)}s CPU)"
5
7
  end
8
+
9
+ def debug_event_name_to_s(method_to_notify: nil)
10
+ "#{method_to_notify}.log"
11
+ end
12
+
6
13
  def debug_invocation_id_to_s(args: nil, config_proxy: nil)
7
14
  if config_proxy.debug_add_invocation_id
8
- invocation = " ~#{args.object_id}@#{(Time.now.to_f.to_s % '%#-21a')[4..(-4)]}~"
15
+ invocation = " ~#{args.object_id}@#{(Time.now.to_f.to_s % '%#-21a')[4..-4]}~"
9
16
  case config_proxy.debug_add_invocation_id
10
- when true then
17
+ when true
11
18
  invocation
12
19
  else
13
20
  config_proxy.debug_add_invocation_id.call(ColorizedString[invocation])
14
21
  end
15
22
  else
16
- ""
23
+ ''
17
24
  end
18
25
  end
26
+
19
27
  def debug_invocation_to_s(klass: nil, separator: nil, method_to_log: nil, config_proxy: nil)
20
28
  klass_string = if config_proxy.debug_colorized_chain_for_class
21
29
  config_proxy.debug_colorized_chain_for_class.call(ColorizedString[klass.to_s])
@@ -29,50 +37,55 @@ module DebugLogging
29
37
  end
30
38
  "#{klass_string}#{separator}#{method_string}"
31
39
  end
32
- def debug_signature_to_s(args: nil, config_proxy: nil)
33
- printed_args = ""
40
+
41
+ def debug_signature_to_s(args: nil, config_proxy: nil) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
42
+ printed_args = ''
43
+
34
44
  add_args_ellipsis = false
35
45
  if config_proxy.debug_last_hash_to_s_proc && args[-1].is_a?(Hash)
46
+ add_last_hash_ellipsis = false
36
47
  if args.length > 1
37
- add_last_hash_ellipsis = false
38
48
  if config_proxy.debug_multiple_last_hashes
39
49
  last_hash_args, other_args = args.partition do |arg|
40
- arg.is_a?(Hash)
41
- end
42
- other_args_string = other_args.map(&:inspect).join(", ")[0..(config_proxy.debug_args_max_length)]
43
- # On the debug_multiple_last_hashes truthy branch we don't print the ellipsis after regular args
44
- # because it will go instead after the last hash (if needed)
45
- # ...join(", ").tap {|x| _add_args_ellipsis = x.length > config_proxy.debug_args_max_length}
50
+ arg.is_a?(Hash)
51
+ end
52
+ other_args_string = other_args.map(&:inspect).join(', ')[0..(config_proxy.debug_args_max_length)]
53
+ # On the debug_multiple_last_hashes truthy branch we don't print the ellipsis after regular args
54
+ # because it will go instead after the last hash (if needed)
55
+ # ...join(", ").tap {|x| _add_args_ellipsis = x.length > config_proxy.debug_args_max_length}
46
56
  last_hash_args_string = last_hash_args.map do |arg|
47
- config_proxy.debug_last_hash_to_s_proc.call(arg).to_s.
48
- tap {|x|
49
- add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length
50
- }[0..(config_proxy.debug_last_hash_max_length)].
51
- tap {|x|
52
- x << config_proxy.debug_ellipsis if add_last_hash_ellipsis
53
- }
54
- end.join(", ")
55
- printed_args << other_args_string if other_args_string
56
- printed_args << ", " if !other_args_string.empty? && !last_hash_args_string.empty?
57
- printed_args << last_hash_args_string if last_hash_args_string
57
+ arr = []
58
+ arr << config_proxy.debug_last_hash_to_s_proc.call(arg).to_s
59
+ .tap do |x|
60
+ add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length
61
+ end
62
+ if add_last_hash_ellipsis
63
+ arr[-1] = arr[-1][0..(config_proxy.debug_last_hash_max_length)]
64
+ arr << config_proxy.debug_ellipsis
65
+ end
66
+ arr
67
+ end.flatten.join(', ')
68
+ printed_args += other_args_string if other_args_string
69
+ printed_args += ', ' if !other_args_string.empty? && !last_hash_args_string.empty?
70
+ printed_args += last_hash_args_string if last_hash_args_string && !last_hash_args_string.empty?
58
71
  else
59
- printed_args << args[0..(-2)].map {|x| x.inspect}.join(", ").tap {|x| add_args_ellipsis = x.length > config_proxy.debug_args_max_length}[0..(config_proxy.debug_args_max_length)]
60
- printed_args << config_proxy.debug_ellipsis if add_args_ellipsis
61
- printed_args << ", " << config_proxy.debug_last_hash_to_s_proc.call(args[-1]).tap {|x| add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length}[0..(config_proxy.debug_last_hash_max_length)]
62
- printed_args << config_proxy.debug_ellipsis if add_last_hash_ellipsis
72
+ printed_args += args[0..-2].map(&:inspect).join(', ').tap { |x| add_args_ellipsis = x.length > config_proxy.debug_args_max_length }[0..(config_proxy.debug_args_max_length)]
73
+ printed_args += config_proxy.debug_ellipsis if add_args_ellipsis
74
+ printed_args += ", #{config_proxy.debug_last_hash_to_s_proc.call(args[-1]).tap { |x| add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length }[0..(config_proxy.debug_last_hash_max_length)]}"
75
+ printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
63
76
  end
64
77
  else
65
- printed_args << String(config_proxy.debug_last_hash_to_s_proc.call(args[0])).tap {|x| add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length}[0..(config_proxy.debug_last_hash_max_length)]
66
- printed_args << config_proxy.debug_ellipsis if add_last_hash_ellipsis
78
+ printed_args += String(config_proxy.debug_last_hash_to_s_proc.call(args[0])).tap { |x| add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length }[0..(config_proxy.debug_last_hash_max_length)]
79
+ printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
67
80
  end
68
81
  else
69
82
  if args.length == 1 && args[0].is_a?(Hash)
70
83
  # handle double splat
71
- printed_args << ("**" << args.map {|x| x.inspect}.join(", ").tap {|x| add_args_ellipsis = x.length > config_proxy.debug_args_max_length})[0..(config_proxy.debug_args_max_length)]
84
+ printed_args += ("**#{args.map(&:inspect).join(', ').tap { |x| add_args_ellipsis = x.length > config_proxy.debug_args_max_length }}")[0..(config_proxy.debug_args_max_length)]
72
85
  else
73
- printed_args << args.map {|x| x.inspect}.join(", ").tap {|x| add_args_ellipsis = x.length > config_proxy.debug_args_max_length}[0..(config_proxy.debug_args_max_length)]
86
+ printed_args += args.map(&:inspect).join(', ').tap { |x| add_args_ellipsis = x.length > config_proxy.debug_args_max_length }[0..(config_proxy.debug_args_max_length)]
74
87
  end
75
- printed_args << config_proxy.debug_ellipsis if add_args_ellipsis
88
+ printed_args += config_proxy.debug_ellipsis if add_args_ellipsis
76
89
  end
77
90
  "(#{printed_args})"
78
91
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DebugLogging
2
4
  module ClassLogger
3
5
  def logged(*methods_to_log)
@@ -19,7 +21,7 @@ module DebugLogging
19
21
  proxy
20
22
  else
21
23
  proxy = if opts
22
- Configuration.new(**(debug_config.to_hash.merge(opts)))
24
+ Configuration.new(**debug_config.to_hash.merge(opts))
23
25
  else
24
26
  debug_config
25
27
  end
@@ -31,7 +33,7 @@ module DebugLogging
31
33
  log_prefix = nil
32
34
  invocation_id = nil
33
35
  config_proxy.log do
34
- log_prefix = debug_invocation_to_s(klass: to_s, separator: ".", method_to_log: method_to_log, config_proxy: config_proxy)
36
+ log_prefix = debug_invocation_to_s(klass: to_s, separator: '.', method_to_log: method_to_log, config_proxy: config_proxy)
35
37
  invocation_id = debug_invocation_id_to_s(args: args, config_proxy: config_proxy)
36
38
  signature = debug_signature_to_s(args: args, config_proxy: config_proxy)
37
39
  "#{log_prefix}#{signature}#{invocation_id}"
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DebugLogging
4
+ module ClassNotifier
5
+ def notifies(*methods_to_notify)
6
+ payload = methods_to_notify.last.is_a?(Hash) && methods_to_notify.pop || {}
7
+ if methods_to_notify.first.is_a?(Array)
8
+ methods_to_notify = methods_to_notify.shift
9
+ else
10
+ # logged :meth1, :meth2, :meth3 without options is valid too
11
+ end
12
+ methods_to_notify.each do |method_to_notify|
13
+ # method name must be a symbol
14
+ method_to_notify = method_to_notify.to_sym
15
+ original_method = method(method_to_notify)
16
+ config_proxy = nil
17
+ (class << self; self; end).class_eval do
18
+ define_method(method_to_notify) do |*args, &block|
19
+ config_proxy = if (proxy = instance_variable_get(DebugLogging::Configuration.config_pointer('k', method_to_notify)))
20
+ proxy
21
+ else
22
+ proxy = if !payload.empty?
23
+ Configuration.new(**debug_config.to_hash.merge(payload))
24
+ else
25
+ debug_config
26
+ end
27
+ proxy.register(method_to_notify)
28
+ instance_variable_set(DebugLogging::Configuration.config_pointer('k', method_to_notify), proxy)
29
+ proxy
30
+ end
31
+ ActiveSupport::Notifications.instrument(
32
+ debug_event_name_to_s(method_to_notify: method_to_notify), { args: args }.merge(payload)
33
+ ) do
34
+ original_method.call(*args, &block)
35
+ end
36
+ end
37
+ end
38
+
39
+ ActiveSupport::Notifications.subscribe(/log/) do |*args|
40
+ config_proxy&.log do
41
+ DebugLogging::LogSubscriber.log_event(ActiveSupport::Notifications::Event.new(*args))
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,21 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DebugLogging
2
4
  class Configuration
3
- DEFAULT_ELLIPSIS = " ✂️ …".freeze
5
+ DEFAULT_ELLIPSIS = ' ✂️ …'
6
+ # For reference, log levels as integers mapped to symbols:
4
7
  # LEVELS = { 0 => :debug, 1 => :info, 2 => :warn, 3 => :error, 4 => :fatal, 5 => :unknown }
5
- attr_accessor :logger
6
- attr_accessor :log_level
7
- attr_accessor :multiple_last_hashes
8
- attr_accessor :last_hash_to_s_proc
9
- attr_accessor :last_hash_max_length
10
- attr_accessor :args_max_length
11
- attr_accessor :instance_benchmarks
12
- attr_accessor :class_benchmarks
13
- attr_accessor :colorized_chain_for_method
14
- attr_accessor :colorized_chain_for_class
15
- attr_accessor :add_invocation_id
16
- attr_accessor :ellipsis
17
- attr_accessor :mark_scope_exit
18
- attr_reader :methods_to_log
8
+ attr_accessor :enabled
9
+ attr_accessor :logger, :log_level, :multiple_last_hashes, :last_hash_to_s_proc, :last_hash_max_length,
10
+ :args_max_length, :colorized_chain_for_method, :colorized_chain_for_class, :add_invocation_id,
11
+ :ellipsis, :mark_scope_exit
12
+ attr_reader :instance_benchmarks, :class_benchmarks, :active_support_notifications, :methods_to_log
19
13
  # alias the readers to the debug_* prefix so an instance of this class
20
14
  # can have the same API granted by `extend DebugLogging`
21
15
  #
@@ -34,20 +28,21 @@ module DebugLogging
34
28
  # }
35
29
  # )
36
30
  #
37
- alias :debug_logger :logger
38
- alias :debug_log_level :log_level
39
- alias :debug_multiple_last_hashes :multiple_last_hashes
40
- alias :debug_last_hash_to_s_proc :last_hash_to_s_proc
41
- alias :debug_last_hash_max_length :last_hash_max_length
42
- alias :debug_args_max_length :args_max_length
43
- alias :debug_instance_benchmarks :instance_benchmarks
44
- alias :debug_class_benchmarks :class_benchmarks
45
- alias :debug_colorized_chain_for_method :colorized_chain_for_method
46
- alias :debug_colorized_chain_for_class :colorized_chain_for_class
47
- alias :debug_add_invocation_id :add_invocation_id
48
- alias :debug_ellipsis :ellipsis
49
- alias :debug_mark_scope_exit :mark_scope_exit
50
-
31
+ alias debug_enabled enabled
32
+ alias debug_logger logger
33
+ alias debug_log_level log_level
34
+ alias debug_multiple_last_hashes multiple_last_hashes
35
+ alias debug_last_hash_to_s_proc last_hash_to_s_proc
36
+ alias debug_last_hash_max_length last_hash_max_length
37
+ alias debug_args_max_length args_max_length
38
+ alias debug_instance_benchmarks instance_benchmarks
39
+ alias debug_class_benchmarks class_benchmarks
40
+ alias debug_colorized_chain_for_method colorized_chain_for_method
41
+ alias debug_colorized_chain_for_class colorized_chain_for_class
42
+ alias debug_add_invocation_id add_invocation_id
43
+ alias debug_ellipsis ellipsis
44
+ alias debug_mark_scope_exit mark_scope_exit
45
+
51
46
  class << self
52
47
  def config_pointer(type, method_to_log)
53
48
  # Methods names that do not match the following regex can't be part of an ivar name
@@ -57,66 +52,87 @@ module DebugLogging
57
52
  end
58
53
  end
59
54
  def initialize(**options)
60
- @logger = options.key?(:logger) ? options[:logger] : Logger.new(STDOUT)
55
+ @enabled = options.key?(:enabled) ? options[:enabled] : true
56
+ @logger = options.key?(:logger) ? options[:logger] : Logger.new($stdout)
61
57
  @log_level = options.key?(:log_level) ? options[:log_level] : :debug
62
58
  @multiple_last_hashes = options.key?(:multiple_last_hashes) ? options[:multiple_last_hashes] : false
63
59
  @last_hash_to_s_proc = options.key?(:last_hash_to_s_proc) ? options[:last_hash_to_s_proc] : nil
64
60
  @last_hash_max_length = options.key?(:last_hash_max_length) ? options[:last_hash_max_length] : 1_000
65
61
  @args_max_length = options.key?(:args_max_length) ? options[:args_max_length] : 1_000
66
- @instance_benchmarks = options.key?(:instance_benchmarks) ? options[:instance_benchmarks] : false
67
- @class_benchmarks = options.key?(:class_benchmarks) ? options[:class_benchmarks] : false
68
62
  @colorized_chain_for_method = options.key?(:colorized_chain_for_method) ? options[:colorized_chain_for_method] : false
69
63
  @colorized_chain_for_class = options.key?(:colorized_chain_for_class) ? options[:colorized_chain_for_class] : false
70
64
  @add_invocation_id = options.key?(:add_invocation_id) ? options[:add_invocation_id] : true
71
65
  @ellipsis = options.key?(:ellipsis) ? options[:ellipsis] : DEFAULT_ELLIPSIS
72
66
  @mark_scope_exit = options.key?(:mark_scope_exit) ? options[:mark_scope_exit] : false
67
+ self.instance_benchmarks = options.key?(:instance_benchmarks) ? options[:instance_benchmarks] : false
68
+ self.class_benchmarks = options.key?(:class_benchmarks) ? options[:class_benchmarks] : false
69
+ self.active_support_notifications = options.key?(:active_support_notifications) ? options[:active_support_notifications] : false
73
70
  @methods_to_log = []
74
71
  end
72
+
75
73
  def log(message = nil, &block)
74
+ return unless enabled
76
75
  return unless logger
76
+
77
77
  if block_given?
78
78
  logger.send(log_level, &block)
79
79
  else
80
80
  logger.send(log_level, message)
81
81
  end
82
82
  end
83
+
83
84
  def loggable?
84
85
  return @loggable if defined?(@loggable)
86
+
85
87
  @loggable = logger.send("#{log_level}?")
86
88
  end
89
+
87
90
  def benchmarkable_for?(benchmarks)
88
91
  return @benchmarkable if defined?(@benchmarkable)
89
- @benchmarkable = loggable? && self.send(benchmarks)
92
+
93
+ @benchmarkable = loggable? && send(benchmarks)
90
94
  end
95
+
91
96
  def exit_scope_markable?
92
97
  return @exit_scope_markable if defined?(@exit_scope_markable)
98
+
93
99
  @exit_scope_markable = loggable? && mark_scope_exit
94
100
  end
101
+
95
102
  def instance_benchmarks=(instance_benchmarks)
96
- require "benchmark" if instance_benchmarks
103
+ require 'benchmark' if instance_benchmarks
97
104
  @instance_benchmarks = instance_benchmarks
98
105
  end
106
+
99
107
  def class_benchmarks=(class_benchmarks)
100
- require "benchmark" if class_benchmarks
108
+ require 'benchmark' if class_benchmarks
101
109
  @class_benchmarks = class_benchmarks
102
110
  end
111
+
112
+ def active_support_notifications=(active_support_notifications)
113
+ require 'debug_logging/active_support_notifications' if active_support_notifications
114
+ @active_support_notifications = active_support_notifications
115
+ end
116
+
103
117
  def to_hash
104
118
  {
105
- logger: logger,
106
- log_level: log_level,
107
- multiple_last_hashes: multiple_last_hashes,
108
- last_hash_to_s_proc: last_hash_to_s_proc,
109
- last_hash_max_length: last_hash_max_length,
110
- args_max_length: args_max_length,
111
- instance_benchmarks: instance_benchmarks,
112
- class_benchmarks: class_benchmarks,
113
- colorized_chain_for_method: colorized_chain_for_method,
114
- colorized_chain_for_class: colorized_chain_for_class,
115
- add_invocation_id: add_invocation_id,
116
- ellipsis: ellipsis,
117
- mark_scope_exit: mark_scope_exit
119
+ logger: logger,
120
+ log_level: log_level,
121
+ multiple_last_hashes: multiple_last_hashes,
122
+ last_hash_to_s_proc: last_hash_to_s_proc,
123
+ last_hash_max_length: last_hash_max_length,
124
+ args_max_length: args_max_length,
125
+ instance_benchmarks: instance_benchmarks,
126
+ class_benchmarks: class_benchmarks,
127
+ colorized_chain_for_method: colorized_chain_for_method,
128
+ colorized_chain_for_class: colorized_chain_for_class,
129
+ add_invocation_id: add_invocation_id,
130
+ ellipsis: ellipsis,
131
+ mark_scope_exit: mark_scope_exit,
132
+ active_support_notifications: active_support_notifications
118
133
  }
119
134
  end
135
+
120
136
  def register(method_lo_log)
121
137
  @methods_to_log << method_lo_log
122
138
  end