Class: KLog::LogStructure

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ LogStructure

Log a structure

Can handle Hash, Array, OpenStruct, Struct, DryStruct, Hash convertible custom classes

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :indent (String)

    Indent with string, defaults to ‘ ’

  • :heading (String)

    Log heading using logger.dynamic_heading

  • :heading_type (String)

    :heading, :subheading, :section_heading

  • :line_width (String)

    line width defaults to 80, but can be overridden here

  • :key_width (String)

    key width defaults to 30, but can be overridden here

  • :formatter (String)

    is a complex configuration for formatting different data within the structure

  • :convert_data_to (Symbol) — default: :raw, open_struct


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_toObject (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

#formatterObject (readonly)

Returns the value of attribute formatter.



17
18
19
# File 'lib/k_log/log_structure.rb', line 17

def formatter
  @formatter
end

#graphObject (readonly)

Returns the value of attribute graph.



16
17
18
# File 'lib/k_log/log_structure.rb', line 16

def graph
  @graph
end

#graph_nodeObject (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_pathObject (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

#headingObject (readonly)

Returns the value of attribute heading.



11
12
13
# File 'lib/k_log/log_structure.rb', line 11

def heading
  @heading
end

#heading_typeObject (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

#indentObject (readonly)

Returns the value of attribute indent.



8
9
10
# File 'lib/k_log/log_structure.rb', line 8

def indent
  @indent
end

#key_formatObject (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_widthObject (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_widthObject (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

#linesObject (readonly)

Returns the value of attribute lines.



25
26
27
# File 'lib/k_log/log_structure.rb', line 25

def lines
  @lines
end

#output_asObject (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_fileObject (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_depthObject (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_countObject (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

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/k_log/log_structure.rb', line 9

def title
  @title
end

#title_typeObject (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_contentObject



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_linesObject



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

#contentObject



86
87
88
# File 'lib/k_log/log_structure.rb', line 86

def content
  @content ||= lines.join("\n")
end

#lObject



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