Class: KLog::Examples

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/k_log/examples.rb

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

#examplesObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



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

def examples
  examples_simple
  examples_complex
end

#examples_complexObject



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/k_log/examples.rb', line 39

def examples_complex
  log.block ['Line 1', 12, 'Line 3', true, 'Line 5']

  log.progress(0, 'Section 1')
  log.progress
  log.progress
  save_progress = log.progress

  log.progress(10, 'Section 2')
  log.progress
  log.progress
  log.progress

  log.progress(save_progress, 'Section 1')
  log.progress
  log.progress
  log.progress

  log.line
  log.line(20)
  log.line(20, character: '-')

  yaml1 = {}
  yaml1['title'] = 'Software Architect'
  yaml1['age'] = 45
  yaml1['name'] = 'David'

  yaml3 = {}
  yaml3['title'] = 'Developer'
  yaml3['age'] = 20
  yaml3['name'] = 'Jin'

  log.yaml(yaml1)

  yaml2 = OpenStruct.new
  yaml2.title = 'Software Architect'
  yaml2.age = 45
  yaml2.name = 'David'

  log.yaml(yaml2)

  mixed_yaml_array = [yaml1, yaml2]

  # This fails because we don't correctly pre-process the array
  log.yaml(mixed_yaml_array)

  hash_yaml_array = [yaml1, yaml3]

  log.yaml(hash_yaml_array)

  begin
    raise 'Here is an error'
  rescue StandardError => e
    log.exception(e)
  end
  begin
    raise 'Here is an error'
  rescue StandardError => e
    log.exception(e, style: :message)
  end
  begin
    raise 'Here is an error'
  rescue StandardError => e
    log.exception(e, style: :short)
  end
end

#examples_simpleObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/k_log/examples.rb', line 13

def examples_simple
  log.debug 'some debug message'
  log.info 'some info message'
  log.warn 'some warning message'
  log.error 'some error message'
  log.fatal 'some fatal message'

  log.kv('First Name', 'David')
  log.kv('Last Name', 'Cruwys')
  log.kv('Age', 45)
  log.kv('Sex', 'male')

  log.heading('Heading')
  log.subheading('Sub Heading')
  log.section_heading('Section Heading')

  data = OpenStruct.new
  data.title = 'Software Architect'
  data.age = 45
  data.name = 'David'
  data.names = %w[David Bill]
  data.status = :debug
  data.statuses = %i[debug info blah]
  log.open_struct(data, section_heading: 'Display Open Struct')
end