debug_logging 3.1.0 → 3.1.5

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.
@@ -7,7 +7,7 @@ require 'debug_logging/version'
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = 'debug_logging'
9
9
  spec.version = DebugLogging::VERSION
10
- spec.authors = ['Peter Boling']
10
+ spec.authors = ['Peter Boling', 'guckin']
11
11
  spec.email = ['peter.boling@gmail.com']
12
12
 
13
13
  spec.summary = 'Drop-in debug logging useful when a call stack gets unruly'
@@ -33,7 +33,10 @@ Automatically log selected methods and their arguments as they are called at run
33
33
  spec.add_development_dependency 'rake', '>= 13'
34
34
  spec.add_development_dependency 'rspec', '>= 3'
35
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'
36
+ spec.add_development_dependency 'rubocop', '~> 1.0'
37
+ spec.add_development_dependency 'rubocop-md'
38
+ spec.add_development_dependency 'rubocop-performance'
39
+ spec.add_development_dependency 'rubocop-rake'
40
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.0'
38
41
  spec.add_development_dependency 'silent_stream', '>= 1'
39
42
  end
@@ -5,8 +5,12 @@ require 'colorized_string'
5
5
  require 'digest'
6
6
 
7
7
  require 'debug_logging/version'
8
+ require 'debug_logging/errors'
8
9
  require 'debug_logging/configuration'
10
+ require 'debug_logging/util'
11
+ require 'debug_logging/finalize'
9
12
  require 'debug_logging/argument_printer'
13
+ require 'debug_logging/hooks'
10
14
  require 'debug_logging/instance_logger_modulizer'
11
15
  require 'debug_logging/instance_logger'
12
16
  require 'debug_logging/class_logger'
@@ -61,6 +65,11 @@ module DebugLogging
61
65
  def self.extended(base)
62
66
  base.send(:extend, ArgumentPrinter)
63
67
  base.debug_config_reset(Configuration.new(**debug_logging_configuration.to_hash))
68
+ base.class_eval do
69
+ def base.inherited(subclass)
70
+ subclass.debug_config_reset(Configuration.new(**debug_config.to_hash))
71
+ end
72
+ end
64
73
  end
65
74
 
66
75
  #### API ####
@@ -154,6 +163,14 @@ module DebugLogging
154
163
  @debug_logging_configuration.last_hash_max_length = last_hash_max_length
155
164
  end
156
165
 
166
+ def debug_args_to_s_proc
167
+ @debug_logging_configuration.args_to_s_proc
168
+ end
169
+
170
+ def debug_args_to_s_proc=(args_to_s_proc)
171
+ @debug_logging_configuration.args_to_s_proc = args_to_s_proc
172
+ end
173
+
157
174
  def debug_args_max_length
158
175
  @debug_logging_configuration.args_max_length
159
176
  end
@@ -2,15 +2,13 @@
2
2
 
3
3
  module DebugLogging
4
4
  module ArgumentPrinter
5
- def debug_benchmark_to_s(tms: nil)
5
+ def debug_benchmark_to_s(tms:)
6
6
  "completed in #{format('%f', tms.real)}s (#{format('%f', tms.total)}s CPU)"
7
7
  end
8
8
 
9
- def debug_event_name_to_s(method_to_notify: nil)
10
- "#{method_to_notify}.log"
11
- end
12
-
13
9
  def debug_invocation_id_to_s(args: nil, config_proxy: nil)
10
+ return '' unless args && config_proxy
11
+
14
12
  if config_proxy.debug_add_invocation_id
15
13
  invocation = " ~#{args.object_id}@#{(Time.now.to_f.to_s % '%#-21a')[4..-4]}~"
16
14
  case config_proxy.debug_add_invocation_id
@@ -25,6 +23,8 @@ module DebugLogging
25
23
  end
26
24
 
27
25
  def debug_invocation_to_s(klass: nil, separator: nil, method_to_log: nil, config_proxy: nil)
