• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pboling / debug_logging / 8114731249

01 Mar 2024 05:11PM UTC coverage: 92.479% (+0.7%) from 91.789%
8114731249

push

github

pboling
🐛 Added missing debug_* methods

136 of 158 branches covered (86.08%)

Branch coverage included in aggregate %.

7 of 11 new or added lines in 2 files covered. (63.64%)

8 existing lines in 3 files now uncovered.

528 of 560 relevant lines covered (94.29%)

136.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.35
/lib/debug_logging.rb
1
# Std Lib
2
require "logger"
1✔
3
require "digest"
1✔
4

5
# External gems
6
require "version_gem"
1✔
7
require "colorized_string"
1✔
8

9
# This gem
10
require "debug_logging/constants"
1✔
11
require "debug_logging/version"
1✔
12
require "debug_logging/errors"
1✔
13
require "debug_logging/configuration"
1✔
14
require "debug_logging/util"
1✔
15
require "debug_logging/finalize"
1✔
16
require "debug_logging/argument_printer"
1✔
17
require "debug_logging/hooks"
1✔
18
require "debug_logging/instance_logger_modulizer"
1✔
19
require "debug_logging/instance_logger"
1✔
20
require "debug_logging/class_logger"
1✔
21

22
####################
23
#                  #
24
# NOTE: The manner this is made to work for class methods is totally different
25
#       than the way this is made to work for instance methods.
26
# NOTE: The instance method manner works on Ruby 2.0+
27
# NOTE: The class method manner works on Ruby 2.1+
28
#                  #
29
####################
30
#                  #
31
# USAGE (see specs)#
32
#                  #
33
#     class Car
34
#
35
#       # adds the helper methods to the class, all are prefixed with debug_*,
36
#       #   except for the logged class method, which comes from extending DebugLogging::ClassLogger
37
#       extend DebugLogging
38
#
39
#       # per class configuration overrides!
40
#       self.debug_class_benchmarks = true
41
#       self.debug_instance_benchmarks = true
42
#
43
#       # For instance methods:
44
#       # Option 1: specify the exact method(s) to add logging to
45
#       extend DebugLogging::InstanceLogger
46
#       i_logged [:drive, :stop]
47
#
48
#       # Provides the `logged` method decorator
49
#       extend DebugLogging::ClassLogger
50
#
51
#       logged def debug_make; new; end
52
#       def design(*args); new; end
53
#       def safety(*args); new; end
54
#       logged :design, :safety
55
#
56
#       def drive(speed); speed; end
57
#       def stop; 0; end
58
#
59
#       # For instance methods:
60
#       # Option 2: add logging to all instance methods defined above (but *not* defined below)
61
#       extend DebugLogging::InstanceLogger
62
#       i_logged instance_methods(false)
63
#
64
#       def will_not_be_logged; false; end
65
#
66
#     end
67
#                  #
68
####################
69

70
module DebugLogging
1✔
71
  # We can't compare with nil to check for no arguments passed as a configuration value,
72
  #   because nil can be an argument passed, hence:
73
  ACTUAL_NOTHING = Object.new
1✔
74

75
  class << self
1✔
76
    def extended(base)
1✔
77
      base.send(:extend, ArgumentPrinter)
187✔
78
      base.send(:include, ArgumentPrinter)
187✔
79
      base.send(:extend, ApiClassMethods)
187✔
80
      base.send(:extend, ConfigClassMethods)
187✔
81
      base.debug_config_reset(Configuration.new(**debug_logging_configuration.to_hash))
187✔
82
      base.class_eval do
187✔
83
        def base.inherited(subclass)
187✔
84
          subclass.debug_config_reset(Configuration.new(**debug_config.to_hash))
9✔
85
        end
86
      end
87
    end
88

89
    #### API ####
90

91
    # For single statement global config in an initializer
92
    # e.g. DebugLogging.configuration.ellipsis = "..."
93
    def configuration
