k_log 0.0.4 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72de9a7d2c858aed9802f1853ab7d141bd3fbb4641456148b294d6d8bb0f4bfc
4
- data.tar.gz: 80199689b1bff33b5033e9c8a475ff97009fd2605f8fcfc99deac3172ef6ee9a
3
+ metadata.gz: 434dc148e16a0ee8d9760e0d7e9b23777da00fbbc653bb48f79c795b265f91cc
4
+ data.tar.gz: 7f756677bcb989f589d9b9bb0ce3526e7f3b8d6e05c13dab9f936a89888954e6
5
5
  SHA512:
6
- metadata.gz: 6948881ccd96667754275eecedc0be3d6eab65d8f0f97f18def65bbf2727395eb35a2197ff3a9db49c8759880aec18afd8082864f70d029f859f6f8af6fa4534
7
- data.tar.gz: d03583cf50fa5d29c46ef7c7271c5d951277ce2718b8e2f5bc738792954da5c02d6082bde44c3a494de2fc990b19ec5c738115ae6900f06848873604e8263c91
6
+ metadata.gz: 6ae7c995344694b8a22424ec4fc974c7b23e4014b039632c3d184b77cad6ad20dc577ee5db5d0d881177013ad5e617e7347fd284b855a41347e0eb3073876b56
7
+ data.tar.gz: 597070e56e27c4d86d426f81e13a13ed1d8db000b44ebcdfe7ce9f1a75f24dc0b0162289ff5bb9ee343c0f23e8a471cbe6956f972cd882539a7a163a77260d4f
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  > K Log provides console logging helpers and formatters
4
4
 
5
+ This code is old and probably needs to be written differently, but it solves my problem of having formatted console logging within different Apps and Gems that I write.
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -62,14 +64,18 @@ L.kv('Last Name', 'Cruwys')
62
64
  L.kv('Age', 45)
63
65
  L.kv('Sex', 'male')
64
66
 
65
- L.line
66
- L.line(20)
67
- L.line(20, character: '-')
68
-
69
67
  L.heading('Heading')
70
68
  L.subheading('Sub Heading')
