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



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

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



102
103
104
# File 'lib/k_log/log_structure.rb', line 102

def add_line(line)
  @lines << line
end

#add_lines(lines) ⇒ Object



98
99
100
# File 'lib/k_log/log_structure.rb', line 98

def add_lines(lines)
  @lines += lines
end

#clean_contentObject



88
89
90
91
# File 'lib/k_log/log_structure.rb', line 88

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

#clean_linesObject



93
94
95
96
# File 'lib/k_log/log_structure.rb', line 93

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

#contentObject



84
85
86
# File 'lib/k_log/log_structure.rb', line 84

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

#lObject



68
69
70
# File 'lib/k_log/log_structure.rb', line 68

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

#log(data) ⇒ Object



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

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