26
+ return '' unless config_proxy
27
+
28
28
  klass_string = if config_proxy.debug_colorized_chain_for_class
29
29
  config_proxy.debug_colorized_chain_for_class.call(ColorizedString[klass.to_s])
30
30
  else
@@ -38,56 +38,152 @@ module DebugLogging
38
38
  "#{klass_string}#{separator}#{method_string}"
39
39
  end
40
40
 
41
- def debug_signature_to_s(args: nil, config_proxy: nil) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
41
+ def debug_signature_to_s(args: nil, config_proxy: nil) # rubocop:disable Metrics/CyclomaticComplexity
42
+ return '' unless args && config_proxy
43
+
42
44
  printed_args = ''
43
45
 
44
46
  add_args_ellipsis = false
45
47
  if config_proxy.debug_last_hash_to_s_proc && args[-1].is_a?(Hash)
46
- add_last_hash_ellipsis = false
48
+ add_other_args_ellipsis = false
47
49
  if args.length > 1
48
50
  if config_proxy.debug_multiple_last_hashes
49
51
  last_hash_args, other_args = args.partition do |arg|
50
52
  arg.is_a?(Hash)
51
53
  end
52
- other_args_string = other_args.map(&:inspect).join(', ')[0..(config_proxy.debug_args_max_length)]
54
+ other_args_string = if config_proxy.debug_args_to_s_proc
55
+ printed, add_other_args_ellipsis = debug_safe_proc(
56
+ proc_name:'args_to_s_proc',
57
+ proc: config_proxy.debug_args_to_s_proc,
58
+ args: other_args,
59
+ max_length: config_proxy.debug_args_max_length
60
+ )
61
+ printed
62
+ else
63
+ other_args.map(&:inspect).join(', ').tap do |x|
64
+ add_other_args_ellipsis = x.length > config_proxy.debug_args_max_length
65
+ end[0..(config_proxy.debug_args_max_length)]
66
+ end
67
+ other_args_string += config_proxy.debug_ellipsis if add_other_args_ellipsis
53
68
  # 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)
69
+ # because it will go instead after each of the last hashes (if needed)
55
70
  # ...join(", ").tap {|x| _add_args_ellipsis = x.length > config_proxy.debug_args_max_length}
56
71
  last_hash_args_string = last_hash_args.map do |arg|
57
72
  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
73
+ printed, add_last_hash_ellipsis = debug_safe_proc(
74
+ proc_name:'last_hash_to_s_proc',
75
+ proc: config_proxy.debug_last_hash_to_s_proc,
76
+ args: arg,
77
+ max_length: config_proxy.debug_last_hash_max_length
78
+ )
79
+ printed += config_proxy.debug_ellipsis if add_last_hash_ellipsis
80
+ arr << printed
66
81
  arr
67
82
  end.flatten.join(', ')
68
83
  printed_args += other_args_string if other_args_string
69
84
  printed_args += ', ' if !other_args_string.empty? && !last_hash_args_string.empty?
70
85
  printed_args += last_hash_args_string if last_hash_args_string && !last_hash_args_string.empty?
71
86
  else
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)]}"
87
+ other_args = args[0..-2]
88
+ other_args_string = if config_proxy.debug_args_to_s_proc
89
+ printed, add_other_args_ellipsis = debug_safe_proc(
90
+ proc_name:'args_to_s_proc',
91
+ proc: config_proxy.debug_args_to_s_proc,
92
+ args: other_args,
93
+ max_length: config_proxy.debug_args_max_length
94
+ )
95
+ printed
96
+ else
97
+ other_args.map(&:inspect).join(', ').tap do |x|
98
+ add_other_args_ellipsis = x.length > config_proxy.debug_args_max_length
99
+ end[0..(config_proxy.debug_args_max_length)]
100
+ end
101
+ other_args_string += config_proxy.debug_ellipsis if add_other_args_ellipsis
102
+ printed_args += other_args_string
103
+ printed, add_last_hash_ellipsis = debug_safe_proc(
104
+ proc_name:'last_hash_to_s_proc',
105
+ proc: config_proxy.debug_last_hash_to_s_proc,
106
+ args: args[-1],
107
+ max_length: config_proxy.debug_last_hash_max_length
108
+ )
109
+ printed_args += ", #{printed}"
75
110
  printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
