k_log 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +10 -13
- data/STORIES.md +8 -10
- data/hooks/pre-commit +2 -2
- data/lib/k_log.rb +14 -1
- data/lib/k_log/log_formatter.rb +22 -26
- data/lib/k_log/log_helper.rb +83 -90
- data/lib/k_log/log_util.rb +253 -240
- data/lib/k_log/version.rb +1 -1
- data/usage.png +0 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f34eecb4cb24782290842b9e92e5ea8aefd756622719ec08f9db89c34922df89
|
4
|
+
data.tar.gz: 13dca8550835143913a74152f9cbedc16c724077fbe8d36f460920b9b54b9c69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a869ac41dee2845a7270ab4248452f8fcebd6c8a055dbfdb849e43f428144eabeae7553c13f723982ff90c897d9fe959de97fbe1eb81945c666c1abdbe62931
|
7
|
+
data.tar.gz: d3f1a36c7d775088dc9139965118f46131527f801385f355d7b3c75630b767cdb6036d865e018857736c94f7f2d1efe623731eeaadaf41e6460d96a6406e289f
|
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
|
+

|
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
|
-
|
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[
|
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?('.
|
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/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
|
-
|
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)
|
data/lib/k_log/log_formatter.rb
CHANGED
@@ -1,36 +1,32 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
-
|
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
|
-
|
18
|
-
severity = severity.upcase
|
17
|
+
color = SEVERITY_TO_COLOR_MAP[severity]
|
19
18
|
|
20
|
-
|
19
|
+
severity_value = format("\033[#{color}m%<severity>-5.5s\033[0m", { severity: severity })
|
21
20
|
|
22
|
-
|
21
|
+
msg = msg.is_a?(String) ? msg : msg.inspect
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
data/lib/k_log/log_helper.rb
CHANGED
@@ -1,119 +1,112 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Log Helper is an internal class that takes care of a lot of the
|
4
|
-
# e.g key/values, lines, progress counters and headings
|
5
|
-
# it is different to the formatter
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
23
|
-
#
|
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
|
-
|
28
|
-
|
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
|
-
|
25
|
+
@progress_section = section unless section.nil?
|
33
26
|
|
34
|
-
|
35
|
-
' ' * section_length
|
36
|
-
else
|
37
|
-
' ' + @progress_section.ljust(section_length - 1, ' ')
|
38
|
-
end
|
27
|
+
section_length = 28
|
39
28
|
|
40
|
-
|
41
|
-
|
29
|
+
section = if @progress_section.nil?
|
30
|
+
' ' * section_length
|
31
|
+
else
|
32
|
+
" #{@progress_section.ljust(section_length - 1, ' ')}"
|
33
|
+
end
|
42
34
|
|
43
|
-
|
35
|
+
result = "..#{section}:#{@progress_position.to_s.rjust(4)}"
|
44
36
|
|
45
|
-
|
37
|
+
@progress_position += 1
|
46
38
|
|
47
|
-
|
48
|
-
|
39
|
+
result
|
40
|
+
end
|
49
41
|
|
50
|
-
|
51
|
-
|
52
|
-
|
42
|
+
def self.line(size = 70, character = '=')
|
43
|
+
green(character * size)
|
44
|
+
end
|
53
45
|
|
54
|
-
|
55
|
-
|
46
|
+
def self.heading(heading, size = 70)
|
47
|
+
line = line(size)
|
56
48
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
49
|
+
[
|
50
|
+
line,
|
51
|
+
heading,
|
52
|
+
line
|
53
|
+
]
|
54
|
+
end
|
63
55
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
56
|
+
def self.subheading(heading, size = 70)
|
57
|
+
line = line(size, '-')
|
58
|
+
[
|
59
|
+
line,
|
60
|
+
heading,
|
61
|
+
line
|
62
|
+
]
|
63
|
+
end
|
72
64
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
65
|
+
# A section heading
|
66
|
+
#
|
67
|
+
# example:
|
68
|
+
# [ I am a heading ]----------------------------------------------------
|
69
|
+
def self.section_heading(heading, size = 70)
|
70
|
+
heading = "[ #{heading} ]"
|
71
|
+
line = line(size - heading.length, '-')
|
80
72
|
|
81
|
-
|
82
|
-
|
83
|
-
|
73
|
+
# It is important that you set the colour after you have calculated the size
|
74
|
+
"#{green(heading)}#{line}"
|
75
|
+
end
|
84
76
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
77
|
+
# :sql_array should be an array with SQL and values
|
78
|
+
# example: L.sql(["name = :name and group_id = :value OR parent_id = :value", name: "foo'bar", value: 4])
|
79
|
+
# def sql(sql_array)
|
80
|
+
# value = ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
|
89
81
|
|
90
|
-
|
91
|
-
|
82
|
+
# info(value)
|
83
|
+
# end
|
92
84
|
|
93
|
-
|
94
|
-
|
95
|
-
|
85
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
86
|
+
def self.block(messages, include_line: true, title: nil)
|
87
|
+
result = include_line ? [line] : []
|
96
88
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
89
|
+
unless title.nil?
|
90
|
+
result.push(title)
|
91
|
+
result.push(line(70, ','))
|
92
|
+
end
|
101
93
|
|
102
|
-
|
94
|
+
result.push messages if messages.is_a?(String) || messages.is_a?(Integer)
|
103
95
|
|
104
|
-
|
105
|
-
|
106
|
-
|
96
|
+
if messages.is_a? Array
|
97
|
+
messages.each do |message|
|
98
|
+
result.push message
|
99
|
+
end
|
107
100
|
end
|
108
|
-
end
|
109
101
|
|
110
|
-
|
102
|
+
result.push line if include_line
|
111
103
|
|
112
|
-
|
113
|
-
|
114
|
-
|
104
|
+
result
|
105
|
+
end
|
106
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
115
107
|
|
116
|
-
|
117
|
-
|
108
|
+
def self.green(value)
|
109
|
+
"\033[32m#{value}\033[0m"
|
110
|
+
end
|
118
111
|
end
|
119
112
|
end
|
data/lib/k_log/log_util.rb
CHANGED
@@ -1,332 +1,345 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Format Logger Util provides static helper methods that delegate responsibility
|
4
|
-
# Format Logger, you can use the Util instead Rails.logger so
|
5
|
-
#
|
6
|
-
#
|
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
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
module KLog
|
16
|
+
# Simple console log helpers
|
17
|
+
class LogUtil
|
18
|
+
def initialize(logger)
|
19
|
+
@logger = logger
|
20
|
+
end
|
18
21
|
|
19
|
-
|
20
|
-
|
22
|
+
# include ActiveSupport::LoggerThreadSafeLevel
|
23
|
+
# include LoggerSilence
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
+
def info(value)
|
33
|
+
@logger.info(value)
|
34
|
+
end
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
+
def warn(value)
|
37
|
+
@logger.warn(value)
|
38
|
+
end
|
36
39
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
+
def error(value)
|
41
|
+
@logger.error(value)
|
42
|
+
end
|
40
43
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
+
def fatal(value)
|
45
|
+
@logger.fatal(value)
|
46
|
+
end
|
44
47
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
+
#----------------------------------------------------------------------------------------------------
|
49
|
+
# Helper Log output Methods
|
50
|
+
#----------------------------------------------------------------------------------------------------
|
48
51
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
62
|
-
|
64
|
+
LogHelper.progress_position
|
65
|
+
end
|
63
66
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
+
# prints out a line to the log
|
68
|
+
def line(size = 70, character: '=')
|
69
|
+
message = LogHelper.line(size, character)
|
67
70
|
|
68
|
-
|
69
|
-
|
71
|
+
@logger.info(message)
|
72
|
+
end
|
70
73
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
74
|
+
def heading(heading, size = 70)
|
75
|
+
lines = LogHelper.heading(heading, size)
|
76
|
+
info_multi_lines(lines)
|
77
|
+
end
|
75
78
|
|
76
|
-
|
77
|
-
|
79
|
+
def subheading(heading, size = 70)
|
80
|
+
lines = LogHelper.subheading(heading, size)
|
78
81
|
|
79
|
-
|
80
|
-
|
82
|
+
info_multi_lines(lines)
|
83
|
+
end
|
81
84
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
90
|
-
|
92
|
+
info(heading)
|
93
|
+
end
|
91
94
|
|
92
|
-
|
93
|
-
|
95
|
+
def block(messages, include_line: true, title: nil)
|
96
|
+
lines = LogHelper.block(messages, include_line: include_line, title: title)
|
94
97
|
|
95
|
-
|
96
|
-
|
98
|
+
info_multi_lines(lines)
|
99
|
+
end
|
97
100
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
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
|
-
|
106
|
-
|
108
|
+
# info(value)
|
109
|
+
# end
|
107
110
|
|
108
|
-
|
109
|
-
|
111
|
+
def yaml(data, is_line: true)
|
112
|
+
require 'yaml'
|
113
|
+
line if is_line
|
110
114
|
|
111
|
-
|
115
|
+
@logger.info(data.to_yaml) if data.is_a?(Hash)
|
112
116
|
|
113
|
-
|
117
|
+
@logger.info(data.marshal_dump.to_yaml) if data.is_a?(OpenStruct)
|
114
118
|
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
-
|
122
|
-
|
125
|
+
line if is_line
|
126
|
+
end
|
123
127
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
data
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
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
|
-
|
144
|
+
L.kv "#{indent}#{key}", ''
|
145
|
+
indent = "#{indent} "
|
146
|
+
open_struct(value, indent, **opts)
|
147
|
+
indent = indent.chomp(' ')
|
148
|
+
end
|
149
|
+
when Array
|
150
|
+
next if opts[:skip_array].present?
|
151
|
+
|
152
|
+
# puts LogHelper.subheading(key, 88)# if opts[:subheading].present?
|
153
|
+
puts LogHelper.subheading(opts[:subheading], 88) if opts[:subheading].present?
|
154
|
+
if value.length.positive?
|
155
|
+
if value.first.is_a?(String)
|
156
|
+
L.kv "#{indent}#{key}", value.join(', ')
|
157
|
+
else
|
158
|
+
tp value, value.first.to_h.keys
|
159
|
+
end
|
152
160
|
end
|
161
|
+
else
|
162
|
+
L.kv "#{indent}#{key}", value
|
153
163
|
end
|
154
|
-
else
|
155
|
-
L.kv "#{indent}#{key}", value
|
156
164
|
end
|
165
|
+
nil
|
157
166
|
end
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
alias o open_struct
|
167
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
|
168
|
+
alias ostruct open_struct
|
169
|
+
alias o open_struct
|
162
170
|
|
163
|
-
|
164
|
-
|
171
|
+
def exception(exception)
|
172
|
+
line
|
165
173
|
|
166
|
-
|
167
|
-
|
174
|
+
@logger.info(exception.message)
|
175
|
+
@logger.info(exception.backtrace.join("\n"))
|
168
176
|
|
169
|
-
|
170
|
-
|
177
|
+
line
|
178
|
+
end
|
171
179
|
|
172
|
-
|
173
|
-
|
174
|
-
|
180
|
+
#----------------------------------------------------------------------------------------------------
|
181
|
+
# Pretty Loggers
|
182
|
+
#----------------------------------------------------------------------------------------------------
|
175
183
|
|
176
|
-
|
177
|
-
|
178
|
-
|
184
|
+
# NOTE: using pretty_inspect is an existing namespace conflict
|
185
|
+
# rubocop:disable Metrics/AbcSize
|
186
|
+
def pretty_class(instance)
|
187
|
+
c = instance.class
|
179
188
|
|
180
|
-
|
189
|
+
line
|
181
190
|
|
182
|
-
|
183
|
-
|
184
|
-
|
191
|
+
kv('Full Class', c.name)
|
192
|
+
kv('Module', c.name.deconstantize)
|
193
|
+
kv('Class', c.name.demodulize)
|
185
194
|
|
186
|
-
|
187
|
-
|
188
|
-
|
195
|
+
source_location = c.instance_methods(false).map do |m|
|
196
|
+
c.instance_method(m).source_location.first
|
197
|
+
end.uniq
|
189
198
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
199
|
+
begin
|
200
|
+
kv('Source Location', source_location)
|
201
|
+
rescue StandardError => e
|
202
|
+
warn e
|
203
|
+
end
|
204
|
+
|
205
|
+
line
|
194
206
|
end
|
207
|
+
# rubocop:enable Metrics/AbcSize
|
195
208
|
|
196
|
-
|
197
|
-
|
209
|
+
# NOTE: using pretty_inspect is an existing namespace conflict
|
210
|
+
def pretty_params(params)
|
211
|
+
line
|
198
212
|
|
199
|
-
|
200
|
-
|
201
|
-
line
|
213
|
+
params.each do |k, v|
|
214
|
+
if params[k].is_a?(Hash)
|
202
215
|
|
203
|
-
|
204
|
-
|
216
|
+
params[k].each do |child_k, child_v|
|
217
|
+
kv("#{k}[#{child_k}]", child_v)
|
218
|
+
end
|
205
219
|
|
206
|
-
|
207
|
-
kv(
|
220
|
+
else
|
221
|
+
kv(k, v)
|
208
222
|
end
|
209
|
-
|
210
|
-
else
|
211
|
-
kv(k, v)
|
212
223
|
end
|
213
|
-
end
|
214
224
|
|
215
|
-
|
216
|
-
|
225
|
+
line
|
226
|
+
end
|
217
227
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
228
|
+
def help_all_symbols
|
229
|
+
# Produces a lot of data, need some sort of filter I think before this is useful
|
230
|
+
Symbol.all_symbols.each do |s|
|
231
|
+
info s
|
232
|
+
# debug s
|
233
|
+
end
|
223
234
|
end
|
224
|
-
end
|
225
235
|
|
226
|
-
|
227
|
-
|
228
|
-
|
236
|
+
#----------------------------------------------------------------------------------------------------
|
237
|
+
# Internal Methods
|
238
|
+
#----------------------------------------------------------------------------------------------------
|
229
239
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
240
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
241
|
+
def self.examples
|
242
|
+
L.debug 'some debug message'
|
243
|
+
L.info 'some info message'
|
244
|
+
L.warn 'some warning message'
|
245
|
+
L.error 'some error message'
|
246
|
+
L.fatal 'some fatal message'
|
236
247
|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
248
|
+
L.kv('First Name', 'David')
|
249
|
+
L.kv('Last Name', 'Cruwys')
|
250
|
+
L.kv('Age', 45)
|
251
|
+
L.kv('Sex', 'male')
|
241
252
|
|
242
|
-
|
243
|
-
|
244
|
-
L.progress
|
245
|
-
save_progress = L.progress
|
253
|
+
L.heading('Heading')
|
254
|
+
L.subheading('Sub Heading')
|
246
255
|
|
247
|
-
|
248
|
-
L.progress
|
249
|
-
L.progress
|
250
|
-
L.progress
|
256
|
+
L.block ['Line 1', 12, 'Line 3', true, 'Line 5']
|
251
257
|
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
258
|
+
L.progress(0, 'Section 1')
|
259
|
+
L.progress
|
260
|
+
L.progress
|
261
|
+
save_progress = L.progress
|
256
262
|
|
257
|
-
|
258
|
-
|
259
|
-
|
263
|
+
L.progress(10, 'Section 2')
|
264
|
+
L.progress
|
265
|
+
L.progress
|
266
|
+
L.progress
|
260
267
|
|
261
|
-
|
262
|
-
|
268
|
+
L.progress(save_progress, 'Section 1')
|
269
|
+
L.progress
|
270
|
+
L.progress
|
271
|
+
L.progress
|
263
272
|
|
264
|
-
|
273
|
+
L.line
|
274
|
+
L.line(20)
|
275
|
+
L.line(20, character: '-')
|
265
276
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
277
|
+
yaml1 = {}
|
278
|
+
yaml1['title'] = 'Software Architect'
|
279
|
+
yaml1['age'] = 45
|
280
|
+
yaml1['name'] = 'David'
|
270
281
|
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
282
|
+
yaml3 = {}
|
283
|
+
yaml3['title'] = 'Developer'
|
284
|
+
yaml3['age'] = 20
|
285
|
+
yaml3['name'] = 'Jin'
|
275
286
|
|
276
|
-
|
287
|
+
L.yaml(yaml1)
|
277
288
|
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
289
|
+
yaml2 = OpenStruct.new
|
290
|
+
yaml2.title = 'Software Architect'
|
291
|
+
yaml2.age = 45
|
292
|
+
yaml2.name = 'David'
|
282
293
|
|
283
|
-
|
294
|
+
L.yaml(yaml2)
|
284
295
|
|
285
|
-
|
296
|
+
mixed_yaml_array = [yaml1, yaml2]
|
286
297
|
|
287
|
-
|
288
|
-
|
298
|
+
# This fails because we don't correctly pre-process the array
|
299
|
+
L.yaml(mixed_yaml_array)
|
289
300
|
|
290
|
-
|
301
|
+
hash_yaml_array = [yaml1, yaml3]
|
291
302
|
|
292
|
-
|
303
|
+
L.yaml(hash_yaml_array)
|
293
304
|
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
305
|
+
begin
|
306
|
+
raise 'Here is an error'
|
307
|
+
rescue StandardError => e
|
308
|
+
L.exception(e)
|
309
|
+
end
|
298
310
|
end
|
299
|
-
|
311
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
300
312
|
|
301
|
-
|
313
|
+
private
|
302
314
|
|
303
|
-
|
304
|
-
|
305
|
-
|
315
|
+
def debug_multi_lines(lines)
|
316
|
+
lines.each do |line|
|
317
|
+
debug(line)
|
318
|
+
end
|
306
319
|
end
|
307
|
-
end
|
308
320
|
|
309
|
-
|
310
|
-
|
311
|
-
|
321
|
+
def info_multi_lines(lines)
|
322
|
+
lines.each do |line|
|
323
|
+
info(line)
|
324
|
+
end
|
312
325
|
end
|
313
|
-
end
|
314
326
|
|
315
|
-
|
316
|
-
|
317
|
-
|
327
|
+
def warn_multi_lines(lines)
|
328
|
+
lines.each do |line|
|
329
|
+
warn(line)
|
330
|
+
end
|
318
331
|
end
|
319
|
-
end
|
320
332
|
|
321
|
-
|
322
|
-
|
323
|
-
|
333
|
+
def error_multi_lines(lines)
|
334
|
+
lines.each do |line|
|
335
|
+
error(line)
|
336
|
+
end
|
324
337
|
end
|
325
|
-
end
|
326
338
|
|
327
|
-
|
328
|
-
|
329
|
-
|
339
|
+
def fatal_multi_lines(lines)
|
340
|
+
lines.each do |line|
|
341
|
+
fatal(line)
|
342
|
+
end
|
330
343
|
end
|
331
344
|
end
|
332
345
|
end
|
data/lib/k_log/version.rb
CHANGED
data/usage.png
ADDED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: k_log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
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-
|
11
|
+
date: 2021-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: " KLog provides console logging helpers and formatters\n"
|
14
14
|
email:
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- lib/k_log/log_helper.rb
|
43
43
|
- lib/k_log/log_util.rb
|
44
44
|
- lib/k_log/version.rb
|
45
|
+
- usage.png
|
45
46
|
homepage: http://appydave.com/gems/k-log
|
46
47
|
licenses:
|
47
48
|
- MIT
|