k_log 0.0.3 → 0.0.9
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 +12 -17
- 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 +85 -91
- data/lib/k_log/log_util.rb +263 -241
- 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: 3ed051548c417927d8f7250f275081ef4782bf285ee88fdc6e99709e6173733d
|
|
4
|
+
data.tar.gz: 263a07335a08a14068b18161328255e0487cb23497875400140ba818ba6c61da
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ff1c14cec827d4e46afe82b1ab37a526ed089c5ed58631b5be3455cd12feeaf4829b55b64dd85afe7511f387a4c3281b139ce9d3cbddcc5867dde483cabdfc97
|
|
7
|
+
data.tar.gz: 9c92c027d93527b7c62c935f6017eb6e7db9893fa85a579b13032fbc36e70f39a49090685b77f3a483e2247245b00e9021fcdf5d4496f9779cef4dbeed6f7ab3
|
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,24 +86,13 @@ 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).
|
|
95
92
|
|
|
96
93
|
```bash
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
# or push the latest gem
|
|
100
|
-
ls *.gem | sort -r | head -1 | xargs gem push
|
|
94
|
+
rake publish
|
|
95
|
+
rake clean
|
|
101
96
|
```
|
|
102
97
|
|
|
103
98
|
## Contributing
|
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,113 @@
|
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
def self.line(size = 70, character = '=')
|
|
51
|
-
green(character * size)
|
|
52
|
-
end
|
|
39
|
+
result
|
|
40
|
+
end
|
|
53
41
|
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
]
|
|
71
|
-
end
|
|
49
|
+
[
|
|
50
|
+
line,
|
|
51
|
+
heading,
|
|
52
|
+
line
|
|
53
|
+
]
|
|
54
|
+
end
|
|
72
55
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
56
|
+
def self.subheading(heading, size = 70)
|
|
57
|
+
line = line(size, '-')
|
|
58
|
+
[
|
|
59
|
+
line,
|
|
60
|
+
heading,
|
|
61
|
+
line
|
|
62
|
+
]
|
|
63
|
+
end
|
|
80
64
|
|
|
81
|
-
#
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
91
|
-
|
|
83
|
+
# info(value)
|
|
84
|
+
# end
|
|
92
85
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
86
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
|
87
|
+
def self.block(messages, include_line: true, title: nil)
|
|
88
|
+
result = include_line ? [line] : []
|
|
96
89
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
90
|
+
unless title.nil?
|
|
91
|
+
result.push(title)
|
|
92
|
+
result.push(line(70, ','))
|
|
93
|
+
end
|
|
101
94
|
|
|
102
|
-
|
|
95
|
+
result.push messages if messages.is_a?(String) || messages.is_a?(Integer)
|
|
103
96
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
103
|
+
result.push line if include_line
|
|
111
104
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
105
|
+
result
|
|
106
|
+
end
|
|
107
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
|
115
108
|
|
|
116
|
-
|
|
117
|
-
|
|
109
|
+
def self.green(value)
|
|
110
|
+
"\033[32m#{value}\033[0m"
|
|
111
|
+
end
|
|
118
112
|
end
|
|
119
113
|
end
|
data/lib/k_log/log_util.rb
CHANGED
|
@@ -1,332 +1,354 @@
|
|
|
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(' ')
|
|
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
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
164
|
-
|
|
172
|
+
def exception(exception)
|
|
173
|
+
line
|
|
165
174
|
|
|
166
|
-
|
|
167
|
-
|
|
175
|
+
@logger.info(exception.message)
|
|
176
|
+
@logger.info(exception.backtrace.join("\n"))
|
|
168
177
|
|
|
169
|
-
|
|
170
|
-
|
|
178
|
+
line
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
#----------------------------------------------------------------------------------------------------
|
|
182
|
+
# Pretty Loggers
|
|
183
|
+
#----------------------------------------------------------------------------------------------------
|
|
171
184
|
|
|
172
|
-
|
|
173
|
-
|
|
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
|
-
|
|
177
|
-
def pretty_class(instance)
|
|
178
|
-
c = instance.class
|
|
190
|
+
line
|
|
179
191
|
|
|
180
|
-
|
|
192
|
+
kv('Full Class', c.name)
|
|
193
|
+
kv('Module', c.name.deconstantize)
|
|
194
|
+
kv('Class', c.name.demodulize)
|
|
181
195
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
196
|
+
source_location = c.instance_methods(false).map do |m|
|
|
197
|
+
c.instance_method(m).source_location.first
|
|
198
|
+
end.uniq
|
|
185
199
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
200
|
+
begin
|
|
201
|
+
kv('Source Location', source_location)
|
|
202
|
+
rescue StandardError => e
|
|
203
|
+
warn e
|
|
204
|
+
end
|
|
189
205
|
|
|
190
|
-
|
|
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
|
-
|
|
197
|
-
|
|
210
|
+
# NOTE: using pretty_inspect is an existing namespace conflict
|
|
211
|
+
def pretty_params(params)
|
|
212
|
+
line
|
|
198
213
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
line
|
|
214
|
+
params.each do |k, v|
|
|
215
|
+
if params[k].is_a?(Hash)
|
|
202
216
|
|
|
203
|
-
|
|
204
|
-
|
|
217
|
+
params[k].each do |child_k, child_v|
|
|
218
|
+
kv("#{k}[#{child_k}]", child_v)
|
|
219
|
+
end
|
|
205
220
|
|
|
206
|
-
|
|
207
|
-
kv(
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
242
|
+
def self.examples
|
|
243
|
+
examples_simple
|
|
244
|
+
# examples_complex
|
|
245
|
+
end
|
|
241
246
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
-
|
|
248
|
-
|
|
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
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
267
|
+
L.progress(0, 'Section 1')
|
|
268
|
+
L.progress
|
|
269
|
+
L.progress
|
|
270
|
+
save_progress = L.progress
|
|
256
271
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
272
|
+
L.progress(10, 'Section 2')
|
|
273
|
+
L.progress
|
|
274
|
+
L.progress
|
|
275
|
+
L.progress
|
|
260
276
|
|
|
261
|
-
|
|
262
|
-
|
|
277
|
+
L.progress(save_progress, 'Section 1')
|
|
278
|
+
L.progress
|
|
279
|
+
L.progress
|
|
280
|
+
L.progress
|
|
263
281
|
|
|
264
|
-
|
|
282
|
+
L.line
|
|
283
|
+
L.line(20)
|
|
284
|
+
L.line(20, character: '-')
|
|
265
285
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
286
|
+
yaml1 = {}
|
|
287
|
+
yaml1['title'] = 'Software Architect'
|
|
288
|
+
yaml1['age'] = 45
|
|
289
|
+
yaml1['name'] = 'David'
|
|
270
290
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
291
|
+
yaml3 = {}
|
|
292
|
+
yaml3['title'] = 'Developer'
|
|
293
|
+
yaml3['age'] = 20
|
|
294
|
+
yaml3['name'] = 'Jin'
|
|
275
295
|
|
|
276
|
-
|
|
296
|
+
L.yaml(yaml1)
|
|
277
297
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
298
|
+
yaml2 = OpenStruct.new
|
|
299
|
+
yaml2.title = 'Software Architect'
|
|
300
|
+
yaml2.age = 45
|
|
301
|
+
yaml2.name = 'David'
|
|
282
302
|
|
|
283
|
-
|
|
303
|
+
L.yaml(yaml2)
|
|
284
304
|
|
|
285
|
-
|
|
305
|
+
mixed_yaml_array = [yaml1, yaml2]
|
|
286
306
|
|
|
287
|
-
|
|
288
|
-
|
|
307
|
+
# This fails because we don't correctly pre-process the array
|
|
308
|
+
L.yaml(mixed_yaml_array)
|
|
289
309
|
|
|
290
|
-
|
|
310
|
+
hash_yaml_array = [yaml1, yaml3]
|
|
291
311
|
|
|
292
|
-
|
|
312
|
+
L.yaml(hash_yaml_array)
|
|
293
313
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
314
|
+
begin
|
|
315
|
+
raise 'Here is an error'
|
|
316
|
+
rescue StandardError => e
|
|
317
|
+
L.exception(e)
|
|
318
|
+
end
|
|
298
319
|
end
|
|
299
|
-
|
|
320
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
|
300
321
|
|
|
301
|
-
|
|
322
|
+
private
|
|
302
323
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
324
|
+
def debug_multi_lines(lines)
|
|
325
|
+
lines.each do |line|
|
|
326
|
+
debug(line)
|
|
327
|
+
end
|
|
306
328
|
end
|
|
307
|
-
end
|
|
308
329
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
330
|
+
def info_multi_lines(lines)
|
|
331
|
+
lines.each do |line|
|
|
332
|
+
info(line)
|
|
333
|
+
end
|
|
312
334
|
end
|
|
313
|
-
end
|
|
314
335
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
336
|
+
def warn_multi_lines(lines)
|
|
337
|
+
lines.each do |line|
|
|
338
|
+
warn(line)
|
|
339
|
+
end
|
|
318
340
|
end
|
|
319
|
-
end
|
|
320
341
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
342
|
+
def error_multi_lines(lines)
|
|
343
|
+
lines.each do |line|
|
|
344
|
+
error(line)
|
|
345
|
+
end
|
|
324
346
|
end
|
|
325
|
-
end
|
|
326
347
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
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
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.9
|
|
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-
|
|
11
|
+
date: 2021-04-01 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
|