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



38
39
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
# File 'lib/k_log/log_structure.rb', line 38

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
  @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.



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

def convert_data_to
  @convert_data_to
end

#formatterObject (readonly)

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#graphObject (readonly)

Returns the value of attribute graph.



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

def graph
  @graph
end

#graph_nodeObject (readonly)

Returns the value of attribute graph_node.



22
23
24
# File 'lib/k_log/log_structure.rb', line 22

def graph_node
  @graph_node
end

#graph_pathObject (readonly)

Returns the value of attribute graph_path.



21
22
23
# File 'lib/k_log/log_structure.rb', line 21

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.



20
21
22
# File 'lib/k_log/log_structure.rb', line 20

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.



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

def lines
  @lines
end

#output_asObject (readonly)

Returns the value of attribute output_as.



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

def output_as
  @output_as
end

#output_fileObject (readonly)

Returns the value of attribute output_file.



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

def output_file
  @output_file
end

#recursion_depthObject (readonly)

Returns the value of attribute recursion_depth.



19
20
21
# File 'lib/k_log/log_structure.rb', line 19

def recursion_depth
  @recursion_depth
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



100
101
102
# File 'lib/k_log/log_structure.rb', line 100

def add_line(line)
  @lines << line
end

#add_lines(lines) ⇒ Object



96
97
98
# File 'lib/k_log/log_structure.rb', line 96

def add_lines(lines)
  @lines += lines
end

#clean_contentObject



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

def clean_content
  # remove color escape codes
  @clean_content ||= content.gsub(/\x1B\[\d*m/, '')
end

#clean_linesObject



91
92
93
94
# File 'lib/k_log/log_structure.rb', line 91

def clean_lines
  # remove color escape codes
  lines.flat_map { |line| line.gsub(/\x1B\[\d*m/, '').split("\n") }
end

#contentObject



82
83
84
# File 'lib/k_log/log_structure.rb', line 82

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

#lObject



66
67
68
# File 'lib/k_log/log_structure.rb', line 66

def l
  @l ||= KLog::LogUtil.new(KLog.logger)
end

#log(data) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/k_log/log_structure.rb', line 70

def log(data)
  log_heading(title, title_type) if title

  data = convert_data(data)

  log_data(data)

  add_line(KLog::LogHelper.line(line_width))

  render_output
end