76
111
  end
77
112
  else
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)]
113
+ printed, add_last_hash_ellipsis = debug_safe_proc(
114
+ proc_name:'last_hash_to_s_proc',
115
+ proc: config_proxy.debug_last_hash_to_s_proc,
116
+ args: args[0],
117
+ max_length: config_proxy.debug_last_hash_max_length
118
+ )
119
+ printed_args += printed
79
120
  printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
80
121
  end
81
122
  else
82
- if args.length == 1 && args[0].is_a?(Hash)
83
- # handle double splat
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)]
85
- else
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)]
87
- end
123
+ printed_args += if config_proxy.debug_args_to_s_proc
124
+ printed, add_args_ellipsis = debug_safe_proc(
125
+ proc_name:'args_to_s_proc',
126
+ proc: config_proxy.debug_args_to_s_proc,
127
+ args: args,
128
+ max_length: config_proxy.debug_args_max_length
129
+ )
130
+ printed
131
+ elsif args.length == 1 && args[0].is_a?(Hash)
132
+ # handle double splat
133
+ ("**#{args.map(&:inspect).join(', ').tap do |x|
134
+ add_args_ellipsis = x.length > config_proxy.debug_args_max_length
135
+ end }")[0..(config_proxy.debug_args_max_length)]
136
+ else
137
+ args.map(&:inspect).join(', ').tap do |x|
138
+ add_args_ellipsis = x.length > config_proxy.debug_args_max_length
139
+ end[0..(config_proxy.debug_args_max_length)]
140
+ end
88
141
  printed_args += config_proxy.debug_ellipsis if add_args_ellipsis
89
142
  end
90
143
  "(#{printed_args})"
91
144
  end
145
+
146
+ def debug_safe_proc(proc_name:, proc:, args:, max_length:)
147
+ max_length ||= 1000 # can't be nil
148
+ begin
149
+ add_ellipsis = false
150
+ printed = String(proc.call(args)).tap do |x|
151
+ add_ellipsis = x.length > max_length
152
+ end[0..(max_length)]
153
+ return printed, add_ellipsis
154
+ rescue => e
155
+ return "#{e.class}: #{e.message}\nPlease check that your #{proc_name} is able to handle #{args}", false
156
+ end
157
+ end
158
+
159
+ def debug_payload_to_s(payload: nil, config_proxy: nil)
160
+ return '' unless payload && config_proxy
161
+
162
+ if payload
163
+ case config_proxy.debug_add_payload
164
+ when true
165
+ payload.inspect
166
+ else
167
+ printed_payload = ""
168
+ printed, add_payload_ellipsis = debug_safe_proc(
169
+ proc_name: "add_payload",
170
+ proc: config_proxy.debug_add_payload,
171
+ args: payload,
172
+ max_length: config_proxy.payload_max_length
173
+ )
174
+ printed_payload += printed
175
+ printed_payload += config_proxy.debug_ellipsis if add_payload_ellipsis
176
+ printed_payload
177
+ end
178
+ else
179
+ ''
180
+ end
181
+ end
182
+
183
+ module_function
184
+
185
+ def debug_event_name_to_s(method_to_notify: nil)
186
+ "#{method_to_notify}.log"
187
+ end
92
188
  end
93
189
  end
@@ -3,50 +3,55 @@
3
3
  module DebugLogging
4
4
  module ClassLogger
5
5
  def logged(*methods_to_log)
