class KLog::LogUtil
Simple console log helpers
Public Class Methods
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# File lib/k_log/log_util.rb, line 296 def self.examples examples_simple # examples_complex end
# File lib/k_log/log_util.rb, line 318 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
# File lib/k_log/log_util.rb, line 301 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
# File lib/k_log/log_util.rb, line 18 def initialize(logger) @logger = logger end
Public Instance Methods
# File lib/k_log/log_util.rb, line 102 def block(messages, include_line: true, title: nil) lines = LogHelper.block(messages, include_line: include_line, title: title) info_multi_lines(lines) end
# File lib/k_log/log_util.rb, line 28 def debug(value) @logger.debug(value) end
# 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
# File lib/k_log/log_util.rb, line 40 def error(value) @logger.error(value) end
# File lib/k_log/log_util.rb, line 198 def exception(exception) line @logger.info(KLog::LogHelper.bg_red(exception.message)) @logger.info(KLog::LogHelper.yellow(exception.backtrace.map { |row| row.start_with?(Dir.pwd) ? KLog::LogHelper.yellow(row) : KLog::LogHelper.red(row) }.join("\n"))) line end
# File lib/k_log/log_util.rb, line 44 def fatal(value) @logger.fatal(value) end
# File lib/k_log/log_util.rb, line 81 def heading(heading, size = 70) lines = LogHelper.heading(heading, size) info_multi_lines(lines) end
# File lib/k_log/log_util.rb, line 262 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
# File lib/k_log/log_util.rb, line 32 def info(value) @logger.info(value) end
# File lib/k_log/log_util.rb, line 135 def json(data) @logger.info(JSON.pretty_generate(data)) end
Write a Key/Value Pair Need to change this to named_param
# File lib/k_log/log_util.rb, line 54 def kv(key, value, key_width = 30) message = LogHelper.kv(key, value, key_width) @logger.info(message) end
rubocop:enable Metrics/AbcSize
# File lib/k_log/log_util.rb, line 237 def kv_hash(hash) hash.each do |key, value| kv(key, value) end end
prints out a line to the log
# File lib/k_log/log_util.rb, line 69 def line(size = 70, character: '=') message = LogHelper.line(size, character) @logger.info(message) end
rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize DEPRECATE
# 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
rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
NOTE: using pretty_inspect is an existing namespace conflict rubocop:disable Metrics/AbcSize
# File lib/k_log/log_util.rb, line 214 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
NOTE: using pretty_inspect is an existing namespace conflict
# File lib/k_log/log_util.rb, line 244 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
Write a progress point, progress will update on it's own
# File lib/k_log/log_util.rb, line 60 def progress(pos = nil, section = nil) message = LogHelper.progress(pos, section) # @logger.debug(message) @logger.info(message) LogHelper.progress_position end
A section heading
example: [ I am a heading ]—————————————————-
# File lib/k_log/log_util.rb, line 96 def section_heading(heading, size = 70) heading = LogHelper.section_heading(heading, size) info(heading) end
Log a structure
Can handle Hash, Array, OpenStruct, Struct, DryStruct, Hash convertible custom classes
@param [Hash] **opts Options @option opts [String] :indent Indent with string, defaults to '' @option opts [String] :depth is a computered @option opts [String] :heading Log title using logger.dynamic_heading @option opts [String] :heading_type :heading, :subheading, :section_heading @option opts [Boolean] :skip_array Arrays items can be skipped
# File lib/k_log/log_util.rb, line 150 def structure(data, **opts) structure = LogStructure.new(opts) structure.log(data) end
# File lib/k_log/log_util.rb, line 86 def subheading(heading, size = 70) lines = LogHelper.subheading(heading, size) info_multi_lines(lines) end
# File lib/k_log/log_util.rb, line 270 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
# File lib/k_log/log_util.rb, line 36 def warn(value) @logger.warn(value) end
info(value)
end
# 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
Private Instance Methods
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
# File lib/k_log/log_util.rb, line 378 def debug_multi_lines(lines) lines.each do |line| debug(line) end end
# File lib/k_log/log_util.rb, line 396 def error_multi_lines(lines) lines.each do |line| error(line) end end
# File lib/k_log/log_util.rb, line 402 def fatal_multi_lines(lines) lines.each do |line| fatal(line) end end
# File lib/k_log/log_util.rb, line 384 def info_multi_lines(lines) lines.each do |line| info(line) end end
# File lib/k_log/log_util.rb, line 390 def warn_multi_lines(lines) lines.each do |line| warn(line) end end