k_log 0.0.13 → 0.0.18

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: 87495f9dbb82ab9af517ee962042df71740f8bf223f8d9935098c28c017379e5
4
- data.tar.gz: d63f3c5ba3af1088ef434bbd2677771304ddbff857aafe63ca64f0b053fbd631
3
+ metadata.gz: 785e24f33c830f5aa042dafcf83f188a097b8cf5060598f690245a71646726d2
4
+ data.tar.gz: 0d78f522bc72498a30c6eb04e845ff51a346228378ffd78ae38767cb5f2b3abc
5
5
  SHA512:
6
- metadata.gz: d612388ab6ed66d80750e91936a7311f6cb2d3b643499ca3297856cdf3cad72b2640affe34c5e75cd364d99537640b40b6dc16cb5edf0e89a51d8e9aa8c0c652
7
- data.tar.gz: 7ebcbf83cfa5b0a48fdab4d879d0aa3a9ed6887f4e01c9109b0d91b9fa916d96ed49407c5e33eeb1f1840f5c6ea3577839165fac8029c3830feccc0a17f0e902
6
+ metadata.gz: 00c9050b591c246589a74a257a1c7b243eff8b0ef23a94652480d33bf713fd2f9bef5ac33f6f7c9e2784ab6d951405d04d57feaf2d3e8625667f443ea8b58f6a
7
+ data.tar.gz: 03676ef5279a6dc4c498ef4dd7c0d77bcb1f39eee93295cc58e3913a1e484809f85f1e7bc68a87b0683d428504e5ccfc5daf81b2861315777e3586d6b9b0d263
data/.rubocop.yml CHANGED
@@ -44,6 +44,9 @@ Layout/LineLength:
44
44
  Lint/UnusedMethodArgument:
45
45
  AllowUnusedKeywordArguments: true
46
46
 
47
+ Style/Documentation:
48
+ Enabled: false
49
+
47
50
  Style/BlockComments:
48
51
  Enabled: false
49
52
  Include:
data/k_log.gemspec CHANGED
@@ -38,6 +38,6 @@ Gem::Specification.new do |spec|
38
38
  spec.require_paths = ['lib']
39
39
  # spec.extensions = ['ext/k_log/extconf.rb']
40
40
 
41
- spec.add_dependency 'table_print', '~> 1.5.7'
41
+ spec.add_dependency 'table_print', '~> 1.5.7'
42
42
  # spec.add_dependency 'tty-box', '~> 0.5.0'
43
43
  end
