k_log 0.0.18 → 0.0.19

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: 785e24f33c830f5aa042dafcf83f188a097b8cf5060598f690245a71646726d2
4
- data.tar.gz: 0d78f522bc72498a30c6eb04e845ff51a346228378ffd78ae38767cb5f2b3abc
3
+ metadata.gz: 243999c48416e082085f9c72519cd2cc97bb8585389ee6848592f18dc2cfc6bd
4
+ data.tar.gz: '009abfdd9b635d89f6e31f8197516f0dd612ce14adc9dfb7f005c9a9da458684'
5
5
  SHA512:
6
- metadata.gz: 00c9050b591c246589a74a257a1c7b243eff8b0ef23a94652480d33bf713fd2f9bef5ac33f6f7c9e2784ab6d951405d04d57feaf2d3e8625667f443ea8b58f6a
7
- data.tar.gz: 03676ef5279a6dc4c498ef4dd7c0d77bcb1f39eee93295cc58e3913a1e484809f85f1e7bc68a87b0683d428504e5ccfc5daf81b2861315777e3586d6b9b0d263
6
+ metadata.gz: f19cf492a22aaf8dbc89dcd9246aacc5732e3db7844d17030d63091e672c4fb201d11c221bbe24450cb1d8745b16d8b70bb21994cc821387ee703aaea4bbc69f
7
+ data.tar.gz: f0355d0438831db47d1ec390ba7d336b763d76f8d1d23f9520c98a1641901f0114c9961e9ac5d158daa8cddf5658782c6311721aeea5f01cd6dbe7ba24a80c80
data/.rubocop.yml CHANGED
@@ -52,6 +52,9 @@ Style/BlockComments:
52
52
  Include:
53
53
  - "**/spec/*"
54
54
 
55
+ Layout/EndOfLine:
56
+ EnforcedStyle: lf
57
+
55
58
  # My Preferences - Start
56
59
  Metrics/ClassLength:
57
60
  Enabled: false
data/lib/k_log.rb CHANGED
@@ -1,39 +1,39 @@
1
- # frozen_string_literal: true
2
-
3
- require 'logger'
4
- require 'table_print'
5
- require 'k_log/version'
6
- require 'k_log/log_formatter'
7
- require 'k_log/log_helper'
8
- require 'k_log/log_util'
9
- require 'k_log/logging'
10
-
11
- # Simple console log helpers
12
- module KLog
13
- # raise KLog::Error, 'Sample message'
14
- class Error < StandardError; end
15
-
16
- class << self
17
- attr_accessor :logger
18
-
19
- def default_logger
20
- return @default_logger if defined? @default_logger
21
-
22
- @default_logger = begin
23
- logger = Logger.new($stdout)
24
- logger.level = Logger::DEBUG
25
- logger.formatter = KLog::LogFormatter.new
26
- KLog::LogUtil.new(logger)
27
- end
28
- end
29
- end
30
- end
31
-
32
- KLog.logger = KLog.default_logger
33
-
34
- if ENV['KLUE_DEBUG']&.to_s&.downcase == 'true'
35
- namespace = 'KLog::Version'
36
- file_path = $LOADED_FEATURES.find { |f| f.include?('k_log/version') }
37
- version = KLog::VERSION.ljust(9)
38
- puts "#{namespace.ljust(35)} : #{version.ljust(9)} : #{file_path}"
39
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'table_print'
5
+ require 'k_log/version'
6
+ require 'k_log/log_formatter'
7
+ require 'k_log/log_helper'
8
+ require 'k_log/log_util'
9
+ require 'k_log/logging'
10
+
11
+ # Simple console log helpers
12
+ module KLog
13
+ # raise KLog::Error, 'Sample message'
14
+ class Error < StandardError; end
15
+
16
+ class << self
17
+ attr_accessor :logger
18
+
19
+ def default_logger
20
+ return @default_logger if defined? @default_logger
21
+
22
+ @default_logger = begin
23
+ logger = Logger.new($stdout)
24
+ logger.level = Logger::DEBUG
25
+ logger.formatter = KLog::LogFormatter.new
26
+ KLog::LogUtil.new(logger)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ KLog.logger = KLog.default_logger
33
+
34
+ if ENV['KLUE_DEBUG']&.to_s&.downcase == 'true'
35
+ namespace = 'KLog::Version'
36
+ file_path = $LOADED_FEATURES.find { |f| f.include?('k_log/version') }
37
+ version = KLog::VERSION.ljust(9)
38
+ puts "#{namespace.ljust(35)} : #{version.ljust(9)} : #{file_path}"
39
+ end
@@ -25,6 +25,15 @@ module KLog
25
25
  log.heading('Heading')
