Class: KLog::LogStructure
- Inherits:
-
Object
- Object
- KLog::LogStructure
- Defined in:
- lib/k_log/log_structure.rb
Overview
Log Structure is flexible logger for working through a complex object graph
Defined Under Namespace
Classes: GraphNode, TablePrintIo
Instance Attribute Summary collapse
-
#convert_data_to ⇒ Object
readonly
Returns the value of attribute convert_data_to.
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#graph_node ⇒ Object
readonly
Returns the value of attribute graph_node.
-
#graph_path ⇒ Object
readonly
Returns the value of attribute graph_path.
-
#heading ⇒ Object
readonly
Returns the value of attribute heading.
-
#heading_type ⇒ Object
readonly
Returns the value of attribute heading_type.
-
#indent ⇒ Object
readonly
Returns the value of attribute indent.
-
#key_format ⇒ Object
readonly
Returns the value of attribute key_format.
-
#key_width ⇒ Object
readonly
Returns the value of attribute key_width.
-
#line_width ⇒ Object
readonly
Returns the value of attribute line_width.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#output_as ⇒ Object
readonly
Returns the value of attribute output_as.
-
#output_file ⇒ Object
readonly
Returns the value of attribute output_file.
-
#recursion_depth ⇒ Object
readonly
Returns the value of attribute recursion_depth.
-
#show_array_count ⇒ Object
readonly
Returns the value of attribute show_array_count.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#title_type ⇒ Object
readonly
Returns the value of attribute title_type.
Instance Method Summary collapse
- #add_line(line) ⇒ Object
- #add_lines(lines) ⇒ Object
- #clean_content ⇒ Object
- #clean_lines ⇒ Object
- #content ⇒ Object
-
#initialize(**opts) ⇒ LogStructure
constructor
Log a structure.
- #l ⇒ Object
- #log(data) ⇒ Object
Constructor Details
#initialize(**opts) ⇒ LogStructure
Log a structure
Can handle Hash, Array, OpenStruct, Struct, DryStruct, Hash convertible custom classes
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/k_log/log_structure.rb', line 40 def initialize(**opts) @indent = opts[:indent] || ' ' @title = opts[:title] @title_type = opts[:title_type] || :heading @heading = opts[:heading] @heading_type = opts[:heading_type] || :heading puts ':heading should be :title' if opts[:heading] puts ':heading_type should be :title_type' if opts[:heading_type] @formatter = opts[:formatter] || {} @graph = parse_graph(opts[:graph] || {}) @convert_data_to = opts[:convert_data_to] || :raw # by default leave data as is @line_width = opts[:line_width] || 80 @key_width = opts[:key_width] || 30 @show_array_count = opts[:show_array_count] || false @output_as = opts[:output_as] || [:console] @output_as = [@output_as] unless @output_as.is_a?(Array) @output_file = opts[:output_file] @recursion_depth = 0 @key_format = nil @graph_path = [] @lines = [] update_indent_label end |
Instance Attribute Details
#convert_data_to ⇒ Object (readonly)
Returns the value of attribute convert_data_to.
18 19 20 |
# File 'lib/k_log/log_structure.rb', line 18 def convert_data_to @convert_data_to end |
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
17 18 19 |
# File 'lib/k_log/log_structure.rb', line 17 def formatter @formatter end |
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
16 17 18 |
# File 'lib/k_log/log_structure.rb', line 16 def graph @graph end |
#graph_node ⇒ Object (readonly)
Returns the value of attribute graph_node.
23 24 25 |
# File 'lib/k_log/log_structure.rb', line 23 def graph_node @graph_node end |
#graph_path ⇒ Object (readonly)
Returns the value of attribute graph_path.
22 23 24 |
# File 'lib/k_log/log_structure.rb', line 22 def graph_path @graph_path end |
#heading ⇒ Object (readonly)
Returns the value of attribute heading.
11 12 13 |
# File 'lib/k_log/log_structure.rb', line 11 def heading @heading end |
#heading_type ⇒ Object (readonly)
Returns the value of attribute heading_type.
12 13 14 |
# File 'lib/k_log/log_structure.rb', line 12 def heading_type @heading_type end |
#indent ⇒ Object (readonly)
Returns the value of attribute indent.
8 9 10 |
# File 'lib/k_log/log_structure.rb', line 8 def indent @indent end |
#key_format ⇒ Object (readonly)
Returns the value of attribute key_format.
21 22 23 |
# File 'lib/k_log/log_structure.rb', line 21 def key_format @key_format end |
#key_width ⇒ Object (readonly)
Returns the value of attribute key_width.
14 15 16 |
# File 'lib/k_log/log_structure.rb', line 14 def key_width @key_width end |
#line_width ⇒ Object (readonly)
Returns the value of attribute line_width.
13 14 15 |
# File 'lib/k_log/log_structure.rb', line 13 def line_width @line_width end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
25 26 27 |
# File 'lib/k_log/log_structure.rb', line 25 def lines @lines end |
#output_as ⇒ Object (readonly)
Returns the value of attribute output_as.
26 27 28 |
# File 'lib/k_log/log_structure.rb', line 26 def output_as @output_as end |
#output_file ⇒ Object (readonly)
Returns the value of attribute output_file.
27 28 29 |
# File 'lib/k_log/log_structure.rb', line 27 def output_file @output_file end |
#recursion_depth ⇒ Object (readonly)
Returns the value of attribute recursion_depth.
20 21 22 |
# File 'lib/k_log/log_structure.rb', line 20 def recursion_depth @recursion_depth end |
#show_array_count ⇒ Object (readonly)
Returns the value of attribute show_array_count.
15 16 17 |
# File 'lib/k_log/log_structure.rb', line 15 def show_array_count @show_array_count end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
9 10 11 |
# File 'lib/k_log/log_structure.rb', line 9 def title @title end |
#title_type ⇒ Object (readonly)
Returns the value of attribute title_type.
10 11 12 |
# File 'lib/k_log/log_structure.rb', line 10 def title_type @title_type end |
Instance Method Details
#add_line(line) ⇒ Object
104 105 106 |
# File 'lib/k_log/log_structure.rb', line 104 def add_line(line) @lines << line end |
#add_lines(lines) ⇒ Object
100 101 102 |
# File 'lib/k_log/log_structure.rb', line 100 def add_lines(lines) @lines += lines end |
#clean_content ⇒ Object
90 91 92 93 |
# File 'lib/k_log/log_structure.rb', line 90 def clean_content # remove color escape codes @clean_content ||= content.gsub(/\x1B\[\d*m/, '') end |
#clean_lines ⇒ Object
95 96 97 98 |
# File 'lib/k_log/log_structure.rb', line 95 def clean_lines # remove color escape codes lines.flat_map { |line| line.gsub(/\x1B\[\d*m/, '').split("\n") } end |
#content ⇒ Object
86 87 88 |
# File 'lib/k_log/log_structure.rb', line 86 def content @content ||= lines.join("\n") end |
#l ⇒ Object
69 70 71 |
# File 'lib/k_log/log_structure.rb', line 69 def l @l ||= KLog::LogUtil.new(KLog.logger) end |
#log(data) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/k_log/log_structure.rb', line 73 def log(data) return puts 'log.structure(data) is nil' if data.nil? log_heading(title, title_type) if title data = convert_data(data) log_data(data) add_line(KLog::LogHelper.line(line_width)) render_output end |