data/lib/k_log.rb CHANGED
@@ -1,30 +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
-
10
- # Simple console log helpers
11
- module KLog
12
- # raise KLog::Error, 'Sample message'
13
- class Error < StandardError; end
14
-
15
- class << self
16
- attr_accessor :logger
17
- end
18
-
19
- def self.default_logger
20
- return @default_logger if defined? @default_logger
21
- @default_logger = begin
22
- logger = Logger.new($stdout)
23
- logger.level = Logger::DEBUG
24
- logger.formatter = KLog::LogFormatter.new
25
- KLog::LogUtil.new(logger)
26
- end
27
- end
28
- end
29
-
30
- puts "KLog::Version: #{KLog::VERSION}" if ENV['KLUE_DEBUG']&.to_s&.downcase == 'true'
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
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KLog
4
+ class Examples
5
+ include KLog::Logging
6
+
7
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
8
+ def examples
9
+ examples_simple
10
+ examples_complex
11
+ end
12
+
13
+ def examples_simple
14
+ log.debug 'some debug message'
15
+ log.info 'some info message'
16
+ log.warn 'some warning message'
17
+ log.error 'some error message'
18
+ log.fatal 'some fatal message'
19
+
20
+ log.kv('First Name', 'David')
21
+ log.kv('Last Name', 'Cruwys')
22
+ log.kv('Age', 45)
23
+ log.kv('Sex', 'male')
24
+
25
+ log.heading('Heading')
26
+ log.subheading('Sub Heading')
27
+ log.section_heading('Section Heading')
28
+ end
29
+
30
+ def examples_complex
31
+ log.block ['Line 1', 12, 'Line 3', true, 'Line 5']
32
+
33
+ log.progress(0, 'Section 1')
34
+ log.progress
35
+ log.progress
36
+ save_progress = log.progress
37
+
38
+ log.progress(10, 'Section 2')
39
+ log.progress
40
+ log.progress
41
+ log.progress
42
+
43
+ log.progress(save_progress, 'Section 1')
44
+ log.progress
45
+ log.progress
46
+ log.progress
47
+
48
+ log.line
49
+ log.line(20)
50
+ log.line(20, character: '-')
51
+
52
+ yaml1 = {}
53
+ yaml1['title'] = 'Software Architect'
54
+ yaml1['age'] = 45
55
+ yaml1['name'] = 'David'
56
+
57
+ yaml3 = {}
58
+ yaml3['title'] = 'Developer'
59
+ yaml3['age'] = 20
60
+ yaml3['name'] = 'Jin'
61
+
62
+ log.yaml(yaml1)
63
+
64
+ yaml2 = OpenStruct.new
65
+ yaml2.title = 'Software Architect'
66
+ yaml2.age = 45
67
+ yaml2.name = 'David'
68
+
69
+ log.yaml(yaml2)
70
+
71
+ mixed_yaml_array = [yaml1, yaml2]
72
+
73
+ # This fails because we don't correctly pre-process the array
74
+ log.yaml(mixed_yaml_array)
75
+
76
+ hash_yaml_array = [yaml1, yaml3]
77
+
78
+ log.yaml(hash_yaml_array)
79
+
80
+ begin
81
+ raise 'Here is an error'
82
+ rescue StandardError => e
83
+ log.exception(e)
84
+ end
85
+ end
86
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
87
+
88
+ private
89
+
90
+ def debug_multi_lines(lines)
91
+ lines.each do |line|
92
+ debug(line)
93
+ end
94
+ end
95
+
96
+ def info_multi_lines(lines)
97
+ lines.each do |line|
98
+ info(line)
99
+ end
100
+ end
101
+
102
+ def warn_multi_lines(lines)
103
+ lines.each do |line|
104
+ warn(line)
105
+ end
106
+ end
107
+
108
+ def error_multi_lines(lines)
109
+ lines.each do |line|
110
+ error(line)
111
+ end
112
+ end
113
+
114
+ def fatal_multi_lines(lines)
115
+ lines.each do |line|
116
+ fatal(line)
117
+ end
118
+ end
119
+ end
120
+ end
@@ -89,7 +89,7 @@ module KLog
89
89
 
90
90
  unless title.nil?
91
91
  result.push(title)
92
- result.push(line(70, ','))
92
+ result.push(line(70, '-'))
93
93
  end
94
94
 
95
95
  result.push messages if messages.is_a?(String) || messages.is_a?(Integer)