1✔
94
      self.debug_logging_configuration ||= Configuration.new
24✔
95
    end
96

97
    # For global config in an initializer with a block
98
    def configure
1✔
99
      yield(configuration)
9✔
100
    end
101
  end
102

103
  module ApiClassMethods
1✔
104
    # Not used by this gem internally, but provides an external interface for
105
    #   classes to also use this logging tool directly,
106
    #   with configured options like benchmarking, colors, or leg level.
107
    def debug_log(message = nil, config_proxy = nil, &block)
1✔
108
      # If a, instance-method-level, or class-method-level custom config is not
109
      #   passed in, then fall back to the class' default config, which is a
110
      #   potentially customized copy of the default config for the whole app.
111
      config_proxy ||= debug_config
3✔
112
      config_proxy.log(message, &block)
3✔
113
    end
114

115
    # There are times when the class will need access to the configuration object,
116
    #   such as to override it per instance method
117
    def debug_config
1✔
118
      @debug_logging_configuration
239✔
119
    end
120
  end
121

122
  #### CONFIG ####
123
  class << self
1✔
124
    attr_accessor :debug_logging_configuration
1✔
125
  end
126

127
  module ConfigClassMethods
1✔
128
    include Constants
1✔
129

130
    # For per-class config with a block
131
    def debug_logging_configure
1✔
132
      @debug_logging_configuration ||= Configuration.new
2✔
133
      yield(@debug_logging_configuration)
2✔
134
    end
135

136
    def debug_config_reset(config = Configuration.new)
1✔
137
      @debug_logging_configuration = config
197✔
138
    end
139

140
    def debug_enabled
1✔
141
      @debug_logging_configuration.enabled
×
142
    end
143

144
    def debug_enabled=(value)
1✔
145
      @debug_logging_configuration.enabled = value
×
146
    end
147

148
    def debug_logger
1✔
149
      @debug_logging_configuration.logger
5✔
150
    end
151

152
    def debug_logger=(logger)
1✔
153
      @debug_logging_configuration.logger = logger
4✔
154
    end
155

156
    def debug_log_level
1✔
157
      @debug_logging_configuration.log_level
5✔
158
    end
159

160
    def debug_log_level=(log_level)
1✔
161
      @debug_logging_configuration.log_level = log_level
1✔
162
    end
163

164
    def debug_multiple_last_hashes
1✔
165
      @debug_logging_configuration.multiple_last_hashes
3✔
166
    end
167

168
    def debug_multiple_last_hashes=(multiple_last_hashes)
1✔
169
      @debug_logging_configuration.multiple_last_hashes = multiple_last_hashes
5✔
170
    end
171

172
    def debug_last_hash_to_s_proc
1✔
173
      @debug_logging_configuration.last_hash_to_s_proc
4✔
174
    end
175

176
    def debug_last_hash_to_s_proc=(last_hash_to_s_proc)
1✔
177
      @debug_logging_configuration.last_hash_to_s_proc = last_hash_to_s_proc
19✔
178
    end
179

180
    def debug_last_hash_max_length
1✔
181
      @debug_logging_configuration.last_hash_max_length
5✔
182
    end
183

184
    def debug_last_hash_max_length=(last_hash_max_length)
1✔
185
      @debug_logging_configuration.last_hash_max_length = last_hash_max_length
20✔
186
    end
187

188
    def debug_args_to_s_proc
1✔
189
      @debug_logging_configuration.args_to_s_proc
2✔
190
    end
191

192
    def debug_args_to_s_proc=(args_to_s_proc)
1✔
193
      @debug_logging_configuration.args_to_s_proc = args_to_s_proc
1✔
194
    end
195

196
    def debug_args_max_length
1✔
197
      @debug_logging_configuration.args_max_length
8✔
198
    end
199

200
    def debug_args_max_length=(args_max_length)
1✔
201
      @debug_logging_configuration.args_max_length = args_max_length
19✔
202
    end
203

204
    def debug_instance_benchmarks
