debug_logging 3.1.4 → 3.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3b22568d767ce27e04784bc0c5884d256dac350db70c0a55e4c3d0111b00e4b
4
- data.tar.gz: a3d0a5b1fa588b2b9c7596ce52e3e46b04724a460cf825c1393c0bd358117fc2
3
+ metadata.gz: 77770fe7ac99f46e73cbcbc12eeaef1ad69978ab9880ae8758758d203c9e1267
4
+ data.tar.gz: 9e09d725eba529f29103468581fb8718eee02946d02a97cbeb444a7df946bfc1
5
5
  SHA512:
6
- metadata.gz: bd581ea6179d7cc21fa501f0ab7870a2e9d25a664ae544999aa0f703fc351955b7fb5a6c1eacafb3946dd6dde7e6c246a3bebac37af07572c21adc4d0a169e15
7
- data.tar.gz: a6ae636072c6e60d19831b4ffa1012b4d7dfd1c67a7e78384f7ad6c004e57e729f83dcd24043938a0c03049722fe477bef7ed4ac9c7bce9c7e80ffc76b48e698
6
+ metadata.gz: e26b9a71370c7f87ffc03a202cee289a0510f9e552dba96edd7034fad32f16c6d4b83da7b64d2f4e516d81af0743b3a70f21896f9e08bd87f68c962e8aeb12b9
7
+ data.tar.gz: e85efa9c27319c63fc3d9923602ac901eb1ca32ecbe726f0cdd54eec656706426b17a69d3ee2b1bb96f76b8b801569b7b54b544966903f9316603da54c3ff64f
@@ -163,6 +163,14 @@ module DebugLogging
163
163
  @debug_logging_configuration.last_hash_max_length = last_hash_max_length
164
164
  end
165
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
+
166
174
  def debug_args_max_length
167
175
  @debug_logging_configuration.args_max_length
168
176
  end
@@ -45,49 +45,90 @@ module DebugLogging
45
45
 
46
46
  add_args_ellipsis = false
47
47
  if config_proxy.debug_last_hash_to_s_proc && args[-1].is_a?(Hash)
48
- add_last_hash_ellipsis = false
48
+ add_other_args_ellipsis = false
49
49
  if args.length > 1
50
50
  if config_proxy.debug_multiple_last_hashes
51
51
  last_hash_args, other_args = args.partition do |arg|
52
52
  arg.is_a?(Hash)
53
53
  end
54
- 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
55
68
  # On the debug_multiple_last_hashes truthy branch we don't print the ellipsis after regular args
56
- # 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)
57
70
  # ...join(", ").tap {|x| _add_args_ellipsis = x.length > config_proxy.debug_args_max_length}
58
71
  last_hash_args_string = last_hash_args.map do |arg|
59
72
  arr = []
60
- arr << config_proxy.debug_last_hash_to_s_proc.call(arg).to_s
61
- .tap do |x|
62
- add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length
63
- end
64
- if add_last_hash_ellipsis
65
- arr[-1] = arr[-1][0..(config_proxy.debug_last_hash_max_length)]
66
- arr << config_proxy.debug_ellipsis
67
- 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
68
81
  arr
69
82
  end.flatten.join(', ')
70
83
  printed_args += other_args_string if other_args_string
71
84
  printed_args += ', ' if !other_args_string.empty? && !last_hash_args_string.empty?
72
85
  printed_args += last_hash_args_string if last_hash_args_string && !last_hash_args_string.empty?
73
86
  else
74
- printed_args += args[0..-2].map(&:inspect).join(', ').tap do |x|
75
- add_args_ellipsis = x.length > config_proxy.debug_args_max_length
76
- end[0..(config_proxy.debug_args_max_length)]
77
- printed_args += config_proxy.debug_ellipsis if add_args_ellipsis
78
- printed_args += ", #{config_proxy.debug_last_hash_to_s_proc.call(args[-1]).tap do |x|
79
- add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length
80
- end[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}"
81
110
  printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
82
111
  end
83
112
  else
84
- printed_args += String(config_proxy.debug_last_hash_to_s_proc.call(args[0])).tap do |x|
85
- add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length
86
- end[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
87
120
  printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
88
121
  end
89
122
  else
90
- printed_args += if args.length == 1 && args[0].is_a?(Hash)
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)
91
132
  # handle double splat
92
133
  ("**#{args.map(&:inspect).join(', ').tap do |x|
93
134
  add_args_ellipsis = x.length > config_proxy.debug_args_max_length
@@ -102,6 +143,19 @@ module DebugLogging
102
143
  "(#{printed_args})"
103
144
  end
104
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
+
105
159
  def debug_payload_to_s(payload: nil, config_proxy: nil)
106
160
  return '' unless payload && config_proxy
107
161
 
@@ -110,7 +164,16 @@ module DebugLogging
110
164
  when true
111
165
  payload.inspect
112
166
  else
113
- config_proxy.debug_add_payload.call(**payload)
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
114
177
  end
115
178
  else
116
179
  ''
@@ -10,13 +10,15 @@ module DebugLogging
10
10
  multiple_last_hashes: false,
11
11
  last_hash_to_s_proc: nil,
12
12
  last_hash_max_length: 1_000,
13
+ args_to_s_proc: nil,
13
14
  args_max_length: 1_000,
14
15
  colorized_chain_for_method: false,
15
16
  colorized_chain_for_class: false,
16
17
  add_invocation_id: true,
17
18
  ellipsis: DEFAULT_ELLIPSIS,
18
19
  mark_scope_exit: false,
19
- add_payload: true
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
20
22
  }.freeze
21
23
  CONFIG_ATTRS = CONFIG_ATTRS_DEFAULTS.keys
22
24
  CONFIG_READERS_DEFAULTS = {
@@ -42,6 +44,7 @@ module DebugLogging
42
44
  # log_level: :debug # at what level do the messages created by this gem sent at?
43
45
  # last_hash_to_s_proc: nil # e.g. ->(hash) { "keys: #{hash.keys}" }
44
46
  # last_hash_max_length: 1_000
47
+ # args_to_s_proc: nil # e.g. ->(record) { "record id: #{record.id}" }
45
48
  # args_max_length: 1_000
46
49
  # instance_benchmarks: false
47
50
  # class_benchmarks: false
@@ -17,4 +17,4 @@ module DebugLogging
17
17
  end
18
18
  end
19
19
  end
20
- end
20
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DebugLogging
4
- VERSION = '3.1.4'
4
+ VERSION = '3.1.5'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debug_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.4
4
+ version: 3.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Boling