6
- # When opts are present it will always be a new configuration instance per method
7
- # When opts are not present it will reuse the class' configuration object
8
- opts = methods_to_log.last.is_a?(Hash) && methods_to_log.pop
9
- if methods_to_log.first.is_a?(Array)
10
- methods_to_log = methods_to_log.shift
11
- else
12
- # logged :meth1, :meth2, :meth3 without options is valid too
13
- end
14
- methods_to_log.each do |method_to_log|
15
- # method name must be a symbol
16
- method_to_log = method_to_log.to_sym
6
+ methods_to_log, payload, config_opts = DebugLogging::Util.extract_payload_and_config(
7
+ method_names: methods_to_log,
8
+ payload: nil,
9
+ config: nil
10
+ )
11
+ Array(methods_to_log).each do |method_to_log|
12
+ method_to_log, method_payload, method_config_opts = DebugLogging::Util.extract_payload_and_config(
13
+ method_names: method_to_log,
14
+ payload: payload,
15
+ config: config_opts
16
+ )
17
17
  original_method = method(method_to_log)
18
18
  (class << self; self; end).class_eval do
19
19
  define_method(method_to_log) do |*args, &block|
20
- config_proxy = if (proxy = instance_variable_get(DebugLogging::Configuration.config_pointer('k', method_to_log)))
21
- proxy
22
- else
23
- proxy = if opts
24
- Configuration.new(**debug_config.to_hash.merge(opts))
25
- else
26
- debug_config
27
- end
28
- proxy.register(method_to_log)
29
- instance_variable_set(DebugLogging::Configuration.config_pointer('k', method_to_log), proxy)
30
- proxy
31
- end
20
+ config_proxy = DebugLogging::Util.config_proxy_finder(
21
+ scope: self,
22
+ config_opts: method_config_opts,
23
+ method_name: method_to_log,
24
+ proxy_ref: 'kl'
25
+ )
32
26
  method_return_value = nil
33
27
  log_prefix = nil
34
28
  invocation_id = nil
35
29
  config_proxy.log do
36
- log_prefix = debug_invocation_to_s(klass: to_s, separator: '.', method_to_log: method_to_log, config_proxy: config_proxy)
30
+ paydirt = DebugLogging::Util.payload_instance_vaiable_hydration(scope: self, payload: method_payload)
31
+ log_prefix = debug_invocation_to_s(klass: to_s, separator: '.', method_to_log: method_to_log,
32
+ config_proxy: config_proxy)
37
33
  invocation_id = debug_invocation_id_to_s(args: args, config_proxy: config_proxy)
38
34
  signature = debug_signature_to_s(args: args, config_proxy: config_proxy)
39
- "#{log_prefix}#{signature}#{invocation_id}"
35
+ paymud = debug_payload_to_s(payload: paydirt, config_proxy: config_proxy)
36
+ "#{log_prefix}#{signature}#{invocation_id} debug: #{paymud}"
40
37
  end
41
38
  if config_proxy.benchmarkable_for?(:debug_class_benchmarks)
42
39
  tms = Benchmark.measure do
43
- method_return_value = original_method.call(*args, &block)
40
+ method_return_value = if args.size == 1 && (harsh = args[0]) && harsh.is_a?(Hash)
41
+ original_method.call(**harsh, &block)
42
+ else
43
+ original_method.call(*args, &block)
44
+ end
44
45
  end
45
46
  config_proxy.log do
46
47
  "#{log_prefix} #{debug_benchmark_to_s(tms: tms)}#{invocation_id}"
47
48
  end
48
49
  else
49
- method_return_value = original_method.call(*args, &block)
50
+ method_return_value = if args.size == 1 && (harsh = args[0]) && harsh.is_a?(Hash)
51
+ original_method.call(**harsh, &block)
52
+ else
53
+ original_method.call(*args, &block)
54
+ end
50
55
  if config_proxy.exit_scope_markable? && invocation_id && !invocation_id.empty?
51
56
  config_proxy.log do
52
57
  "#{log_prefix} completed#{invocation_id}"
@@ -3,44 +3,51 @@
3
3
  module DebugLogging
4
4
  module ClassNotifier