1✔
205
      @debug_logging_configuration.instance_benchmarks
16✔
206
    end
207

208
    def debug_instance_benchmarks=(instance_benchmarks)
1✔
209
      @debug_logging_configuration.instance_benchmarks = instance_benchmarks
42✔
210
    end
211

212
    def debug_class_benchmarks
1✔
213
      @debug_logging_configuration.class_benchmarks
9✔
214
    end
215

216
    def debug_class_benchmarks=(class_benchmarks)
1✔
217
      @debug_logging_configuration.class_benchmarks = class_benchmarks
26✔
218
    end
219

220
    def debug_colorized_chain_for_method
1✔
221
      @debug_logging_configuration.colorized_chain_for_method
3✔
222
    end
223

224
    def debug_colorized_chain_for_method=(colorized_chain_for_method)
1✔
225
      @debug_logging_configuration.colorized_chain_for_method = colorized_chain_for_method
1✔
226
    end
227

228
    def debug_colorized_chain_for_class
1✔
229
      @debug_logging_configuration.colorized_chain_for_class
3✔
230
    end
231

232
    def debug_colorized_chain_for_class=(colorized_chain_for_class)
1✔
233
      @debug_logging_configuration.colorized_chain_for_class = colorized_chain_for_class
1✔
234
    end
235

236
    def debug_add_timestamp
1✔
NEW
237
      @debug_logging_configuration.add_timestamp
×
238
    end
239

240
    def debug_add_timestamp=(add_timestamp)
1✔
NEW
241
      @debug_logging_configuration.add_timestamp = add_timestamp
×
242
    end
243

244
    def debug_time_formatter_proc
1✔
NEW
245
      @debug_logging_configuration.time_formatter_proc
×
246
    end
247

248
    def debug_time_formatter_proc=(time_formatter_proc)
1✔
NEW
249
      @debug_logging_configuration.time_formatter_proc = time_formatter_proc
×
250
    end
251

252
    def debug_add_invocation_id
1✔
253
      @debug_logging_configuration.add_invocation_id
20✔
254
    end
255

256
    def debug_add_invocation_id=(add_invocation_id)
1✔
257
      @debug_logging_configuration.add_invocation_id = add_invocation_id
65✔
258
    end
259

260
    def debug_ellipsis
1✔
261
      @debug_logging_configuration.ellipsis
8✔
262
    end
263

264
    def debug_ellipsis=(ellipsis)
1✔
265
      @debug_logging_configuration.ellipsis = ellipsis
25✔
266
    end
267

268
    def debug_mark_scope_exit
1✔
269
      @debug_logging_configuration.mark_scope_exit
3✔
270
    end
271

272
    def debug_mark_scope_exit=(mark_scope_exit)
1✔
273
      @debug_logging_configuration.mark_scope_exit = mark_scope_exit
1✔
274
    end
275

276
    def debug_add_payload
1✔
277
      @debug_logging_configuration.add_payload
3✔
278
    end
279

280
    def debug_add_payload=(add_payload)
1✔
281
      @debug_logging_configuration.add_payload = add_payload
1✔
282
    end
283

284
    def debug_payload_max_length
1✔
285
      @debug_logging_configuration.payload_max_length
3✔
286
    end
287

288
    def debug_payload_max_length=(payload_max_length)
1✔
289
      @debug_logging_configuration.payload_max_length = payload_max_length
1✔
290
    end
291

292
    def debug_error_handler_proc
1✔
293
      @debug_logging_configuration.error_handler_proc
3✔
294
    end
295

296
    def debug_error_handler_proc=(error_handler_proc)
1✔
297
      @debug_logging_configuration.error_handler_proc = error_handler_proc
1✔
298
    end
299
  end
300

301
  self.debug_logging_configuration = Configuration.new # setup defaults
1✔
302
end
303

304
DebugLogging::Version.class_eval do
1✔
305
  extend VersionGem::Basic
1✔
306
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc