Class: KLog::LogUtil
- Inherits:
-
Object
- Object
- KLog::LogUtil
- Defined in:
- lib/k_log/log_util.rb
Overview
Simple console log helpers
Class Method Summary collapse
-
.examples ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- .examples_complex ⇒ Object
- .examples_simple ⇒ Object
Instance Method Summary collapse
- #block(messages, include_line: true, title: nil) ⇒ Object
-
#debug(value) ⇒ Object
—————————————————————————————————- Standard Accessors that are on the standard rails Logger —————————————————————————————————-.
- #dynamic_heading(heading, size: 70, type: :heading) ⇒ Object
- #error(value) ⇒ Object
-
#exception(exception, style: :long, method_info: nil) ⇒ Object
Examples log.exception(e) # <- :long log.exception(e, style: :message) log.exception(e, style: :short) log.exception(e, style: :long).
- #fatal(value) ⇒ Object
- #heading(heading, size = 70) ⇒ Object
- #help_all_symbols ⇒ Object
- #info(value) ⇒ Object
-
#initialize(logger) ⇒ LogUtil
constructor
A new instance of LogUtil.
- #json(data) ⇒ Object (also: #j)
-
#kv(key, value, key_width = 30) ⇒ Object
Write a Key/Value Pair Need to change this to named_param.
-
#kv_hash(hash) ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#line(size = 70, character: '=') ⇒ Object
prints out a line to the log.
-
#open_struct(data, indent = '', **opts) ⇒ Object
(also: #ostruct, #o)
rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize DEPRECATE.
-
#pretty_class(instance) ⇒ Object
NOTE: using pretty_inspect is an existing namespace conflict rubocop:disable Metrics/AbcSize.
-
#pretty_params(params) ⇒ Object
NOTE: using pretty_inspect is an existing namespace conflict.
-
#progress(pos = nil, section = nil) ⇒ Object
Write a progress point, progress will update on it’s own.
-
#section_heading(heading, size = 70) ⇒ Object
A section heading.
-
#structure(data, **opts) ⇒ Object
Log a structure.
- #subheading(heading, size = 70) ⇒ Object
- #visual_compare_hashes(hash1, hash2) ⇒ Object
- #warn(value) ⇒ Object
-
#yaml(data, is_line: true) ⇒ Object
info(value) end.
Constructor Details
#initialize(logger) ⇒ LogUtil
Returns a new instance of LogUtil.
18 19 20 |
# File 'lib/k_log/log_util.rb', line 18 def initialize(logger) @logger = logger end |
Class Method Details
.examples ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
314 315 316 317 |
# File 'lib/k_log/log_util.rb', line 314 def self.examples examples_simple # examples_complex end |
.examples_complex ⇒ Object
336 337 338 339 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 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/k_log/log_util.rb', line 336 def self.examples_complex KLog.logger.block ['Line 1', 12, 'Line 3', true, 'Line 5'] KLog.logger.progress(0, 'Section 1') KLog.logger.progress KLog.logger.progress save_progress = KLog.logger.progress KLog.logger.progress(10, 'Section 2') KLog.logger.progress KLog.logger.progress KLog.logger.progress KLog.logger.progress(save_progress, 'Section 1') KLog.logger.progress KLog.logger.progress KLog.logger.progress KLog.logger.line KLog.logger.line(20) KLog.logger.line(20, character: '-') yaml1 = {} yaml1['title'] = 'Software Architect' yaml1['age'] = 45 yaml1['name'] = 'David' yaml3 = {} yaml3['title'] = 'Developer' yaml3['age'] = 20 yaml3['name'] = 'Jin' KLog.logger.yaml(yaml1) yaml2 = OpenStruct.new yaml2.title = 'Software Architect' yaml2.age = 45 yaml2.name = 'David' KLog.logger.yaml(yaml2) mixed_yaml_array = [yaml1, yaml2] # This fails because we don't correctly pre-process the array KLog.logger.yaml(mixed_yaml_array) hash_yaml_array = [yaml1, yaml3] KLog.logger.yaml(hash_yaml_array) begin raise 'Here is an error' rescue StandardError => e KLog.logger.exception(e) end end |
.examples_simple ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/k_log/log_util.rb', line 319 def self.examples_simple KLog.logger.debug 'some debug message' KLog.logger.info 'some info message' KLog.logger.warn 'some warning message' KLog.logger.error 'some error message' KLog.logger.fatal 'some fatal message' KLog.logger.kv('First Name', 'David') KLog.logger.kv('Last Name', 'Cruwys') KLog.logger.kv('Age', 45) KLog.logger.kv('Sex', 'male') KLog.logger.heading('Heading') KLog.logger.subheading('Sub Heading') KLog.logger.section_heading('Section Heading') end |
Instance Method Details
#block(messages, include_line: true, title: nil) ⇒ Object
102 103 104 105 106 |
# File 'lib/k_log/log_util.rb', line 102 def block(, include_line: true, title: nil) lines = LogHelper.block(, include_line: include_line, title: title) info_multi_lines(lines) end |
#debug(value) ⇒ Object
Standard Accessors that are on the standard rails Logger
28 29 30 |
# File 'lib/k_log/log_util.rb', line 28 def debug(value) @logger.debug(value) end |
#dynamic_heading(heading, size: 70, type: :heading) ⇒ Object
75 76 77 78 79 |
# File 'lib/k_log/log_util.rb', line 75 def dynamic_heading(heading, size: 70, type: :heading) KLog.logger.heading(heading, size) if type == :heading KLog.logger.subheading(heading, size) if type == :subheading KLog.logger.section_heading(heading, size) if %i[section_heading section].include?(type) end |
#error(value) ⇒ Object
40 41 42 |
# File 'lib/k_log/log_util.rb', line 40 def error(value) @logger.error(value) end |
#exception(exception, style: :long, method_info: nil) ⇒ Object
Examples log.exception(e) # <- :long log.exception(e, style: :message) log.exception(e, style: :short) log.exception(e, style: :long)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/k_log/log_util.rb', line 203 def exception(exception, style: :long, method_info: nil) line unless style == :message @logger.error(KLog::LogHelper.bg_red(exception.)) @logger.info([method_info.owner.name, method_info.name].join('#')) if method_info case style when :short trace_items = exception.backtrace.select { |row| row.start_with?(Dir.pwd) } when :long trace_items = exception.backtrace else trace_items = [] end trace_items = trace_items.map { |row| row.start_with?(Dir.pwd) ? KLog::LogHelper.yellow(row) : KLog::LogHelper.red(row) } @logger.info(KLog::LogHelper.yellow(trace_items.join("\n"))) if trace_items.length.positive? line unless style == :message end |
#fatal(value) ⇒ Object
44 45 46 |
# File 'lib/k_log/log_util.rb', line 44 def fatal(value) @logger.fatal(value) end |
#heading(heading, size = 70) ⇒ Object
81 82 83 84 |
# File 'lib/k_log/log_util.rb', line 81 def heading(heading, size = 70) lines = LogHelper.heading(heading, size) info_multi_lines(lines) end |
#help_all_symbols ⇒ Object
280 281 282 283 284 285 286 |
# File 'lib/k_log/log_util.rb', line 280 def help_all_symbols # Produces a lot of data, need some sort of filter I think before this is useful Symbol.all_symbols.each do |s| info s # debug s end end |
#info(value) ⇒ Object
32 33 34 |
# File 'lib/k_log/log_util.rb', line 32 def info(value) @logger.info(value) end |
#json(data) ⇒ Object Also known as: j
135 136 137 |
# File 'lib/k_log/log_util.rb', line 135 def json(data) @logger.info(JSON.pretty_generate(data)) end |
#kv(key, value, key_width = 30) ⇒ Object
Write a Key/Value Pair Need to change this to named_param
54 55 56 57 |
# File 'lib/k_log/log_util.rb', line 54 def kv(key, value, key_width = 30) = LogHelper.kv(key, value, key_width) @logger.info() end |
#kv_hash(hash) ⇒ Object
rubocop:enable Metrics/AbcSize
255 256 257 258 259 |
# File 'lib/k_log/log_util.rb', line 255 def kv_hash(hash) hash.each do |key, value| kv(key, value) end end |
#line(size = 70, character: '=') ⇒ Object
prints out a line to the log
69 70 71 72 73 |
# File 'lib/k_log/log_util.rb', line 69 def line(size = 70, character: '=') = LogHelper.line(size, character) @logger.info() end |
#open_struct(data, indent = '', **opts) ⇒ Object Also known as: ostruct, o
rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize DEPRECATE
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 |
# File 'lib/k_log/log_util.rb', line 157 def open_struct(data, indent = '', **opts) KLog.logger.heading(opts[:heading], 88) unless opts[:heading].nil? KLog.logger.subheading(opts[:subheading], 88) unless opts[:subheading].nil? KLog.logger.section_heading(opts[:section_heading], 88) unless opts[:section_heading].nil? data.each_pair do |key, value| case value when OpenStruct if value['rows'].is_a?(Array) # KLog.logger.subheading(key) opts[:subheading] = key open_struct(value, indent, **opts) opts.delete(:subheading) else KLog.logger.kv "#{indent}#{key}", '' indent = "#{indent} " open_struct(value, indent, **opts) indent = indent.chomp(' ') end when Array next unless opts[:skip_array].nil? puts LogHelper.section_heading(opts[:subheading], 88) unless opts[:subheading].nil? if value.length.positive? if value.first.is_a?(String) || value.first.is_a?(Symbol) KLog.logger.kv "#{indent}#{key}", value.map(&:to_s).join(', ') else tp value, value.first.to_h.keys end end else KLog.logger.kv "#{indent}#{key}", value end end nil end |
#pretty_class(instance) ⇒ Object
NOTE: using pretty_inspect is an existing namespace conflict rubocop:disable Metrics/AbcSize
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/k_log/log_util.rb', line 232 def pretty_class(instance) c = instance.class line kv('Full Class', c.name) kv('Module', c.name.deconstantize) kv('Class', c.name.demodulize) source_location = c.instance_methods(false).map do |m| c.instance_method(m).source_location.first end.uniq begin kv('Source Location', source_location) rescue StandardError => e warn e end line end |
#pretty_params(params) ⇒ Object
NOTE: using pretty_inspect is an existing namespace conflict
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/k_log/log_util.rb', line 262 def pretty_params(params) line params.each do |k, v| if params[k].is_a?(Hash) params[k].each do |child_k, child_v| kv("#{k}[#{child_k}]", child_v) end else kv(k, v) end end line end |
#progress(pos = nil, section = nil) ⇒ Object
Write a progress point, progress will update on it’s own
60 61 62 63 64 65 66 |
# File 'lib/k_log/log_util.rb', line 60 def progress(pos = nil, section = nil) = LogHelper.progress(pos, section) # @logger.debug(message) @logger.info() LogHelper.progress_position end |
#section_heading(heading, size = 70) ⇒ Object
A section heading
example: [ I am a heading ]—————————————————-
96 97 98 99 100 |
# File 'lib/k_log/log_util.rb', line 96 def section_heading(heading, size = 70) heading = LogHelper.section_heading(heading, size) info(heading) end |
#structure(data, **opts) ⇒ Object
Log a structure
Can handle Hash, Array, OpenStruct, Struct, DryStruct, Hash convertible custom classes
150 151 152 153 |
# File 'lib/k_log/log_util.rb', line 150 def structure(data, **opts) structure = LogStructure.new(**opts) structure.log(data) end |
#subheading(heading, size = 70) ⇒ Object
86 87 88 89 90 |
# File 'lib/k_log/log_util.rb', line 86 def subheading(heading, size = 70) lines = LogHelper.subheading(heading, size) info_multi_lines(lines) end |
#visual_compare_hashes(hash1, hash2) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/k_log/log_util.rb', line 288 def visual_compare_hashes(hash1, hash2) content1 = JSON.pretty_generate(hash1) content2 = JSON.pretty_generate(hash2) file1 = Tempfile.new('hash1') file1.write(content1) file1.close file2 = Tempfile.new('hash2') file2.write(content2) file2.close system "code --diff #{file1.path} #{file2.path}" # Provide enough time for vscode to open and display the files before deleting them sleep 1 file1.unlink file2.unlink end |
#warn(value) ⇒ Object
36 37 38 |
# File 'lib/k_log/log_util.rb', line 36 def warn(value) @logger.warn(value) end |
#yaml(data, is_line: true) ⇒ Object
info(value) end
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/k_log/log_util.rb', line 118 def yaml(data, is_line: true) require 'yaml' line if is_line @logger.info(data.to_yaml) if data.is_a?(Hash) @logger.info(data.marshal_dump.to_yaml) if data.is_a?(OpenStruct) if data.is_a? Array data.each do |d| @logger.info(d.to_yaml) end end line if is_line end |