k_log 0.0.27 → 0.0.32

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2035ee429ccfbb7cd0307d243e9b8bd303f44444d1b356c28ad079bc8219de8f
4
- data.tar.gz: c7eeff9ee82d1514859052d264cfcb06216ad939f3c215c34dfee6f64ef0d21f
3
+ metadata.gz: 0fa223a72a5c70dcb1c311f1da3c1e5f370c64847942a2e4e8dccf3abff05fd1
4
+ data.tar.gz: ab980569c01e1c99a99c157623f92a75d08a6af82163d929130f8bc1080658b2
5
5
  SHA512:
6
- metadata.gz: f12d2c0fae01798339647d3905fefe8e5e63049d074965c04b878e3ba4df7b8571cba637877395f8fa033d2c780d53bfc4aaa2da605af647908fdb7e29bd1e3a
7
- data.tar.gz: 3d780ecca23bac05227ff96629c464f3ecf2ed61f908a6f28145e9b00786fae625e37d751210ccaaf03d613e1683bbfbb15cf009d35e57909ac3110488b34b0f
6
+ metadata.gz: f1fe198aee1f8dab3e7fad5676eb209abb8e034162b26a52edc36c107dfc7606f1db5f18bbbafa8eaf120499ebc18cea302eac8aa6acadd3f2debf01dc23c1fd
7
+ data.tar.gz: 62f28c9a49847aaec1896759ff3ab0d9c367e84ff67c4ef80baa018d32bd25ac0cc9e4c30704d3aba9a660b38443ced2e4f6ba4418e065e6adc8d68853a3c177
@@ -91,6 +91,16 @@ module KLog
91
91
  rescue StandardError => e
92
92
  log.exception(e)
93
93
  end
94
+ begin
95
+ raise 'Here is an error'
96
+ rescue StandardError => e
97
+ log.exception(e, style: :message)
98
+ end
99
+ begin
100
+ raise 'Here is an error'
101
+ rescue StandardError => e
102
+ log.exception(e, style: :short)
103
+ end
94
104
  end
95
105
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
96
106
 
@@ -20,8 +20,10 @@ module KLog
20
20
 
21
21
  msg = msg.is_a?(String) ? msg : msg.inspect
22
22
 
