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 +4 -4
- data/.rubocop.yml +3 -0
- data/lib/k_log.rb +39 -39
- data/lib/k_log/examples.rb +9 -0
- data/lib/k_log/log_util.rb +385 -381
- data/lib/k_log/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 243999c48416e082085f9c72519cd2cc97bb8585389ee6848592f18dc2cfc6bd
|
4
|
+
data.tar.gz: '009abfdd9b635d89f6e31f8197516f0dd612ce14adc9dfb7f005c9a9da458684'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f19cf492a22aaf8dbc89dcd9246aacc5732e3db7844d17030d63091e672c4fb201d11c221bbe24450cb1d8745b16d8b70bb21994cc821387ee703aaea4bbc69f
|
7
|
+
data.tar.gz: f0355d0438831db47d1ec390ba7d336b763d76f8d1d23f9520c98a1641901f0114c9961e9ac5d158daa8cddf5658782c6311721aeea5f01cd6dbe7ba24a80c80
|
data/.rubocop.yml
CHANGED
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
|
data/lib/k_log/examples.rb
CHANGED
@@ -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
|
data/lib/k_log/log_util.rb
CHANGED
@@ -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
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
indent
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
#
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
KLog.logger.
|
280
|
-
|
281
|
-
KLog.logger.
|
282
|
-
KLog.logger.
|
283
|
-
KLog.logger.
|
284
|
-
|
285
|
-
|
286
|
-
KLog.logger.
|
287
|
-
KLog.logger.
|
288
|
-
KLog.logger.
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
KLog.logger.
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
KLog.logger.
|
297
|
-
|
298
|
-
|
299
|
-
KLog.logger.progress
|
300
|
-
KLog.logger.progress
|
301
|
-
KLog.logger.progress
|
302
|
-
|
303
|
-
|
304
|
-
KLog.logger.progress
|
305
|
-
KLog.logger.progress
|
306
|
-
KLog.logger.progress
|
307
|
-
|
308
|
-
|
309
|
-
KLog.logger.
|
310
|
-
KLog.logger.
|
311
|
-
KLog.logger.
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
KLog.logger.yaml(
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
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
|
+
# 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
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.
|
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-
|
11
|
+
date: 2021-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: table_print
|