debug_logging 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -5
- data/debug_logging.gemspec +1 -0
- data/lib/debug_logging/argument_printer.rb +30 -1
- data/lib/debug_logging/class_logger.rb +9 -7
- data/lib/debug_logging/configuration.rb +8 -0
- data/lib/debug_logging/instance_logger_modulizer.rb +6 -5
- data/lib/debug_logging/version.rb +1 -1
- data/lib/debug_logging.rb +13 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3175dd95278445508de7ab86b58993c7758777a9
|
4
|
+
data.tar.gz: 2f63b6b6728dbd590fe43f099c4ca0076ebc90de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7044a9f9f938a180fd8284c665f0d5dc3d4cbd0f61cc7f92e91803cc7bb9cdc024d759981866c15269d098f1065b217dd44c529dee52cb43a219d291cb0376d6
|
7
|
+
data.tar.gz: a842c4d45b4a5ab522776841a07ff29c0a8569733bbc7640871df496e5ba3ae38994fde4324d5f915ceabc0f02f5422d07c04f30c8ea96b459de974982f1f387
|
data/README.md
CHANGED
@@ -49,14 +49,17 @@ Recommend creating `config/initializers/debug_logging.rb` with:
|
|
49
49
|
|
50
50
|
```ruby
|
51
51
|
# Showing the defaults
|
52
|
-
DebugLogging.configuration.logger = Logger.new(STDOUT) # you probably want to override to be the Rails.logger
|
52
|
+
DebugLogging.configuration.logger = Logger.new(STDOUT) # you probably want to override to be the Rails.logger, and if so you can't set it in the initializer, as it needs to be set after Rails.logger is set.
|
53
53
|
DebugLogging.configuration.log_level = :debug # at what level do the messages created by this gem sent at?
|
54
|
+
DebugLogging.configuration.multiple_last_hashes = false # pass every hash argument to last_hash_to_s_proc?
|
54
55
|
DebugLogging.configuration.last_hash_to_s_proc = nil # e.g. ->(hash) { "keys: #{hash.keys}" }
|
55
56
|
DebugLogging.configuration.last_hash_max_length = 1_000
|
56
57
|
DebugLogging.configuration.args_max_length = 1_000
|
57
58
|
DebugLogging.configuration.instance_benchmarks = false
|
58
59
|
DebugLogging.configuration.class_benchmarks = false
|
59
|
-
DebugLogging.configuration.
|
60
|
+
DebugLogging.configuration.colorized_chain_for_method = false # e.g. ->(colorized_string) { colorized_string.red.on_blue.underline }
|
61
|
+
DebugLogging.configuration.colorized_chain_for_class = false # e.g. ->(colorized_string) { colorized_string.colorize(:light_blue ).colorize( :background => :red) }
|
62
|
+
DebugLogging.configuration.add_invocation_id = true # identify a method call uniquely in a log, pass a proc for colorization, e.g. ->(colorized_string) { colorized_string.light_black }
|
60
63
|
DebugLogging.configuration.ellipsis = " ✂️ …".freeze
|
61
64
|
```
|
62
65
|
|
@@ -64,14 +67,17 @@ If you prefer to use the block style:
|
|
64
67
|
|
65
68
|
```ruby
|
66
69
|
DebugLogging.configure do |config|
|
67
|
-
config.logger = Logger.new(STDOUT) # probably want to override to be the Rails.logger
|
70
|
+
config.logger = Logger.new(STDOUT) # probably want to override to be the Rails.logger, and if so you can't set it in the initializer, as it needs to be set after Rails.logger is set.
|
68
71
|
config.log_level = :debug # at what level do the messages created by this gem sent at?
|
72
|
+
config.multiple_last_hashes = false # pass every hash argument to last_hash_to_s_proc?
|
69
73
|
config.last_hash_to_s_proc = nil # e.g. ->(hash) { "keys: #{hash.keys}" }
|
70
74
|
config.last_hash_max_length = 1_000
|
71
75
|
config.args_max_length = 1_000
|
72
76
|
config.instance_benchmarks = false
|
73
77
|
config.class_benchmarks = false
|
74
|
-
config.
|
78
|
+
config.colorized_chain_for_method = false # e.g. ->(colorized_string) { colorized_string.red.on_blue.underline }
|
79
|
+
config.colorized_chain_for_class = false # e.g. ->(colorized_string) { colorized_string.colorize(:light_blue ).colorize( :background => :red) }
|
80
|
+
config.add_invocation_id = true # identify a method call uniquely in a log, pass a proc for colorization, e.g. ->(colorized_string) { colorized_string.light_black }
|
75
81
|
config.ellipsis = " ✂️ …".freeze
|
76
82
|
end
|
77
83
|
```
|
@@ -80,8 +86,13 @@ end
|
|
80
86
|
Just prepend `debug_` to any config value you want to override in a class.
|
81
87
|
|
82
88
|
**All** of the above **config** is **inheritable** and **configurable** at the **per-method** level as well!
|
83
|
-
Just send along a hash of the config options when you call `logged` or `include DebugLogging::InstanceLogger.new(i_methods: [:drive, :stop])`. See the example class below
|
89
|
+
Just send along a hash of the config options when you call `logged` or `include DebugLogging::InstanceLogger.new(i_methods: [:drive, :stop])`. See the example class below, and the specs.
|
84
90
|
|
91
|
+
**NOTE ON** `Rails.logger` - It will probably be nil in your initializer, so setting the `config.logger` to `Rails.logger` there will result in setting it to `nil`, which means the default will end up being used: `Logger.new(STDOUT)`. Instead just config the logger in your application.rb, or anytime later, but *before your classes get loaded* and start inheriting the config:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
DebugLogging.configuration.logger = Rails.logger
|
95
|
+
```
|
85
96
|
|
86
97
|
Every time a method is called, get logs, optionally with arguments, a benchmarck, and a unique invocation identifier:
|
87
98
|
|
data/debug_logging.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
spec.require_paths = ["lib"]
|
22
22
|
|
23
|
+
spec.add_runtime_dependency "colorize", "~> 0.8"
|
23
24
|
spec.add_development_dependency "rspec-pending_for"
|
24
25
|
spec.add_development_dependency "byebug", "~> 9.0"
|
25
26
|
spec.add_development_dependency "bundler", "~> 1.14"
|
@@ -1,6 +1,35 @@
|
|
1
1
|
module DebugLogging
|
2
2
|
module ArgumentPrinter
|
3
|
-
def
|
3
|
+
def debug_benchmark_to_s(tms: nil)
|
4
|
+
"completed in #{sprintf("%f", tms.real)}s (#{sprintf("%f", tms.total)}s CPU)"
|
5
|
+
end
|
6
|
+
def debug_invocation_id_to_s(args: nil, config_proxy: nil)
|
7
|
+
if config_proxy.debug_add_invocation_id
|
8
|
+
invocation = " ~#{args.object_id}@#{sprintf("%#-21a", Time.now.to_f)[4..(-4)]}~"
|
9
|
+
case config_proxy.debug_add_invocation_id
|
10
|
+
when true then
|
11
|
+
invocation
|
12
|
+
else
|
13
|
+
config_proxy.debug_add_invocation_id.call(ColorizedString[invocation])
|
14
|
+
end
|
15
|
+
else
|
16
|
+
""
|
17
|
+
end
|
18
|
+
end
|
19
|
+
def debug_invocation_to_s(klass: nil, separator: nil, method_to_log: nil, config_proxy: nil)
|
20
|
+
klass_string = if config_proxy.debug_colorized_chain_for_class
|
21
|
+
config_proxy.debug_colorized_chain_for_class.call(ColorizedString[klass.to_s])
|
22
|
+
else
|
23
|
+
klass.to_s
|
24
|
+
end
|
25
|
+
method_string = if config_proxy.debug_colorized_chain_for_method
|
26
|
+
config_proxy.debug_colorized_chain_for_method.call(ColorizedString[method_to_log.to_s])
|
27
|
+
else
|
28
|
+
method_to_log.to_s
|
29
|
+
end
|
30
|
+
"#{klass_string}#{separator}#{method_string}"
|
31
|
+
end
|
32
|
+
def debug_signature_to_s(args: nil, config_proxy: nil)
|
4
33
|
printed_args = ""
|
5
34
|
add_args_ellipsis = false
|
6
35
|
if config_proxy.debug_last_hash_to_s_proc && args[-1].is_a?(Hash)
|
@@ -12,22 +12,24 @@ module DebugLogging
|
|
12
12
|
method_to_log = method_to_log.to_sym
|
13
13
|
original_method = method(method_to_log)
|
14
14
|
(class << self; self; end).class_eval do
|
15
|
-
define_method(method_to_log) do |*args|
|
15
|
+
define_method(method_to_log) do |*args, &block|
|
16
16
|
config_proxy = if opts
|
17
17
|
Configuration.new(**(debug_config.to_hash.merge(opts)))
|
18
18
|
else
|
19
19
|
self
|
20
20
|
end
|
21
21
|
method_return_value = nil
|
22
|
-
|
23
|
-
|
22
|
+
log_prefix = debug_invocation_to_s(klass: self.to_s, separator: ".", method_to_log: method_to_log, config_proxy: config_proxy)
|
23
|
+
signature = debug_signature_to_s(args: args, config_proxy: config_proxy)
|
24
|
+
invocation_id = debug_invocation_id_to_s(args: args, config_proxy: config_proxy)
|
25
|
+
debug_log "#{log_prefix}#{signature}#{invocation_id}"
|
24
26
|
if config_proxy.debug_class_benchmarks
|
25
|
-
|
26
|
-
method_return_value = original_method.call(*args)
|
27
|
+
tms = Benchmark.measure do
|
28
|
+
method_return_value = original_method.call(*args, &block)
|
27
29
|
end
|
28
|
-
debug_log "#{
|
30
|
+
debug_log "#{log_prefix} #{debug_benchmark_to_s(tms: tms)}#{invocation_id}"
|
29
31
|
else
|
30
|
-
method_return_value = original_method.call(*args)
|
32
|
+
method_return_value = original_method.call(*args, &block)
|
31
33
|
end
|
32
34
|
method_return_value
|
33
35
|
end
|
@@ -9,6 +9,8 @@ module DebugLogging
|
|
9
9
|
attr_accessor :args_max_length
|
10
10
|
attr_accessor :instance_benchmarks
|
11
11
|
attr_accessor :class_benchmarks
|
12
|
+
attr_accessor :colorized_chain_for_method
|
13
|
+
attr_accessor :colorized_chain_for_class
|
12
14
|
attr_accessor :add_invocation_id
|
13
15
|
attr_accessor :ellipsis
|
14
16
|
# alias the readers to the debug_* prefix so an instance of this class
|
@@ -37,6 +39,8 @@ module DebugLogging
|
|
37
39
|
alias :debug_args_max_length :args_max_length
|
38
40
|
alias :debug_instance_benchmarks :instance_benchmarks
|
39
41
|
alias :debug_class_benchmarks :class_benchmarks
|
42
|
+
alias :debug_colorized_chain_for_method :colorized_chain_for_method
|
43
|
+
alias :debug_colorized_chain_for_class :colorized_chain_for_class
|
40
44
|
alias :debug_add_invocation_id :add_invocation_id
|
41
45
|
alias :debug_ellipsis :ellipsis
|
42
46
|
def initialize(**options)
|
@@ -48,6 +52,8 @@ module DebugLogging
|
|
48
52
|
@args_max_length = options.key?(:args_max_length) ? options[:args_max_length] : 1_000
|
49
53
|
@instance_benchmarks = options.key?(:instance_benchmarks) ? options[:instance_benchmarks] : false
|
50
54
|
@class_benchmarks = options.key?(:class_benchmarks) ? options[:class_benchmarks] : false
|
55
|
+
@colorized_chain_for_method = options.key?(:colorized_chain_for_method) ? options[:colorized_chain_for_method] : false
|
56
|
+
@colorized_chain_for_class = options.key?(:colorized_chain_for_class) ? options[:colorized_chain_for_class] : false
|
51
57
|
@add_invocation_id = options.key?(:add_invocation_id) ? options[:add_invocation_id] : true
|
52
58
|
@ellipsis = options.key?(:ellipsis) ? options[:ellipsis] : DEFAULT_ELLIPSIS
|
53
59
|
end
|
@@ -69,6 +75,8 @@ module DebugLogging
|
|
69
75
|
args_max_length: args_max_length,
|
70
76
|
instance_benchmarks: instance_benchmarks,
|
71
77
|
class_benchmarks: class_benchmarks,
|
78
|
+
colorized_chain_for_method: colorized_chain_for_method,
|
79
|
+
colorized_chain_for_class: colorized_chain_for_class,
|
72
80
|
add_invocation_id: add_invocation_id,
|
73
81
|
ellipsis: ellipsis
|
74
82
|
}
|
@@ -11,14 +11,15 @@ module DebugLogging
|
|
11
11
|
self.class
|
12
12
|
end
|
13
13
|
method_return_value = nil
|
14
|
-
log_prefix =
|
15
|
-
|
16
|
-
|
14
|
+
log_prefix = self.class.debug_invocation_to_s(klass: self.class.to_s, separator: "#", method_to_log: method_to_log, config_proxy: config_proxy)
|
15
|
+
signature = self.class.debug_signature_to_s(args: args, config_proxy: config_proxy)
|
16
|
+
invocation_id = self.class.debug_invocation_id_to_s(args: args, config_proxy: config_proxy)
|
17
|
+
self.class.debug_log "#{log_prefix}#{signature}#{invocation_id}"
|
17
18
|
if config_proxy.debug_instance_benchmarks
|
18
|
-
|
19
|
+
tms = Benchmark.measure do
|
19
20
|
method_return_value = super(*args, &block)
|
20
21
|
end
|
21
|
-
self.class.debug_log "#{log_prefix}
|
22
|
+
self.class.debug_log "#{log_prefix} #{self.class.debug_benchmark_to_s(tms: tms)}#{invocation_id}"
|
22
23
|
else
|
23
24
|
method_return_value = super(*args, &block)
|
24
25
|
end
|
data/lib/debug_logging.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "logger"
|
2
|
+
require "colorized_string"
|
2
3
|
require "debug_logging/version"
|
3
4
|
require "debug_logging/configuration"
|
4
5
|
require "debug_logging/argument_printer"
|
@@ -140,6 +141,18 @@ module DebugLogging
|
|
140
141
|
def debug_class_benchmarks=(class_benchmarks)
|
141
142
|
@debug_logging_configuration.class_benchmarks = class_benchmarks
|
142
143
|
end
|
144
|
+
def debug_colorized_chain_for_method
|
145
|
+
@debug_logging_configuration.colorized_chain_for_method
|
146
|
+
end
|
147
|
+
def debug_colorized_chain_for_method=(colorized_chain_for_method)
|
148
|
+
@debug_logging_configuration.colorized_chain_for_method = colorized_chain_for_method
|
149
|
+
end
|
150
|
+
def debug_colorized_chain_for_class
|
151
|
+
@debug_logging_configuration.colorized_chain_for_class
|
152
|
+
end
|
153
|
+
def debug_colorized_chain_for_class=(colorized_chain_for_class)
|
154
|
+
@debug_logging_configuration.colorized_chain_for_class = colorized_chain_for_class
|
155
|
+
end
|
143
156
|
def debug_add_invocation_id
|
144
157
|
@debug_logging_configuration.add_invocation_id
|
145
158
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: debug_logging
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Boling
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.8'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec-pending_for
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|