@@ -1,353 +1,381 @@
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
- def kv(key, value, key_width = 30)
54
- message = LogHelper.kv(key, value, key_width)
55
- @logger.info(message)
56
- end
57
-
58
- # Write a progress point, progress will update on it's own
59
- def progress(pos = nil, section = nil)
60
- message = LogHelper.progress(pos, section)
61
- # @logger.debug(message)
62
- @logger.info(message)
63
-
64
- LogHelper.progress_position
65
- end
66
-
67
- # prints out a line to the log
68
- def line(size = 70, character: '=')
69
- message = LogHelper.line(size, character)
70
-
71
- @logger.info(message)
72
- end
73
-
74
- def heading(heading, size = 70)
75
- lines = LogHelper.heading(heading, size)
76
- info_multi_lines(lines)
77
- end
78
-
79
- def subheading(heading, size = 70)
80
- lines = LogHelper.subheading(heading, size)
81
-
82
- info_multi_lines(lines)
83
- end
84
-
85
- # A section heading
86
- #
87
- # example:
88
- # [ I am a heading ]----------------------------------------------------
89
- def section_heading(heading, size = 70)
90
- heading = LogHelper.section_heading(heading, size)
91
-
92
- info(heading)
93
- end
94
-
95
- def block(messages, include_line: true, title: nil)
96
- lines = LogHelper.block(messages, include_line: include_line, title: title)
97
-
98
- info_multi_lines(lines)
99
- end
100
-
101
- # # :sql_array should be an array with SQL and values or with SQL and Hash
102
- # # example:
103
- # # L.sql(["name = :name and group_id = :value OR parent_id = :value", name: "foo'bar", value: 4])
104
- # # L.sql([sql_exact_match_skills_in_use, {names: self.segments_container.segment_values}])
105
- # def sql(sql_array)
106
- # value = ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
107
-
108
- # info(value)
109
- # end
110
-
111
- def yaml(data, is_line: true)
112
- require 'yaml'
113
- line if is_line
114
-
115
- @logger.info(data.to_yaml) if data.is_a?(Hash)
116
-
117
- @logger.info(data.marshal_dump.to_yaml) if data.is_a?(OpenStruct)
118
-
119
- if data.is_a? Array
120
- data.each do |d|
121
- @logger.info(d.to_yaml)
122
- end
123
- end
124
-
125
- line if is_line
126
- end
127
-
128
- def json(data)
129
- @logger.info(JSON.pretty_generate(data))
130
- end
131
- alias j json
132
-
133
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
134
- def open_struct(data, indent = '', **opts)
135
- data.each_pair do |key, value|
136
- case value
137
- when OpenStruct
138
- if value['rows'].is_a?(Array)
139
- # L.subheading(key)
140
- opts[:subheading] = key
141
- open_struct(value, indent, **opts)
142
- opts.delete(:subheading)
143
- else
144
- L.kv "#{indent}#{key}", ''
145
- indent = "#{indent} "
146
- open_struct(value, indent, **opts)
147
- indent = indent.chomp(' ')
148
- end
149
- when Array
150
- next unless opts[:skip_array].nil?
151
-
152
- puts LogHelper.section_heading(opts[:subheading], 88) unless opts[:subheading].nil?
153
-
154
- if value.length.positive?
155
- if value.first.is_a?(String)
156
- L.kv "#{indent}#{key}", value.join(', ')
157
- else
158
- tp value, value.first.to_h.keys
159
- end
160
- end
161
- else
162
- L.kv "#{indent}#{key}", value
163
- end
164
- end
165
- nil
166
- end
167
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
168
- alias ostruct open_struct
169
- alias o open_struct
170
-
171
- def exception(exception)
172
- line
173
-
174
- @logger.info(exception.message)
175
- @logger.info(exception.backtrace.join("\n"))
176
-
177
- line
178
- end
179
-
180
- #----------------------------------------------------------------------------------------------------
181
- # Pretty Loggers
182
- #----------------------------------------------------------------------------------------------------
183
-
184
- # NOTE: using pretty_inspect is an existing namespace conflict
185
- # rubocop:disable Metrics/AbcSize
186
- def pretty_class(instance)
187
- c = instance.class
188
-
189
- line
190
-
191
- kv('Full Class', c.name)
192
- kv('Module', c.name.deconstantize)
193
- kv('Class', c.name.demodulize)
194
-
195
- source_location = c.instance_methods(false).map do |m|
196
- c.instance_method(m).source_location.first
197
- end.uniq
198
-
199
- begin
200
- kv('Source Location', source_location)
201
- rescue StandardError => e
202
- warn e
203
- end
204
-
205
- line
206
- end
207
- # rubocop:enable Metrics/AbcSize
208
-
209
- # NOTE: using pretty_inspect is an existing namespace conflict
210
- def pretty_params(params)
211
- line
212
-
213
- params.each do |k, v|
214
- if params[k].is_a?(Hash)
215
-
216
- params[k].each do |child_k, child_v|
217
- kv("#{k}[#{child_k}]", child_v)
218
- end
219
-
220
- else
221
- kv(k, v)
222
- end
223
- end
224
-
225
- line
226
- end
227
-
228
- def help_all_symbols
229
- # Produces a lot of data, need some sort of filter I think before this is useful
230
- Symbol.all_symbols.each do |s|
231
- info s
232
- # debug s
233
- end
234
- end
235
-
236
- #----------------------------------------------------------------------------------------------------
237
- # Internal Methods
238
- #----------------------------------------------------------------------------------------------------
239
-
240
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
241
- def self.examples
242
- examples_simple
243
- # examples_complex
244
- end
245
-
246
- def self.examples_simple
247
- L.debug 'some debug message'
248
- L.info 'some info message'
249
- L.warn 'some warning message'
250
- L.error 'some error message'
251
- L.fatal 'some fatal message'
252
-
253
- L.kv('First Name', 'David')
254
- L.kv('Last Name', 'Cruwys')
255
- L.kv('Age', 45)
256
- L.kv('Sex', 'male')
257
-
258
- L.heading('Heading')
259
- L.subheading('Sub Heading')
260
- L.section_heading('Section Heading')
261
- end
262
-
263
- def self.examples_complex
264
- L.block ['Line 1', 12, 'Line 3', true, 'Line 5']
265
-
266
- L.progress(0, 'Section 1')
267
- L.progress
268
- L.progress
269
- save_progress = L.progress
270
-
271
- L.progress(10, 'Section 2')
272
- L.progress
273
- L.progress
274
- L.progress
275
-
276
- L.progress(save_progress, 'Section 1')
277
- L.progress
278
- L.progress
279
- L.progress
280
-
281
- L.line
282
- L.line(20)
283
- L.line(20, character: '-')
284
-
285
- yaml1 = {}
286
- yaml1['title'] = 'Software Architect'
287
- yaml1['age'] = 45
288
- yaml1['name'] = 'David'
289
-
290
- yaml3 = {}
291
- yaml3['title'] = 'Developer'
292
- yaml3['age'] = 20
293
- yaml3['name'] = 'Jin'
294
-
295
- L.yaml(yaml1)
296
-
297
- yaml2 = OpenStruct.new
298
- yaml2.title = 'Software Architect'
299
- yaml2.age = 45
300
- yaml2.name = 'David'
301
-
302
- L.yaml(yaml2)
303
-
304
- mixed_yaml_array = [yaml1, yaml2]
305
-
306
- # This fails because we don't correctly pre-process the array
307
- L.yaml(mixed_yaml_array)
308
-
309
- hash_yaml_array = [yaml1, yaml3]
310
-
311
- L.yaml(hash_yaml_array)
312
-
313
- begin
314
- raise 'Here is an error'
315
- rescue StandardError => e
316
- L.exception(e)
317
- end
318
- end
319
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
320
-
321
- private
322
-
323
- def debug_multi_lines(lines)
324
- lines.each do |line|
325
- debug(line)
326
- end
327
- end
328
-
329
- def info_multi_lines(lines)
330
- lines.each do |line|
331
- info(line)
332
- end
333
- end
334
-
335
- def warn_multi_lines(lines)
336
- lines.each do |line|
337
- warn(line)
338
- end
339
- end
340
-
341
- def error_multi_lines(lines)
342
- lines.each do |line|
343
- error(line)
344
- end
345
- end
346
-
347
- def fatal_multi_lines(lines)
348
- lines.each do |line|
349
- fatal(line)
350
- end
351
- end
352
- end
353
- 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
+ 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
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KLog
4
+ module Logging
5
+ def log
6
+ @log ||= KLog.logger
7
+ end
8
+ end
9
+ 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.13'
4
+ VERSION = '0.0.18'
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.13
4
+ version: 0.0.18
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-04-01 00:00:00.000000000 Z
11
+ date: 2021-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: table_print
@@ -52,9 +52,11 @@ files:
52
52
  - hooks/update-version
53
53
  - k_log.gemspec
54
54
  - lib/k_log.rb
55
+ - lib/k_log/examples.rb
55
56
  - lib/k_log/log_formatter.rb
56
57
  - lib/k_log/log_helper.rb
57
58
  - lib/k_log/log_util.rb
59
+ - lib/k_log/logging.rb
58
60
  - lib/k_log/version.rb
59
61
  - usage.png
60
62
  homepage: http://appydave.com/gems/k-log