5
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
6
+ methods_to_notify, payload, config_opts = DebugLogging::Util.extract_payload_and_config(
7
+ method_names: methods_to_notify,
8
+ payload: nil,
9
+ config: nil
10
+ )
11
+ Array(methods_to_notify).each do |method_to_notify|
12
+ method_to_notify, method_payload, method_config_opts = DebugLogging::Util.extract_payload_and_config(
13
+ method_names: method_to_notify,
14
+ payload: payload,
15
+ config: config_opts
16
+ )
15
17
  original_method = method(method_to_notify)
16
- config_proxy = nil
17
18
  (class << self; self; end).class_eval do
18
19
  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
20
+ config_proxy = DebugLogging::Util.config_proxy_finder(
21
+ scope: self,
22
+ config_opts: method_config_opts,
23
+ method_name: method_to_notify,
24
+ proxy_ref: 'kn'
25
+ ) do |proxy|
26
+ ActiveSupport::Notifications.subscribe(
27
+ DebugLogging::ArgumentPrinter.debug_event_name_to_s(method_to_notify: method_to_notify)
28
+ ) do |*debug_args|
29
+ proxy.log do
30
+ DebugLogging::LogSubscriber.log_event(ActiveSupport::Notifications::Event.new(*debug_args))
31
+ end
32
+ end
33
+ end
34
+ paydirt = DebugLogging::Util.payload_instance_vaiable_hydration(scope: self, payload: method_payload)
31
35
  ActiveSupport::Notifications.instrument(
32
- debug_event_name_to_s(method_to_notify: method_to_notify), { args: args }.merge(payload)
36
+ DebugLogging::ArgumentPrinter.debug_event_name_to_s(method_to_notify: method_to_notify),
37
+ {
38
+ debug_args: args,
39
+ config_proxy: config_proxy,
40
+ **paydirt
41
+ }
33
42
  ) do
34
- original_method.call(*args, &block)
43
+ if args.size == 1 && (harsh = args[0]) && harsh.is_a?(Hash)
44
+ original_method.call(**harsh, &block)
45
+ else
46
+ original_method.call(*args, &block)
47
+ end
35
48
  end
36
49
  end
37
50
  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
51
  end
45
52
  end
46
53
  end
@@ -3,13 +3,37 @@
3
3
  module DebugLogging
4
4
  class Configuration
5
5
  DEFAULT_ELLIPSIS = ' ✂️ …'
6
+ CONFIG_ATTRS_DEFAULTS = {
7
+ enabled: true,
8
+ logger: Logger.new($stdout),
9
+ log_level: :debug,
10
+ multiple_last_hashes: false,
11
+ last_hash_to_s_proc: nil,
12
+ last_hash_max_length: 1_000,
13
+ args_to_s_proc: nil,
14
+ args_max_length: 1_000,
15
+ colorized_chain_for_method: false,
16
+ colorized_chain_for_class: false,
17
+ add_invocation_id: true,
18
+ ellipsis: DEFAULT_ELLIPSIS,
19
+ mark_scope_exit: false,
20
+ add_payload: true, # Can also be a proc returning a string, which will be called when printing the payload
21
+ payload_max_length: 1_000
22
+ }.freeze
23
+ CONFIG_ATTRS = CONFIG_ATTRS_DEFAULTS.keys
24
+ CONFIG_READERS_DEFAULTS = {
25
+ instance_benchmarks: false,
26
+ class_benchmarks: false,
27
+ active_support_notifications: false
28
+ }.freeze
29
+ CONFIG_READERS = CONFIG_READERS_DEFAULTS.keys
30
+ CONFIG_KEYS = CONFIG_ATTRS + CONFIG_READERS
31
+
6
32
  # For reference, log levels as integers mapped to symbols:
7
33
  # LEVELS = { 0 => :debug, 1 => :info, 2 => :warn, 3 => :error, 4 => :fatal, 5 => :unknown }
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
34
+ attr_accessor(*CONFIG_ATTRS)
35
+ attr_reader :methods_to_log, *CONFIG_READERS
36
+
13
37
  # alias the readers to the debug_* prefix so an instance of this class
14
38
  # can have the same API granted by `extend DebugLogging`
15
39
  #
@@ -20,6 +44,7 @@ module DebugLogging
20
44
  # log_level: :debug # at what level do the messages created by this gem sent at?
21
45
  # last_hash_to_s_proc: nil # e.g. ->(hash) { "keys: #{hash.keys}" }
22
46
  # last_hash_max_length: 1_000
47
+ # args_to_s_proc: nil # e.g. ->(record) { "record id: #{record.id}" }
23
48
  # args_max_length: 1_000
24
49
  # instance_benchmarks: false
25
50
  # class_benchmarks: false
@@ -28,20 +53,9 @@ module DebugLogging
28
53
  # }
29
54
  # )
30
55
  #
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
56
+ CONFIG_KEYS.each do |key|
57
+ alias_method :"debug_#{key}", :"#{key}"
58
+ end
45
59
 
46
60
  class << self
47
61
  def config_pointer(type, method_to_log)
@@ -52,21 +66,12 @@ module DebugLogging
52
66
  end
53
67
  end
54
68
  def initialize(**options)
55
- @enabled = options.key?(:enabled) ? options[:enabled] : true
56
- @logger = options.key?(:logger) ? options[:logger] : Logger.new($stdout)
57
- @log_level = options.key?(:log_level) ? options[:log_level] : :debug
58
- @multiple_last_hashes = options.key?(:multiple_last_hashes) ? options[:multiple_last_hashes] : false
59
- @last_hash_to_s_proc = options.key?(:last_hash_to_s_proc) ? options[:last_hash_to_s_proc] : nil
60
- @last_hash_max_length = options.key?(:last_hash_max_length) ? options[:last_hash_max_length] : 1_000
61
- @args_max_length = options.key?(:args_max_length) ? options[:args_max_length] : 1_000
62
- @colorized_chain_for_method = options.key?(:colorized_chain_for_method) ? options[:colorized_chain_for_method] : false
63
- @colorized_chain_for_class = options.key?(:colorized_chain_for_class) ? options[:colorized_chain_for_class] : false
64
- @add_invocation_id = options.key?(:add_invocation_id) ? options[:add_invocation_id] : true
65
- @ellipsis = options.key?(:ellipsis) ? options[:ellipsis] : DEFAULT_ELLIPSIS
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
69
+ CONFIG_ATTRS.each do |key|
70
+ send("#{key}=", get_attr_from_options(options, key))
71
+ end
72
+ CONFIG_READERS.each do |key|
73
+ send("#{key}=", get_reader_from_options(options, key))
74
+ end
70
75
  @methods_to_log = []
71
76
  end
72
77
 
@@ -74,7 +79,7 @@ module DebugLogging
74
79
  return unless enabled
75
80
  return unless logger
76
81
 
77
- if block_given?
82
+ if block
78
83
  logger.send(log_level, &block)
79
84
  else
80
85
  logger.send(log_level, message)
@@ -115,26 +120,23 @@ module DebugLogging
115
120
  end
116
121
 
117
122
  def to_hash
118
- {
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
133
- }
123
+ CONFIG_KEYS.each_with_object({}) do |key, hash|
124
+ hash[key] = instance_variable_get("@#{key}")
125
+ end
134
126
  end
135
127
 
136
128
  def register(method_lo_log)
137
129
  @methods_to_log << method_lo_log
138
130
  end
131
+
132
+ private
133
+
134
+ def get_attr_from_options(options, key)
135
+ options.key?(key) ? options[key] : CONFIG_ATTRS_DEFAULTS[key]
136
+ end
137
+
138
+ def get_reader_from_options(options, key)
139
+ options.key?(key) ? options[key] : CONFIG_READERS_DEFAULTS[key]
140
+ end
139
141
  end
140
142
  end