26
26
  log.subheading('Sub Heading')
27
27
  log.section_heading('Section Heading')
28
+
29
+ data = OpenStruct.new
30
+ data.title = 'Software Architect'
31
+ data.age = 45
32
+ data.name = 'David'
33
+ data.names = %w[David Bill]
34
+ data.status = :debug
35
+ data.statuses = %i[debug info blah]
36
+ log.open_struct(data, section_heading: 'Display Open Struct')
28
37
  end
29
38
 
30
39
  def examples_complex
@@ -1,381 +1,385 @@
1
- # frozen_string_literal: true
2
-
3
- # Format Logger Util provides static helper methods that delegate responsibility
4
- # to the underlying Format Logger, you can use the Util instead Rails.logger so
5
- # that you have access to IDE intellisense around available methods and so you
6
- # can use the same logger calls from controllers/models which normally have
7
- # access to to a logger variable and services which do not have access to a
8
- # logger variable
9
- #
10
- # I usually alias the call to LogUtil by doing L = LogUtil
11
-
12
- # require_relative 'format_logger'
13
- # require_relative 'format_logger_helper'
14
-
15
- module KLog
16
- # Simple console log helpers
17
- class LogUtil
18
- def initialize(logger)
19
- @logger = logger
20
- end
21
-
22
- # include ActiveSupport::LoggerThreadSafeLevel
23
- # include LoggerSilence
24
-
25
- #----------------------------------------------------------------------------------------------------
26
- # Standard Accessors that are on the standard rails Logger
27
- #----------------------------------------------------------------------------------------------------
28
- def debug(value)
29
- @logger.debug(value)
30
- end
31
-
32
- def info(value)
33
- @logger.info(value)
34
- end
35
-
36
- def warn(value)
37
- @logger.warn(value)
38
- end
39
-
40
- def error(value)
41
- @logger.error(value)
42
- end
43
-
44
- def fatal(value)
45
- @logger.fatal(value)
46
- end
47
-
48
- #----------------------------------------------------------------------------------------------------
49
- # Helper Log output Methods
50
- #----------------------------------------------------------------------------------------------------
51
-
52
- # Write a Key/Value Pair
53
- # Need to change this to named_param
54
- def kv(key, value, key_width = 30)
55
- message = LogHelper.kv(key, value, key_width)
56
- @logger.info(message)
57
- end
58
-
59
- # Write a progress point, progress will update on it's own
60
- def progress(pos = nil, section = nil)
61
- message = LogHelper.progress(pos, section)
62
- # @logger.debug(message)
63
- @logger.info(message)
64
-
65
- LogHelper.progress_position
66
- end
67
-
68
- # prints out a line to the log
69
- def line(size = 70, character: '=')
70
- message = LogHelper.line(size, character)
71
-
72
- @logger.info(message)
73
- end
74
-
75
- def heading(heading, size = 70)
76
- lines = LogHelper.heading(heading, size)
77
- info_multi_lines(lines)
78
- end
79
-
80
- def subheading(heading, size = 70)
81
- lines = LogHelper.subheading(heading, size)
82
-
83
- info_multi_lines(lines)
84
- end
85
-
86
- # A section heading
87
- #
88
- # example:
89
- # [ I am a heading ]----------------------------------------------------
90
- def section_heading(heading, size = 70)
91
- heading = LogHelper.section_heading(heading, size)
92
-
93
- info(heading)
94
- end
95
-
96
- def block(messages, include_line: true, title: nil)
97
- lines = LogHelper.block(messages, include_line: include_line, title: title)
98
-
99
- info_multi_lines(lines)
100
- end
101
-
102
- # # :sql_array should be an array with SQL and values or with SQL and Hash
103
- # # example:
104
- # # KLog.logger.sql(["name = :name and group_id = :value OR parent_id = :value", name: "foo'bar", value: 4])
105
- # # KLog.logger.sql([sql_exact_match_skills_in_use, {names: self.segments_container.segment_values}])
106
- # def sql(sql_array)
107
- # value = ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
108
-
109
- # info(value)
110
- # end
111
-
112
- def yaml(data, is_line: true)
113
- require 'yaml'
114
- line if is_line
115
-
116
- @logger.info(data.to_yaml) if data.is_a?(Hash)
117
-
118
- @logger.info(data.marshal_dump.to_yaml) if data.is_a?(OpenStruct)
119
-
120
- if data.is_a? Array
121
- data.each do |d|
122
- @logger.info(d.to_yaml)
123
- end
124
- end
125
-
126
- line if is_line
127
- end
128
-
129
- def json(data)
130
- @logger.info(JSON.pretty_generate(data))
131
- end
132
- alias j json
133
-
134
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
135
- def open_struct(data, indent = '', **opts)
136
- data.each_pair do |key, value|
137
- case value
138
- when OpenStruct
139
- if value['rows'].is_a?(Array)
140
- # KLog.logger.subheading(key)
141
- opts[:subheading] = key
142
- open_struct(value, indent, **opts)
143
- opts.delete(:subheading)
144
- else
145
- KLog.logger.kv "#{indent}#{key}", ''
146
- indent = "#{indent} "
147
- open_struct(value, indent, **opts)
148
- indent = indent.chomp(' ')
149
- end
150
- when Array
151
- next unless opts[:skip_array].nil?
152
-
153
- puts LogHelper.section_heading(opts[:subheading], 88) unless opts[:subheading].nil?
154
-
155
- if value.length.positive?
156
- if value.first.is_a?(String)
157
- KLog.logger.kv "#{indent}#{key}", value.join(', ')
158
- else
159
- tp value, value.first.to_h.keys
160
- end
161
- end
162
- else
163
- KLog.logger.kv "#{indent}#{key}", value
164
- end
165
- end
166
- nil
167
- end
168
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
169
- alias ostruct open_struct
170
- alias o open_struct
171
-
172
- def exception(exception)
173
- line
174
-
175
- @logger.info(exception.message)
176
- @logger.info(exception.backtrace.join("\n"))
177
-
178
- line
179
- end
180
-
181
- #----------------------------------------------------------------------------------------------------
182
- # Pretty Loggers
183
- #----------------------------------------------------------------------------------------------------
184
-
185
- # NOTE: using pretty_inspect is an existing namespace conflict
186
- # rubocop:disable Metrics/AbcSize
187
- def pretty_class(instance)
188
- c = instance.class
189
-
190
- line
191
-
192
- kv('Full Class', c.name)
193
- kv('Module', c.name.deconstantize)
194
- kv('Class', c.name.demodulize)
195
-
196
- source_location = c.instance_methods(false).map do |m|
197
- c.instance_method(m).source_location.first
198
- end.uniq
199
-
200
- begin
201
- kv('Source Location', source_location)
202
- rescue StandardError => e
203
- warn e
204
- end
205
-
206
- line
207
- end
208
- # rubocop:enable Metrics/AbcSize
209
-
210
- def kv_hash(hash)
211
- hash.each do |key, value|
212
- kv(key, value)
213
- end
214
- end
215
-
216
- # NOTE: using pretty_inspect is an existing namespace conflict
217
- def pretty_params(params)
218
- line
219
-
220
- params.each do |k, v|
221
- if params[k].is_a?(Hash)
222
-
223
- params[k].each do |child_k, child_v|
224
- kv("#{k}[#{child_k}]", child_v)
225
- end
226
-
227
- else
228
- kv(k, v)
229
- end
230
- end
231
-
232
- line
233
- end
234
-
235
- def help_all_symbols
236
- # Produces a lot of data, need some sort of filter I think before this is useful
237
- Symbol.all_symbols.each do |s|
238
- info s
239
- # debug s
240
- end
241
- end
242
-
243
- def visual_compare_hashes(hash1, hash2)
244
- content1 = JSON.pretty_generate(hash1)
245
- content2 = JSON.pretty_generate(hash2)
246
-
247
- file1 = Tempfile.new('hash1')
248
- file1.write(content1)
249
- file1.close
250
-
251
- file2 = Tempfile.new('hash2')
252
- file2.write(content2)
253
- file2.close
254
-
255
- system "code --diff #{file1.path} #{file2.path}"
256
-
257
- # Provide enough time for vscode to open and display the files before deleting them
258
- sleep 1
259
-
260
- file1.unlink
261
- file2.unlink
262
- end
263
-
264
- #----------------------------------------------------------------------------------------------------
265
- # Internal Methods
266
- #----------------------------------------------------------------------------------------------------
267
-
268
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
269
- def self.examples
270
- examples_simple
271
- # examples_complex
272
- end
273
-
274
- def self.examples_simple
275
- KLog.logger.debug 'some debug message'
276
- KLog.logger.info 'some info message'
277
- KLog.logger.warn 'some warning message'
278
- KLog.logger.error 'some error message'
279
- KLog.logger.fatal 'some fatal message'
280
-
281
- KLog.logger.kv('First Name', 'David')
282
- KLog.logger.kv('Last Name', 'Cruwys')
283
- KLog.logger.kv('Age', 45)
284
- KLog.logger.kv('Sex', 'male')
285
-
286
- KLog.logger.heading('Heading')
287
- KLog.logger.subheading('Sub Heading')
288
- KLog.logger.section_heading('Section Heading')
289
- end
290
-
291
- def self.examples_complex
292
- KLog.logger.block ['Line 1', 12, 'Line 3', true, 'Line 5']
293
-
294
- KLog.logger.progress(0, 'Section 1')
295
- KLog.logger.progress
296
- KLog.logger.progress
297
- save_progress = KLog.logger.progress
298
-
299
- KLog.logger.progress(10, 'Section 2')
300
- KLog.logger.progress
301
- KLog.logger.progress
302
- KLog.logger.progress
303
-
304
- KLog.logger.progress(save_progress, 'Section 1')
305
- KLog.logger.progress
306
- KLog.logger.progress
307
- KLog.logger.progress
308
-
309
- KLog.logger.line
310
- KLog.logger.line(20)
311
- KLog.logger.line(20, character: '-')
312
-
313
- yaml1 = {}
314
- yaml1['title'] = 'Software Architect'
315
- yaml1['age'] = 45
316
- yaml1['name'] = 'David'
317
-
318
- yaml3 = {}
319
- yaml3['title'] = 'Developer'
320
- yaml3['age'] = 20
321
- yaml3['name'] = 'Jin'
322
-
323
- KLog.logger.yaml(yaml1)
324
-
325
- yaml2 = OpenStruct.new
326
- yaml2.title = 'Software Architect'
327
- yaml2.age = 45
328
- yaml2.name = 'David'
329
-
330
- KLog.logger.yaml(yaml2)
331
-
332
- mixed_yaml_array = [yaml1, yaml2]
333
-
334
- # This fails because we don't correctly pre-process the array
335
- KLog.logger.yaml(mixed_yaml_array)
336
-
337
- hash_yaml_array = [yaml1, yaml3]
338
-
339
- KLog.logger.yaml(hash_yaml_array)
340
-
341
- begin
342
- raise 'Here is an error'
343
- rescue StandardError => e
344
- KLog.logger.exception(e)
345
- end
346
- end
347
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
348
-
349
- private
350
-
351
- def debug_multi_lines(lines)
352
- lines.each do |line|
353
- debug(line)
354
- end
355
- end
356
-
357
- def info_multi_lines(lines)
358
- lines.each do |line|
359
- info(line)
360
- end
361
- end
362
-
363
- def warn_multi_lines(lines)
364
- lines.each do |line|
365
- warn(line)
366
- end
367
- end
368
-
369
- def error_multi_lines(lines)
370
- lines.each do |line|
371
- error(line)
372
- end
373
- end
374
-
375
- def fatal_multi_lines(lines)
376
- lines.each do |line|
377
- fatal(line)
378
- end
379
- end
380
- end
381
- end
1
+ # frozen_string_literal: true
2
+
3
+ # Format Logger Util provides static helper methods that delegate responsibility
4
+ # to the underlying Format Logger, you can use the Util instead Rails.logger so
5
+ # that you have access to IDE intellisense around available methods and so you
6
+ # can use the same logger calls from controllers/models which normally have
7
+ # access to to a logger variable and services which do not have access to a
8
+ # logger variable
9
+ #
10
+ # I usually alias the call to LogUtil by doing L = LogUtil
11
+
12
+ # require_relative 'format_logger'
13
+ # require_relative 'format_logger_helper'
14
+
15
+ module KLog
16
+ # Simple console log helpers
17
+ class LogUtil
18
+ def initialize(logger)
19
+ @logger = logger
20
+ end
21
+
22
+ # include ActiveSupport::LoggerThreadSafeLevel
23
+ # include LoggerSilence
24
+
25
+ #----------------------------------------------------------------------------------------------------
26
+ # Standard Accessors that are on the standard rails Logger
27
+ #----------------------------------------------------------------------------------------------------
28
+ def debug(value)
29
+ @logger.debug(value)
30
+ end
31
+
32
+ def info(value)
33
+ @logger.info(value)
34
+ end
35
+
36
+ def warn(value)
37
+ @logger.warn(value)
38
+ end
39
+
40
+ def error(value)
41
+ @logger.error(value)
42
+ end
43
+
44
+ def fatal(value)
45
+ @logger.fatal(value)
46
+ end
47
+
48
+ #----------------------------------------------------------------------------------------------------
49
+ # Helper Log output Methods
50
+ #----------------------------------------------------------------------------------------------------
51
+
52
+ # Write a Key/Value Pair
53
+ # Need to change this to named_param
54
+ def kv(key, value, key_width = 30)
55
+ message = LogHelper.kv(key, value, key_width)
56
+ @logger.info(message)
57
+ end
58
+
59
+ # Write a progress point, progress will update on it's own
60
+ def progress(pos = nil, section = nil)
61
+ message = LogHelper.progress(pos, section)
62
+ # @logger.debug(message)
63
+ @logger.info(message)
64
+
65
+ LogHelper.progress_position
66
+ end
67
+
68
+ # prints out a line to the log
69
+ def line(size = 70, character: '=')
70
+ message = LogHelper.line(size, character)
71
+
72
+ @logger.info(message)
73
+ end
74
+
75
+ def heading(heading, size = 70)
76
+ lines = LogHelper.heading(heading, size)
77
+ info_multi_lines(lines)
78
+ end
79
+
80
+ def subheading(heading, size = 70)
81
+ lines = LogHelper.subheading(heading, size)
82
+
83
+ info_multi_lines(lines)
84
+ end
85
+
86
+ # A section heading
87
+ #
88
+ # example:
89
+ # [ I am a heading ]----------------------------------------------------
90
+ def section_heading(heading, size = 70)
91
+ heading = LogHelper.section_heading(heading, size)
92
+
93
+ info(heading)
94
+ end
95
+
96
+ def block(messages, include_line: true, title: nil)
97
+ lines = LogHelper.block(messages, include_line: include_line, title: title)
98
+
99
+ info_multi_lines(lines)
100
+ end
101
+
102
+ # # :sql_array should be an array with SQL and values or with SQL and Hash
103
+ # # example:
104
+ # # KLog.logger.sql(["name = :name and group_id = :value OR parent_id = :value", name: "foo'bar", value: 4])
105
+ # # KLog.logger.sql([sql_exact_match_skills_in_use, {names: self.segments_container.segment_values}])
106
+ # def sql(sql_array)
107
+ # value = ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
108
+
109
+ # info(value)
110
+ # end
111
+
112
+ def yaml(data, is_line: true)
113
+ require 'yaml'
114
+ line if is_line
115
+
116
+ @logger.info(data.to_yaml) if data.is_a?(Hash)
117
+
118
+ @logger.info(data.marshal_dump.to_yaml) if data.is_a?(OpenStruct)
119
+
120
+ if data.is_a? Array
121
+ data.each do |d|
122
+ @logger.info(d.to_yaml)
123
+ end
124
+ end
125
+
126
+ line if is_line
127
+ end
128
+
129
+ def json(data)
130
+ @logger.info(JSON.pretty_generate(data))
131
+ end
132
+ alias j json
133
+
134
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
135
+ def open_struct(data, indent = '', **opts)
136
+ KLog.logger.heading(opts[:heading], 88) unless opts[:heading].nil?
137
+ KLog.logger.subheading(opts[:subheading], 88) unless opts[:subheading].nil?
138
+ KLog.logger.section_heading(opts[:section_heading], 88) unless opts[:section_heading].nil?
139
+
140
+ data.each_pair do |key, value|
141
+ case value
142
+ when OpenStruct
143
+ if value['rows'].is_a?(Array)
144
+ # KLog.logger.subheading(key)
145
+ opts[:subheading] = key
146
+ open_struct(value, indent, **opts)
147
+ opts.delete(:subheading)
148
+ else
149
+ KLog.logger.kv "#{indent}#{key}", ''
150
+ indent = "#{indent} "
151
+ open_struct(value, indent, **opts)
152
+ indent = indent.chomp(' ')
153
+ end
154
+ when Array
155
+ next unless opts[:skip_array].nil?
156
+
157
+ puts LogHelper.section_heading(opts[:subheading], 88) unless opts[:subheading].nil?
158
+
159
+ if value.length.positive?
160
+ if value.first.is_a?(String) || value.first.is_a?(Symbol)
161
+ KLog.logger.kv "#{indent}#{key}", value.map(&:to_s).join(', ')
162
+ else
163
+ tp value, value.first.to_h.keys
164
+ end
165
+ end
166
+ else
167
+ KLog.logger.kv "#{indent}#{key}", value
168
+ end
169
+ end
170
+ nil
171
+ end
172
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
173
+ alias ostruct open_struct
174
+ alias o open_struct
175
+
176
+ def exception(exception)
177
+ line
178
+
179
+ @logger.info(exception.message)
180
+ @logger.info(exception.backtrace.join("\n"))
181
+
182
+ line
183
+ end
184
+
185
+ #----------------------------------------------------------------------------------------------------
186
+ # Pretty Loggers
187
+ #----------------------------------------------------------------------------------------------------
188
+
189
+ # NOTE: using pretty_inspect is an existing namespace conflict
190
+ # rubocop:disable Metrics/AbcSize
191
+ def pretty_class(instance)
192
+ c = instance.class
193
+
194
+ line
195
+
196
+ kv('Full Class', c.name)
197
+ kv('Module', c.name.deconstantize)
198
+ kv('Class', c.name.demodulize)
199
+
200
+ source_location = c.instance_methods(false).map do |m|
201
+ c.instance_method(m).source_location.first
202
+ end.uniq
203
+
204
+ begin
205
+ kv('Source Location', source_location)
206
+ rescue StandardError => e
207
+ warn e
208
+ end
209
+
210
+ line
211
+ end
212
+ # rubocop:enable Metrics/AbcSize
213
+
214
+ def kv_hash(hash)
215
+ hash.each do |key, value|
216
+ kv(key, value)
217
+ end
218
+ end
219
+
220
+ # NOTE: using pretty_inspect is an existing namespace conflict
221
+ def pretty_params(params)
222
+ line
223
+
224
+ params.each do |k, v|
225
+ if params[k].is_a?(Hash)
226
+
227
+ params[k].each do |child_k, child_v|
228
+ kv("#{k}[#{child_k}]", child_v)
229
+ end
230
+
231
+ else
232
+ kv(k, v)
233
+ end
234
+ end
235
+
236
+ line
237
+ end
238
+
239
+ def help_all_symbols
240
+ # Produces a lot of data, need some sort of filter I think before this is useful
241
+ Symbol.all_symbols.each do |s|
242
+ info s
243
+ # debug s
244
+ end
245
+ end
246
+
247
+ def visual_compare_hashes(hash1, hash2)
248
+ content1 = JSON.pretty_generate(hash1)
249
+ content2 = JSON.pretty_generate(hash2)
250
+
251
+ file1 = Tempfile.new('hash1')
252
+ file1.write(content1)
253
+ file1.close
254
+
255
+ file2 = Tempfile.new('hash2')
256
+ file2.write(content2)
257
+ file2.close
258
+
259
+ system "code --diff #{file1.path} #{file2.path}"
260
+
261
+ # Provide enough time for vscode to open and display the files before deleting them
262
+ sleep 1
263
+
264
+ file1.unlink
265
+ file2.unlink
266
+ end
267
+
268
+ #----------------------------------------------------------------------------------------------------
269
+ # Internal Methods
270
+ #----------------------------------------------------------------------------------------------------
271
+
272
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
273
+ def self.examples
274
+ examples_simple
275
+ # examples_complex
276
+ end
277
+
278
+ def self.examples_simple
279
+ KLog.logger.debug 'some debug message'
280
+ KLog.logger.info 'some info message'
281
+ KLog.logger.warn 'some warning message'
282
+ KLog.logger.error 'some error message'
283
+ KLog.logger.fatal 'some fatal message'
284
+
285
+ KLog.logger.kv('First Name', 'David')
286
+ KLog.logger.kv('Last Name', 'Cruwys')
287
+ KLog.logger.kv('Age', 45)
288
+ KLog.logger.kv('Sex', 'male')
289
+
290
+ KLog.logger.heading('Heading')
291
+ KLog.logger.subheading('Sub Heading')
292
+ KLog.logger.section_heading('Section Heading')
293
+ end
294
+
295
+ def self.examples_complex
296
+ KLog.logger.block ['Line 1', 12, 'Line 3', true, 'Line 5']
297
+
298
+ KLog.logger.progress(0, 'Section 1')
299
+ KLog.logger.progress
300
+ KLog.logger.progress
301
+ save_progress = KLog.logger.progress
302
+
303
+ KLog.logger.progress(10, 'Section 2')
304
+ KLog.logger.progress
305
+ KLog.logger.progress
306
+ KLog.logger.progress
307
+
308
+ KLog.logger.progress(save_progress, 'Section 1')
309
+ KLog.logger.progress
310
+ KLog.logger.progress
311
+ KLog.logger.progress
312
+
313
+ KLog.logger.line
314
+ KLog.logger.line(20)
315
+ KLog.logger.line(20, character: '-')
316
+
317
+ yaml1 = {}
318
+ yaml1['title'] = 'Software Architect'
319
+ yaml1['age'] = 45
320
+ yaml1['name'] = 'David'
321
+
322
+ yaml3 = {}
323
+ yaml3['title'] = 'Developer'
324
+ yaml3['age'] = 20
325
+ yaml3['name'] = 'Jin'
326
+
327
+ KLog.logger.yaml(yaml1)
328
+
329
+ yaml2 = OpenStruct.new
330
+ yaml2.title = 'Software Architect'
331
+ yaml2.age = 45
332
+ yaml2.name = 'David'
333
+
334
+ KLog.logger.yaml(yaml2)
335
+
336
+ mixed_yaml_array = [yaml1, yaml2]
337
+
338
+ # This fails because we don't correctly pre-process the array
339
+ KLog.logger.yaml(mixed_yaml_array)
340
+
341
+ hash_yaml_array = [yaml1, yaml3]
342
+
343
+ KLog.logger.yaml(hash_yaml_array)
344
+
345
+ begin
346
+ raise 'Here is an error'
347
+ rescue StandardError => e
348
+ KLog.logger.exception(e)
349
+ end
350
+ end
351
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
352
+
353
+ private
354
+
355
+ def debug_multi_lines(lines)
356
+ lines.each do |line|
357
+ debug(line)
358
+ end
359
+ end
360
+
361
+ def info_multi_lines(lines)
362
+ lines.each do |line|
363
+ info(line)
364
+ end
365
+ end
366
+
367
+ def warn_multi_lines(lines)
368
+ lines.each do |line|
369
+ warn(line)
370
+ end
371
+ end
372
+
373
+ def error_multi_lines(lines)
374
+ lines.each do |line|
375
+ error(line)
376
+ end
377
+ end
378
+
379
+ def fatal_multi_lines(lines)
380
+ lines.each do |line|
381
+ fatal(line)
382
+ end
383
+ end
384
+ end
385
+ end
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.18'
4
+ VERSION = '0.0.19'
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.18
4
+ version: 0.0.19
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-06-26 00:00:00.000000000 Z
11
+ date: 2021-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: table_print