23
+ # "%<time>s %<severity>s %<message>s\n", {
24
+
23
25
  format(
24
- "%<time>s %<severity>s %<message>s\n", {
26
+ "%<severity>s %<message>s\n", {
25
27
  time: Time.now.strftime('%d|%H:%M:%S'),
26
28
  severity: severity_value,
27
29
  message: msg
@@ -11,6 +11,8 @@ module KLog
11
11
  attr_reader :heading
12
12
  attr_reader :heading_type
13
13
  attr_reader :line_width
14
+ attr_reader :key_width
15
+ attr_reader :show_array_count
14
16
  attr_reader :graph
15
17
  attr_reader :formatter
16
18
  attr_reader :convert_data_to
@@ -32,7 +34,9 @@ module KLog
32
34
  # @option opts [String] :heading Log heading using logger.dynamic_heading
33
35
  # @option opts [String] :heading_type :heading, :subheading, :section_heading
34
36
  # @option opts [String] :line_width line width defaults to 80, but can be overridden here
37
+ # @option opts [String] :key_width key width defaults to 30, but can be overridden here
35
38
  # @option opts [String] :formatter is a complex configuration for formatting different data within the structure
39
+ # @option opts [Symbol] :convert_data_to (:raw, open_struct)
36
40
  def initialize(opts)
37
41
  @indent = opts[:indent] || ' '
38
42
  @title = opts[:title]
@@ -48,6 +52,8 @@ module KLog
48
52
  @convert_data_to = opts[:convert_data_to] || :raw # by default leave data as is
49
53
 
50
54
  @line_width = opts[:line_width] || 80
55
+ @key_width = opts[:key_width] || 30
56
+ @show_array_count = opts[:show_array_count] || false
51
57
  @output_as = opts[:output_as] || [:console]
52
58
  @output_as = [@output_as] unless @output_as.is_a?(Array)
53
59
  @output_file = opts[:output_file]
@@ -102,12 +108,19 @@ module KLog
102
108
 
103
109
  # format_config = @formatter[:_root] if format_config.nil? && @recursion_depth.zero?
104
110
 
111
+ def data_enumerator(data)
112
+ return data.attributes if data.respond_to?(:attributes)
113
+
114
+ data
115
+ end
116
+
105
117
  def log_data(data)
106
- data.each_pair do |k, v|
118
+ data_enumerator(data).each_pair do |k, v|
107
119
  key = k.is_a?(String) ? k.to_sym : k
108
120
 
109
121
  graph_path.push(key)
110
- @graph_node = GraphNode.for(graph, graph_path)
122
+ @graph_node = GraphNode.for(self, graph, graph_path)
123
+ # @graph_node.debug
111
124
  # l.kv 'key', "#{key.to_s.ljust(15)}#{graph_node.skip?.to_s.ljust(6)}#{@recursion_depth}"
112
125
 
113
126
  if graph_node.skip?
@@ -116,21 +129,26 @@ module KLog
116
129
  next
117
130
  end
118
131
 
132
+ # xxxx.pry if graph_node.pry_at?(:before_value) # 'puts xmen'
133
+
119
134
  value = graph_node.transform? ? graph_node.transform(v) : v
120
135
 
121
- case value
122
- when OpenStruct
136
+ # xxxx.pry if graph_node.pry_at?(:after_value) # 'puts xmen'
137
+ if value.is_a?(OpenStruct) || value.respond_to?(:attributes)
138
+
123
139
  # l.kv 'go', 'open struct ->'
140
+ # xxxx.pry if graph_node.pry_at?(:before_structure) # 'puts xmen'
124
141
  log_structure(key, value)
125
142
  # l.kv 'go', 'open struct <-'
126
- when Array
143
+ elsif value.is_a?(Array)
127
144
  # l.kv 'go', 'array ->'
128
145
  log_array(key, value)
129
146
  # l.kv 'go', 'array <-'
130
147
  else
131
148
  # l.kv 'go', 'value ->'
149
+ # xxxx.pry if graph_node.pry_at?(:before_kv) # 'puts xmen'
132
150
  log_heading(graph_node.heading, graph_node.heading_type) if graph_node.heading
133
- add_line KLog::LogHelper.kv "#{@indent_label}#{key}", value
151
+ add_line KLog::LogHelper.kv("#{@indent_label}#{key}", value, key_width)
134
152
  # l.kv 'go', 'value <-'
135
153
  end
136
154
 
@@ -155,13 +173,15 @@ module KLog
155
173
  end
156
174
 
157
175
  def log_array(key, array)
158
- # return if items.length.zero? && graph_node.skip_empty?
176
+ # xxxx.pry if graph_node.pry_at?(:before_array) # 'puts xmen'
159
177
 
160
178
  items = array.clone
161
179
  items.select! { |item| graph_node.filter(item) } if graph_node.filter?
162
180
  items = items.take(graph_node.take) if graph_node.limited?
163
181
  items.sort!(&graph_node.sort) if graph_node.sort?
164
182
 
183
+ # xxxx.pry if graph_node.pry_at?(:before_array_print) # 'puts xmen'
184
+
165
185
  return if items.length.zero? && graph_node.skip_empty?
166
186
 
167
187
  log_heading(graph_node.heading, graph_node.heading_type) if graph_node.heading
@@ -170,9 +190,12 @@ module KLog
170
190
  add_line KLog::LogHelper.kv "#{@indent_label}#{key}", items.map(&:to_s).join(', ')
171
191
  else
172
192
  table_print items, tp_columns(items)
193
+
194
+ # NEED SUPPORT FOR A configured ARRAY COUNT with width and label
195
+ add_line KLog::LogHelper.kv key.to_s, items.count if show_array_count
173
196
  end
174
- rescue StandardError
175
- #
197
+ rescue StandardError => e
198
+ KLog.logger.exception(e)
176
199
  end
177
200
 
178
201
  def table_print(items, columns)
@@ -228,6 +251,7 @@ module KLog
228
251
  def render_output
229
252
  puts content if output_as.include?(:console)
230
253
  File.write(output_file, clean_content) if output_as.include?(:file) && output_file
254
+ # content
231
255
  end
232
256
 
233
257
  # convert_data_to: :open_struct
@@ -304,6 +328,7 @@ module KLog
304
328
  end
305
329
 
306
330
  class GraphNode
331
+ attr_reader :log_structure
307
332
  attr_accessor :config
308
333
 
309
334
  class << self
@@ -311,7 +336,7 @@ module KLog
311
336
  @null ||= OpenStruct.new
312
337
  end
313
338
 
314
- def for(graph, graph_path)
339
+ def for(log_structure, graph, graph_path)
315
340
  # node_config = graph_path.inject(graph, :send) # (uses deep nesting, but fails when nil is returned) https://stackoverflow.com/questions/15862455/ruby-nested-send
316
341
  # node.nil? ? null : node.send(name) || null
317
342
  node_config = graph_path.reduce(graph) do |node, name|
@@ -321,12 +346,14 @@ module KLog
321
346
 
322
347
  result
323
348
  end
349
+ # puts node_config
324
350
 
325
- new(node_config)
351
+ new(log_structure, node_config)
326
352
  end
327
353
  end
328
354
 
329
- def initialize(config)
355
+ def initialize(log_structure, config)
356
+ @log_structure = log_structure
330
357
  @config = config || OpenStruct.new
331
358
  end
332
359
 
@@ -390,10 +417,38 @@ module KLog
390
417
  config.skip == true
391
418
  end
392
419
 
420
+ # Useful in complex debug scenarios
421
+ def pry_at
422
+ config.pry_at || []
423
+ end
424
+
425
+ def pry_at?(section)
426
+ pry_at.include?(section)
427
+ end
428
+
393
429
  # Skip empty array node (my be useful for other nodes, but not yet)
394
430
  def skip_empty?
395
431
  config.skip_empty == true
396
432
  end
433
+
434
+ def show_array_count
435
+ log_structure.show_array_count
436
+ end
437
+
438
+ def debug
439
+ l = KLog::LogUtil.new(KLog.logger)
440
+ l.kv('columns', columns) if columns
441
+ l.kv('heading', heading) if heading
442
+ l.kv('heading_type', heading_type) if heading_type
443
+ l.kv('filter?', filter?)
444
+ l.kv('take', take)
445
+ l.kv('limited?', limited?)
446
+ l.kv('sort?', sort?)
447
+ l.kv('sort', sort)
448
+ l.kv('skip?', skip?)
449
+ l.kv('pry_at', pry_at)
450
+ l.kv('skip_empty?', skip_empty?)
451
+ end
397
452
  end
398
453
  end
399
454
  end
@@ -148,7 +148,7 @@ module KLog
148
148
  # @option opts [String] :heading_type :heading, :subheading, :section_heading
149
149
  # @option opts [Boolean] :skip_array Arrays items can be skipped
150
150
  def structure(data, **opts)
151
- structure = LogStructure.new(**opts)
151
+ structure = LogStructure.new(opts)
152
152
  structure.log(data)
153
153
  end
154
154
 
@@ -195,14 +195,31 @@ module KLog
195
195
  alias ostruct open_struct
196
196
  alias o open_struct
197
197
 
198
- def exception(exception)
199
- line
198
+ # Examples
199
+ # log.exception(e)
200
+ # log.exception(e, style: :message)
201
+ # log.exception(e, style: :short)
202
+ def exception(exception, style: :long, method_info: nil)
203
+ line unless style == :message
204
+
205
+ @logger.error(KLog::LogHelper.bg_red(exception.message))
206
+ @logger.info([method_info.owner.name, method_info.name].join('#')) if method_info
207
+
208
+ case style
209
+ when :short
210
+ trace_items = exception.backtrace.select { |row| row.start_with?(Dir.pwd) }
211
+ when :long
212
+ trace_items = exception.backtrace
213
+ else
214
+ trace_items = []
215
+ end
200
216
 
201
- @logger.info(KLog::LogHelper.bg_red(exception.message))
202
217
 
203
- @logger.info(KLog::LogHelper.yellow(exception.backtrace.map { |row| row.start_with?(Dir.pwd) ? KLog::LogHelper.yellow(row) : KLog::LogHelper.red(row) }.join("\n")))
218
+ trace_items = trace_items.map { |row| row.start_with?(Dir.pwd) ? KLog::LogHelper.yellow(row) : KLog::LogHelper.red(row) }
204
219
 
205
- line
220
+ @logger.info(KLog::LogHelper.yellow(trace_items.join("\n"))) if trace_items.length.positive?
221
+
222
+ line unless style == :message
206
223
  end
207
224
 
208
225
  #----------------------------------------------------------------------------------------------------
data/lib/k_log/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KLog
4
- VERSION = '0.0.27'
4
+ VERSION = '0.0.32'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: k_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.27
4
+ version: 0.0.32
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cruwys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-29 00:00:00.000000000 Z
11
+ date: 2021-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: k_util