69
+
70
+ L.block ['Line 1', 12, 'Line 3', true, 'Line 5']
71
+
72
+ L.progress(0, 'Section 1')
73
+ L.progress
74
+ L.progress
71
75
  ```
72
76
 
77
+ ![Usage](usage.png)
78
+
73
79
  ## Development
74
80
 
75
81
  Checkout the repo
@@ -80,15 +86,6 @@ git clone klueless-io/k_log
80
86
 
81
87
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
82
88
 
83
- You can also run `bin/console` for an interactive prompt that will allow you to experiment.
84
-
85
- ```bash
86
- bin/console
87
-
88
- Aaa::Bbb::Program.execute()
89
- # => ""
90
- ```
91
-
92
89
  `k_log` is setup with Guard, run `guard`, this will watch development file changes and run tests automatically, if successful, it will then run rubocop for style quality.
93
90
 
94
91
  To release a new version, update the version number in `version.rb`, build the gem and push the `.gem` file to [rubygems.org](https://rubygems.org).
data/STORIES.md CHANGED
@@ -12,22 +12,15 @@ As a Developer, I need formatted console logs, so that information presents clea
12
12
 
13
13
  - A range of helper methods that write formatted log entries to the console
14
14
 
15
- ### Tasks next on list
15
+ ## Stories and tasks
16
+
17
+ ### Tasks - completed
16
18
 
17
19
  Setup RubyGems and RubyDoc
18
20
 
19
21
  - Build and deploy gem to [rubygems.org](https://rubygems.org/gems/k_log)
20
22
  - Attach documentation to [rubydoc.info](https://rubydoc.info/github/to-do-/k_log/master)
21
23
 
22
- Setup GitHub Action (test and lint)
23
-
24
- - Setup Rspec action
25
- - Setup RuboCop action
26
-
27
- ## Stories and tasks
28
-
29
- ### Tasks - completed
30
-
31
24
  Setup project management, requirement and SCRUM documents
32
25
 
33
26
  - Setup readme file
@@ -35,6 +28,11 @@ Setup project management, requirement and SCRUM documents
35
28
  - Setup a project backlog
36
29
  - Setup an examples/usage document
37
30
 
31
+ Setup GitHub Action (test and lint)
32
+
33
+ - Setup Rspec action
34
+ - Setup RuboCop action
35
+
38
36
  Setup new Ruby GEM
39
37
 
40
38
  - Build out a standard GEM structure
data/hooks/pre-commit CHANGED
@@ -8,7 +8,7 @@ require 'English'
8
8
 
9
9
  exit 0 if ARGV.include?('--no-verify')
10
10
 
11
- warning_keywords = %w[console.log]
11
+ warning_keywords = %w[]
12
12
  keywords = %w[binding.pry console.dir byebug debugger]
13
13
  files_changed = `git diff-index --name-only HEAD --`.split
14
14
 
@@ -23,7 +23,7 @@ files_changed -= ['Gemfile']
23
23
  files_changed -= ['Gemfile.lock']
24
24
  files_changed -= ['.gitignore']
25
25
 
26
- files_changed = files_changed.reject { |f| f.downcase.end_with?('.MD') }
26
+ files_changed = files_changed.reject { |f| f.downcase.end_with?('.mds') }
27
27
  files_changed = files_changed.reject { |f| f.downcase.end_with?('.json') }
28
28
  files_changed = files_changed.reject { |f| f.downcase.end_with?('.yml') }
29
29
 
data/k_log.gemspec CHANGED
@@ -38,5 +38,6 @@ Gem::Specification.new do |spec|
38
38
  spec.require_paths = ['lib']
39
39
  # spec.extensions = ['ext/k_log/extconf.rb']
40
40
 
41
+ spec.add_dependency 'table_print', '~> 1.5.7'
41
42
  # spec.add_dependency 'tty-box', '~> 0.5.0'
42
43
  end
data/lib/k_log.rb CHANGED
@@ -1,10 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'logger'
3
4
  require 'k_log/version'
5
+ require 'k_log/log_formatter'
6
+ require 'k_log/log_helper'
7
+ require 'k_log/log_util'
4
8
 
9
+ # Simple console log helpers
5
10
  module KLog
6
11
  # raise KLog::Error, 'Sample message'
7
12
  class Error < StandardError; end
8
13
 
9
- # Your code goes here...
14
+ class << self
15
+ attr_accessor :logger
16
+ end
10
17
  end
18
+
19
+ # KLog.logger = Logger.new($stdout)
20
+ # KLog.logger.level = Logger::DEBUG
21
+ # KLog.logger.formatter = KLog::LogFormatter.new
22
+
23
+ # L = KLog::LogUtil.new(KLog.logger)
@@ -1,36 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module KDsl
4
- module Logger
5
- # Styled Log formatter
6
- class LogFormatter < ::Logger::Formatter
7
- attr_accessor :show_caller
3
+ module KLog
4
+ # Styled Log formatter
5
+ class LogFormatter < ::Logger::Formatter
6
+ SEVERITY_TO_COLOR_MAP = {
7
+ 'DEBUG' => '34',
8
+ 'INFO' => '32',
9
+ 'WARN' => '33',
10
+ 'ERROR' => '31',
11
+ 'FATAL' => '37'
12
+ }.freeze
8
13
 
9
- SEVERITY_TO_COLOR_MAP = {
10
- 'DEBUG' => '34',
11
- 'INFO' => '32',
12
- 'WARN' => '33',
13
- 'ERROR' => '31',
14
- 'FATAL' => '37'
15
- }.freeze
14
+ def call(severity, _timestamp, _prog_name, msg)
15
+ severity = severity.upcase
16
16
 
17
- def call(severity, _timestamp, _progname, msg)
18
- severity = severity.upcase
17
+ color = SEVERITY_TO_COLOR_MAP[severity]
19
18
 
20
- color = SEVERITY_TO_COLOR_MAP[severity]
19
+ severity_value = format("\033[#{color}m%<severity>-5.5s\033[0m", { severity: severity })
21
20
 
22
- severity_value = format("\033[#{color}m%<severity>-5.5s\033[0m", { severity: severity })
21
+ msg = msg.is_a?(String) ? msg : msg.inspect
23
22
 
24
- msg = msg.is_a?(String) ? msg : msg.inspect
25
-
26
- format(
27
- "%<time>s %<severity>s %<message>s\n", {
28
- time: Time.now.strftime('%d|%H:%M:%S'),
29
- severity: severity_value,
30
- message: msg
31
- }
32
- )
33
- end
23
+ format(
24
+ "%<time>s %<severity>s %<message>s\n", {
25
+ time: Time.now.strftime('%d|%H:%M:%S'),
26
+ severity: severity_value,
27
+ message: msg
28
+ }
29
+ )
34
30
  end
35
31
  end
36
32
  end
@@ -1,119 +1,113 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Log Helper is an internal class that takes care of a lot of the formating of different content types
4
- # e.g key/values, lines, progress counters and headings
5
- # it is different to the formatter becuase the formatter is used by Rails Logger to change the output stream style and format
6
- class LogHelper
7
- @progress_section = ''
8
- @progress_position = 0
9
-
10
- class << self
11
- attr_accessor :progress_position
12
- attr_accessor :progress_section
13
- end
14
-
15
- def self.kv(key, value, key_width = 30)
16
- "#{green(key.to_s.ljust(key_width))}: #{value}"
17
- end
18
-
19
- def self.progress(pos = nil, section = nil)
20
- @progress_position = pos.nil? ? @progress_position : pos
3
+ # Log Helper is an internal class that takes care of a lot of the formatting
4
+ # of different content types e.g key/values, lines, progress counters and headings
5
+ # it is different to the formatter because the formatter is used by Rails Logger
6
+ # to change the output stream style and format
7
+ module KLog
8
+ # Simple console log helpers
9
+ class LogHelper
10
+ @progress_section = ''
11
+ @progress_position = 0
12
+
13
+ class << self
14
+ attr_accessor :progress_position
15
+ attr_accessor :progress_section
16
+ end
21
17
 
22
- unless section.nil?
23
- # Pl.info 'here'
24
- @progress_section = section
18
+ def self.kv(key, value, key_width = 30)
19
+ "#{green(key.to_s.ljust(key_width))}: #{value}"
25
20
  end
26
21
 
27
- # puts '@progress_position'
28
- # puts @progress_position
29
- # puts '@progress_section'
30
- # puts @progress_section
22
+ def self.progress(pos = nil, section = nil)
23
+ @progress_position = pos.nil? ? @progress_position : pos
31
24
 
32
- section_length = 28
25
+ @progress_section = section unless section.nil?
33
26
 
34
- section = if @progress_section.nil?
35
- ' ' * section_length
36
- else
37
- ' ' + @progress_section.ljust(section_length - 1, ' ')
38
- end
27
+ section_length = 28
39
28
 
40
- # puts 'section'
41
- # puts section
29
+ section = if @progress_section.nil?
30
+ ' ' * section_length
31
+ else
32
+ " #{@progress_section.ljust(section_length - 1, ' ')}"
33
+ end
42
34
 
43
- result = '..' + section + ':' + @progress_position.to_s.rjust(4)
35
+ result = "..#{section}:#{@progress_position.to_s.rjust(4)}"
44
36
 
45
- @progress_position += 1
37
+ @progress_position += 1
46
38
 
47
- result
48
- end
49
-
50
- def self.line(size = 70, character = '=')
51
- green(character * size)
52
- end
39
+ result
40
+ end
53
41
 
54
- def self.heading(heading, size = 70)
55
- line = line(size)
42
+ def self.line(size = 70, character = '=')
43
+ green(character * size)
44
+ end
56
45
 
57
- [
58
- line,
59
- heading,
60
- line
61
- ]
62
- end
46
+ def self.heading(heading, size = 70)
47
+ line = line(size)
63
48
 
64
- def self.subheading(heading, size = 70)
65
- line = line(size, '-')
66
- [
67
- line,
68
- heading,
69
- line
70
- ]
71
- end
49
+ [
50
+ line,
51
+ heading,
52
+ line
53
+ ]
54
+ end
72
55
 
73
- # A section heading
74
- #
75
- # example:
76
- # [ I am a heading ]----------------------------------------------------
77
- def self.section_heading(heading, size = 70)
78
- heading = "[ #{heading} ]"
79
- line = line(size - heading.length, '-')
56
+ def self.subheading(heading, size = 70)
57
+ line = line(size, '-')
58
+ [
59
+ line,
60
+ heading,
61
+ line
62
+ ]
63
+ end
80
64
 
81
- # It is important that you set the colour after you have calculated the size
82
- "#{green(heading)}#{line}"
83
- end
65
+ # A section heading
66
+ #
67
+ # example:
68
+ # [ I am a heading ]----------------------------------------------------
69
+ def self.section_heading(heading, size = 70)
70
+ brace_open = green('[ ')
71
+ brace_close = green(' ]')
72
+ line = line(size - heading.length - 4, '-')
73
+
74
+ # It is important that you set the colour after you have calculated the size
75
+ "#{brace_open}#{heading}#{brace_close}#{green(line)}"
76
+ end
84
77
 
85
- # :sql_array should be an array with SQL and values
86
- # example: L.sql(["name = :name and group_id = :value OR parent_id = :value", name: "foo'bar", value: 4])
87
- # def sql(sql_array)
88
- # value = ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
78
+ # :sql_array should be an array with SQL and values
79
+ # example: L.sql(["name = :name and group_id = :value OR parent_id = :value", name: "foo'bar", value: 4])
80
+ # def sql(sql_array)
81
+ # value = ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
89
82
 
90
- # info(value)
91
- # end
83
+ # info(value)
84
+ # end
92
85
 
93
- # rubocop:disable Metrics/CyclomaticComplexity
94
- def self.block(messages, include_line = true, title: nil)
95
- result = include_line ? [line] : []
86
+ # rubocop:disable Metrics/CyclomaticComplexity
87
+ def self.block(messages, include_line: true, title: nil)
88
+ result = include_line ? [line] : []
96
89
 
97
- unless title.nil?
98
- result.push(title)
99
- result.push(line(70, ','))
100
- end
90
+ unless title.nil?
91
+ result.push(title)
92
+ result.push(line(70, ','))
93
+ end
101
94
 
102
- result.push messages if messages.is_a?(String) || messages.is_a?(Integer)
95
+ result.push messages if messages.is_a?(String) || messages.is_a?(Integer)
103
96
 
104
- if messages.is_a? Array
105
- messages.each do |message|
106
- result.push message
97
+ if messages.is_a? Array
98
+ messages.each do |message|
99
+ result.push message
100
+ end
107
101
  end
108
- end
109
102
 
110
- result.push line if include_line
103
+ result.push line if include_line
111
104
 
112
- result
113
- end
114
- # rubocop:enable Metrics/CyclomaticComplexity
105
+ result
106
+ end
107
+ # rubocop:enable Metrics/CyclomaticComplexity
115
108
 
116
- def self.green(value)
117
- "\033[32m#{value}\033[0m"
109
+ def self.green(value)
110
+ "\033[32m#{value}\033[0m"
111
+ end
118
112
  end
119
113
  end
@@ -1,332 +1,354 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Format Logger Util provides static helper methods that delegate responsibility to the underlying
4
- # Format Logger, you can use the Util instead Rails.logger so that you have access to IDE intellisense around
5
- # available methods and so that you can use the same logger calls from controllers/models which normally have
6
- # access to to a logger variable and services which do not have access to a logger varialbe
3
+ # Format Logger Util provides static helper methods that delegate responsibility
4
+ # to the underlying Format Logger, you can use the Util instead Rails.logger so
5
+ # that you have access to IDE intellisense around available methods and so you
6
+ # can use the same logger calls from controllers/models which normally have
7
+ # access to to a logger variable and services which do not have access to a
8
+ # logger variable
7
9
  #
8
- # I usually alias the call to FormatLoggerUtil by doing L = FormatLoggerUtil
10
+ # I usually alias the call to LogUtil by doing L = LogUtil
9
11
 
10
12
  # require_relative 'format_logger'
11
13
  # require_relative 'format_logger_helper'
12
14
 
13
- class LogUtil
14
- def initialize(logger)
15
- @logger = logger
16
- @fuck = true
17
- end
15
+ module KLog
16
+ # Simple console log helpers
17
+ class LogUtil
18
+ def initialize(logger)
19
+ @logger = logger
20
+ end
18
21
 
19
- # include ActiveSupport::LoggerThreadSafeLevel
20
- # include LoggerSilence
22
+ # include ActiveSupport::LoggerThreadSafeLevel
23
+ # include LoggerSilence
21
24
 
22
- #----------------------------------------------------------------------------------------------------
23
- # Standard Accessors that are on the standard rails Logger
24
- #----------------------------------------------------------------------------------------------------
25
- def debug(value)
26
- @logger.debug(value)
27
- end
25
+ #----------------------------------------------------------------------------------------------------
26
+ # Standard Accessors that are on the standard rails Logger
27
+ #----------------------------------------------------------------------------------------------------
28
+ def debug(value)
29
+ @logger.debug(value)
30
+ end
28
31
 
29
- def info(value)
30
- @logger.info(value)
31
- end
32
+ def info(value)
33
+ @logger.info(value)
34
+ end
32
35
 
33
- def warn(value)
34
- @logger.warn(value)
35
- end
36
+ def warn(value)
37
+ @logger.warn(value)
38
+ end
36
39
 
37
- def error(value)
38
- @logger.error(value)
39
- end
40
+ def error(value)
41
+ @logger.error(value)
42
+ end
40
43
 
41
- def fatal(value)
42
- @logger.fatal(value)
43
- end
44
+ def fatal(value)
45
+ @logger.fatal(value)
46
+ end
44
47
 
45
- #----------------------------------------------------------------------------------------------------
46
- # Helper Log output Methods
47
- #----------------------------------------------------------------------------------------------------
48
+ #----------------------------------------------------------------------------------------------------
49
+ # Helper Log output Methods
50
+ #----------------------------------------------------------------------------------------------------
48
51
 
49
- # Write a Key/Value Pair
50
- def kv(key, value, key_width = 30)
51
- message = LogHelper.kv(key, value, key_width)
52
- @logger.info(message)
53
- end
52
+ # Write a Key/Value Pair
53
+ def kv(key, value, key_width = 30)
54
+ message = LogHelper.kv(key, value, key_width)
55
+ @logger.info(message)
56
+ end
54
57
 
55
- # Write a progress point, progress will update on it's own
56
- def progress(pos = nil, section = nil)
57
- message = LogHelper.progress(pos, section)
58
- # @logger.debug(message)
59
- @logger.info(message)
58
+ # Write a progress point, progress will update on it's own
59
+ def progress(pos = nil, section = nil)
60
+ message = LogHelper.progress(pos, section)
61
+ # @logger.debug(message)
62
+ @logger.info(message)
60
63
 
61
- LogHelper.progress_position
62
- end
64
+ LogHelper.progress_position
65
+ end
63
66
 
64
- # prints out a line to the log
65
- def line(size = 70, character: '=')
66
- message = LogHelper.line(size, character)
67
+ # prints out a line to the log
68
+ def line(size = 70, character: '=')
69
+ message = LogHelper.line(size, character)
67
70
 
68
- @logger.info(message)
69
- end
71
+ @logger.info(message)
72
+ end
70
73
 
71
- def heading(heading, size = 70)
72
- lines = LogHelper.heading(heading, size)
73
- info_multi_lines(lines)
74
- end
74
+ def heading(heading, size = 70)
75
+ lines = LogHelper.heading(heading, size)
76
+ info_multi_lines(lines)
77
+ end
75
78
 
76
- def subheading(heading, size = 70)
77
- lines = LogHelper.subheading(heading, size)
79
+ def subheading(heading, size = 70)
80
+ lines = LogHelper.subheading(heading, size)
78
81
 
79
- info_multi_lines(lines)
80
- end
82
+ info_multi_lines(lines)
83
+ end
81
84
 
82
- # A section heading
83
- #
84
- # example:
85
- # [ I am a heading ]----------------------------------------------------
86
- def section_heading(heading, size = 70)
87
- heading = LogHelper.section_heading(heading, size)
85
+ # A section heading
86
+ #
87
+ # example:
88
+ # [ I am a heading ]----------------------------------------------------
89
+ def section_heading(heading, size = 70)
90
+ heading = LogHelper.section_heading(heading, size)
88
91
 
89
- info(heading)
90
- end
92
+ info(heading)
93
+ end
91
94
 
92
- def block(messages, include_line = true, title: nil)
93
- lines = LogHelper.block(messages, include_line, title: title)
95
+ def block(messages, include_line: true, title: nil)
96
+ lines = LogHelper.block(messages, include_line: include_line, title: title)
94
97
 
95
- info_multi_lines(lines)
96
- end
98
+ info_multi_lines(lines)
99
+ end
97
100
 
98
- # # :sql_array should be an array with SQL and values or with SQL and Hash
99
- # # example:
100
- # # L.sql(["name = :name and group_id = :value OR parent_id = :value", name: "foo'bar", value: 4])
101
- # # L.sql([sql_exact_match_skills_in_use, {names: self.segments_container.segment_values}])
102
- # def sql(sql_array)
103
- # value = ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
101
+ # # :sql_array should be an array with SQL and values or with SQL and Hash
102
+ # # example:
103
+ # # L.sql(["name = :name and group_id = :value OR parent_id = :value", name: "foo'bar", value: 4])
104
+ # # L.sql([sql_exact_match_skills_in_use, {names: self.segments_container.segment_values}])
105
+ # def sql(sql_array)
106
+ # value = ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
104
107
 
105
- # info(value)
106
- # end
108
+ # info(value)
109
+ # end
107
110
 
108
- def yaml(data, is_line = true)
109
- line if is_line
111
+ def yaml(data, is_line: true)
112
+ require 'yaml'
113
+ line if is_line
110
114
 
111
- @logger.info(data.to_yaml) if data.is_a?(Hash)
115
+ @logger.info(data.to_yaml) if data.is_a?(Hash)
112
116
 
113
- @logger.info(data.marshal_dump.to_yaml) if data.is_a?(OpenStruct)
117
+ @logger.info(data.marshal_dump.to_yaml) if data.is_a?(OpenStruct)
114
118
 
115
- if data.is_a? Array
116
- data.each do |d|
117
- @logger.info(d.to_yaml)
119
+ if data.is_a? Array
120
+ data.each do |d|
121
+ @logger.info(d.to_yaml)
122
+ end
118
123
  end
119
- end
120
124
 
121
- line if is_line
122
- end
125
+ line if is_line
126
+ end
123
127
 
124
- def json(data)
125
- @logger.info(JSON.pretty_generate(data))
126
- end
127
- alias j json
128
-
129
- def open_struct(data, indent = '', **opts)
130
- data.each_pair do |key, value|
131
- if value.is_a?(OpenStruct)
132
- if value['rows'].is_a?(Array)
133
- # L.subheading(key)
134
- opts[:subheading] = key
135
- open_struct(value, indent, **opts)
136
- opts.delete(:subheading)
137
- else
138
- L.kv "#{indent}#{key}", ''
139
- indent = "#{indent} "
140
- open_struct(value, indent, **opts)
141
- indent = indent.chomp(' ')
142
- end
143
- elsif value.is_a?(Array)
144
- next if opts[:skip_array].present?
145
- # puts LogHelper.subheading(key, 88)# if opts[:subheading].present?
146
- puts LogHelper.subheading(opts[:subheading], 88) if opts[:subheading].present?
147
- if value.length > 0
148
- if value.first.is_a?(String)
149
- L.kv "#{indent}#{key}", value.join(', ')
128
+ def json(data)
129
+ @logger.info(JSON.pretty_generate(data))
130
+ end
131
+ alias j json
132
+
133
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
134
+ def open_struct(data, indent = '', **opts)
135
+ data.each_pair do |key, value|
136
+ case value
137
+ when OpenStruct
138
+ if value['rows'].is_a?(Array)
139
+ # L.subheading(key)
140
+ opts[:subheading] = key
141
+ open_struct(value, indent, **opts)
142
+ opts.delete(:subheading)
150
143
  else
151
- tp value, value.first.to_h.keys
144
+ L.kv "#{indent}#{key}", ''
145
+ indent = "#{indent} "
146
+ open_struct(value, indent, **opts)
147
+ indent = indent.chomp(' ')
152
148
  end
149
+ when Array
150
+ next unless opts[:skip_array].nil?
151
+
152
+ # puts LogHelper.subheading(key, 88)# unless opts[:subheading].nil?
153
+ puts LogHelper.subheading(opts[:subheading], 88) unless opts[:subheading].nil?
154
+
155
+ if value.length.positive?
156
+ if value.first.is_a?(String)
157
+ L.kv "#{indent}#{key}", value.join(', ')
158
+ else
159
+ tp value, value.first.to_h.keys
160
+ end
161
+ end
162
+ else
163
+ L.kv "#{indent}#{key}", value
153
164
  end
154
- else
155
- L.kv "#{indent}#{key}", value
156
165
  end
166
+ nil
157
167
  end
158
- nil
159
- end
160
- alias ostruct open_struct
161
- alias o open_struct
168
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
169
+ alias ostruct open_struct
170
+ alias o open_struct
162
171
 
163
- def exception(e)
164
- line
172
+ def exception(exception)
173
+ line
165
174
 
166
- @logger.info(e.message)
167
- @logger.info(e.backtrace.join("\n"))
175
+ @logger.info(exception.message)
176
+ @logger.info(exception.backtrace.join("\n"))
168
177
 
169
- line
170
- end
178
+ line
179
+ end
180
+
181
+ #----------------------------------------------------------------------------------------------------
182
+ # Pretty Loggers
183
+ #----------------------------------------------------------------------------------------------------
171
184
 
172
- #----------------------------------------------------------------------------------------------------
173
- # Pretty Loggers
174
- #----------------------------------------------------------------------------------------------------
185
+ # NOTE: using pretty_inspect is an existing namespace conflict
186
+ # rubocop:disable Metrics/AbcSize
187
+ def pretty_class(instance)
188
+ c = instance.class
175
189
 
176
- # NOTE: using pretty_inspect is an existing namespace conflict
177
- def pretty_class(instance)
178
- c = instance.class
190
+ line
179
191
 
180
- line
192
+ kv('Full Class', c.name)
193
+ kv('Module', c.name.deconstantize)
194
+ kv('Class', c.name.demodulize)
181
195
 
182
- kv('Full Class', c.name)
183
- kv('Module', c.name.deconstantize)
184
- kv('Class', c.name.demodulize)
196
+ source_location = c.instance_methods(false).map do |m|
197
+ c.instance_method(m).source_location.first
198
+ end.uniq
185
199
 
186
- source_location = c.instance_methods(false).map { |m|
187
- c.instance_method(m).source_location.first
188
- }.uniq
200
+ begin
201
+ kv('Source Location', source_location)
202
+ rescue StandardError => e
203
+ warn e
204
+ end
189
205
 
190
- begin
191
- kv('Source Location', source_location)
192
- rescue => e
193
- warn e
206
+ line
194
207
  end
208
+ # rubocop:enable Metrics/AbcSize
195
209
 
196
- line
197
- end
210
+ # NOTE: using pretty_inspect is an existing namespace conflict
211
+ def pretty_params(params)
212
+ line
198
213
 
199
- # NOTE: using pretty_inspect is an existing namespace conflict
200
- def pretty_params(params)
201
- line
214
+ params.each do |k, v|
215
+ if params[k].is_a?(Hash)
202
216
 
203
- params.each do |k,v|
204
- if (params[k].is_a?(Hash))
217
+ params[k].each do |child_k, child_v|
218
+ kv("#{k}[#{child_k}]", child_v)
219
+ end
205
220
 
206
- params[k].each do |childK, childV|
207
- kv("#{k}[#{childK}]",childV)
221
+ else
222
+ kv(k, v)
208
223
  end
209
-
210
- else
211
- kv(k, v)
212
224
  end
213
- end
214
-
215
- line
216
- end
217
225
 
218
- def help_all_symbols
219
- # Produces a lot of data, need some sort of filter I think before this is useful
220
- Symbol.all_symbols.each do |s|
221
- info s
222
- # debug s
226
+ line
223
227
  end
224
- end
225
228
 
226
- #----------------------------------------------------------------------------------------------------
227
- # Internal Methods
228
- #----------------------------------------------------------------------------------------------------
229
+ def help_all_symbols
230
+ # Produces a lot of data, need some sort of filter I think before this is useful
231
+ Symbol.all_symbols.each do |s|
232
+ info s
233
+ # debug s
234
+ end
235
+ end
229
236
 
230
- def self.samples
231
- L.debug 'some debug message'
232
- L.info 'some info message'
233
- L.warn 'some warning message'
234
- L.error 'some error message'
235
- L.fatal 'some fatal message'
237
+ #----------------------------------------------------------------------------------------------------
238
+ # Internal Methods
239
+ #----------------------------------------------------------------------------------------------------
236
240
 
237
- L.kv('First Name', 'David')
238
- L.kv('Last Name', 'Cruwys')
239
- L.kv('Age', 45)
240
- L.kv('Sex', 'male')
241
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
242
+ def self.examples
243
+ examples_simple
244
+ # examples_complex
245
+ end
241
246
 
242
- L.progress(0, 'Section 1')
243
- L.progress
244
- L.progress
245
- save_progress = L.progress
247
+ def self.examples_simple
248
+ L.debug 'some debug message'
249
+ L.info 'some info message'
250
+ L.warn 'some warning message'
251
+ L.error 'some error message'
252
+ L.fatal 'some fatal message'
253
+
254
+ L.kv('First Name', 'David')
255
+ L.kv('Last Name', 'Cruwys')
256
+ L.kv('Age', 45)
257
+ L.kv('Sex', 'male')
258
+
259
+ L.heading('Heading')
260
+ L.subheading('Sub Heading')
261
+ L.section_heading('Section Heading')
262
+ end
246
263
 
247
- L.progress(10, 'Section 2')
248
- L.progress
249
- L.progress
250
- L.progress
264
+ def self.examples_complex
265
+ L.block ['Line 1', 12, 'Line 3', true, 'Line 5']
251
266
 
252
- L.progress(save_progress, 'Section 1')
253
- L.progress
254
- L.progress
255
- L.progress
267
+ L.progress(0, 'Section 1')
268
+ L.progress
269
+ L.progress
270
+ save_progress = L.progress
256
271
 
257
- L.line
258
- L.line(20)
259
- L.line(20, character: '-')
272
+ L.progress(10, 'Section 2')
273
+ L.progress
274
+ L.progress
275
+ L.progress
260
276
 
261
- L.heading('Heading')
262
- L.subheading('Sub Heading')
277
+ L.progress(save_progress, 'Section 1')
278
+ L.progress
279
+ L.progress
280
+ L.progress
263
281
 
264
- L.block ['Line 1', 12, 'Line 3', true,'Line 5']
282
+ L.line
283
+ L.line(20)
284
+ L.line(20, character: '-')
265
285
 
266
- yaml1 = Hash.new
267
- yaml1['title'] = 'Software Architect'
268
- yaml1['age'] = 45
269
- yaml1['name'] = 'David'
286
+ yaml1 = {}
287
+ yaml1['title'] = 'Software Architect'
288
+ yaml1['age'] = 45
289
+ yaml1['name'] = 'David'
270
290
 
271
- yaml3 = Hash.new
272
- yaml3['title'] = 'Develoer'
273
- yaml3['age'] = 20
274
- yaml3['name'] = 'Jin'
291
+ yaml3 = {}
292
+ yaml3['title'] = 'Developer'
293
+ yaml3['age'] = 20
294
+ yaml3['name'] = 'Jin'
275
295
 
276
- L.yaml(yaml1)
296
+ L.yaml(yaml1)
277
297
 
278
- yaml2 = OpenStruct.new
279
- yaml2.title = 'Software Architect'
280
- yaml2.age = 45
281
- yaml2.name = 'David'
298
+ yaml2 = OpenStruct.new
299
+ yaml2.title = 'Software Architect'
300
+ yaml2.age = 45
301
+ yaml2.name = 'David'
282
302
 
283
- L.yaml(yaml2)
303
+ L.yaml(yaml2)
284
304
 
285
- mixed_yaml_array = [yaml1, yaml2]
305
+ mixed_yaml_array = [yaml1, yaml2]
286
306
 
287
- # This fails because we don't correctly pre-process the array
288
- L.yaml(mixed_yaml_array)
307
+ # This fails because we don't correctly pre-process the array
308
+ L.yaml(mixed_yaml_array)
289
309
 
290
- hash_yaml_array = [yaml1, yaml3]
310
+ hash_yaml_array = [yaml1, yaml3]
291
311
 
292
- L.yaml(hash_yaml_array)
312
+ L.yaml(hash_yaml_array)
293
313
 
294
- begin
295
- raise 'Here is an error'
296
- rescue StandardError => e
297
- L.exception(e)
314
+ begin
315
+ raise 'Here is an error'
316
+ rescue StandardError => e
317
+ L.exception(e)
318
+ end
298
319
  end
299
- end
320
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
300
321
 
301
- private
322
+ private
302
323
 
303
- def debug_multi_lines(lines)
304
- lines.each do |line|
305
- debug(line)
324
+ def debug_multi_lines(lines)
325
+ lines.each do |line|
326
+ debug(line)
327
+ end
306
328
  end
307
- end
308
329
 
309
- def info_multi_lines(lines)
310
- lines.each do |line|
311
- info(line)
330
+ def info_multi_lines(lines)
331
+ lines.each do |line|
332
+ info(line)
333
+ end
312
334
  end
313
- end
314
335
 
315
- def warn_multi_lines(lines)
316
- lines.each do |line|
317
- warn(line)
336
+ def warn_multi_lines(lines)
337
+ lines.each do |line|
338
+ warn(line)
339
+ end
318
340
  end
319
- end
320
341
 
321
- def error_multi_lines(lines)
322
- lines.each do |line|
323
- error(line)
342
+ def error_multi_lines(lines)
343
+ lines.each do |line|
344
+ error(line)
345
+ end
324
346
  end
325
- end
326
347
 
327
- def fatal_multi_lines(lines)
328
- lines.each do |line|
329
- fatal(line)
348
+ def fatal_multi_lines(lines)
349
+ lines.each do |line|
350
+ fatal(line)
351
+ end
330
352
  end
331
353
  end
332
354
  end
data/lib/k_log/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KLog
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.10'
5
5
  end
data/usage.png ADDED
Binary file
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: k_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cruwys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-30 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: table_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.7
13
27
  description: " KLog provides console logging helpers and formatters\n"
14
28
  email:
15
29
  - david@ideasmen.com.au
@@ -42,6 +56,7 @@ files:
42
56
  - lib/k_log/log_helper.rb
43
57
  - lib/k_log/log_util.rb
44
58
  - lib/k_log/version.rb
59
+ - usage.png
45
60
  homepage: http://appydave.com/gems/k-log
46
61
  licenses:
47
62
  - MIT