k_log 0.0.1 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -16
- data/STORIES.md +11 -13
- data/USAGE.md +8 -6
- 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 -243
- 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: ff185cf6c22b2a587bd95539a23a6001f3c05e58f05f4bfdea234d6fee47da9a
|
4
|
+
data.tar.gz: 4d80fecd333fd27c05a33bd513cf1d582b6eff788ee708fcb739fe0bffa96d37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 063bcc89ed9fb8ecac60e04675745273dccaefa107adffd22f1a7b71b9d6e08b84204ae364bc47bde0a0191c80036e7725e1bc0fec6c4d30cbf2cfa5386e7335
|
7
|
+
data.tar.gz: f110db54bbdb1e69260aa97e669883e5f7d43e1e94d72dde9753b8ed922b1bf9f5fda81c3384e7e9039ac9abec0afd6d4aa63ff7d7af28ca501a3a3f223ee202
|
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:
|
@@ -36,14 +38,44 @@ See all [usage examples](./USAGE.md)
|
|
36
38
|
|
37
39
|
### Basic Example
|
38
40
|
|
39
|
-
####
|
41
|
+
#### Setup KLog
|
42
|
+
|
43
|
+
Pass a standard Logger to KLog and then setup an alias for easier access
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
KLog.logger = Logger.new($stdout)
|
47
|
+
KLog.logger.level = Logger::DEBUG
|
48
|
+
KLog.logger.formatter = KLog::LogFormatter.new
|
49
|
+
|
50
|
+
L = KLog::LogUtil.new(KLog.logger)
|
51
|
+
```
|
40
52
|
|
41
|
-
|
53
|
+
#### Sample Usage
|
42
54
|
|
43
55
|
```ruby
|
44
|
-
|
56
|
+
L.debug 'some debug message'
|
57
|
+
L.info 'some info message'
|
58
|
+
L.warn 'some warning message'
|
59
|
+
L.error 'some error message'
|
60
|
+
L.fatal 'some fatal message'
|
61
|
+
|
62
|
+
L.kv('First Name', 'David')
|
63
|
+
L.kv('Last Name', 'Cruwys')
|
64
|
+
L.kv('Age', 45)
|
65
|
+
L.kv('Sex', 'male')
|
66
|
+
|
67
|
+
L.heading('Heading')
|
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
|
45
75
|
```
|
46
76
|
|
77
|
+
![Usage](usage.png)
|
78
|
+
|
47
79
|
## Development
|
48
80
|
|
49
81
|
Checkout the repo
|
@@ -54,24 +86,13 @@ git clone klueless-io/k_log
|
|
54
86
|
|
55
87
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
56
88
|
|
57
|
-
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
-
|
59
|
-
```bash
|
60
|
-
bin/console
|
61
|
-
|
62
|
-
Aaa::Bbb::Program.execute()
|
63
|
-
# => ""
|
64
|
-
```
|
65
|
-
|
66
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.
|
67
90
|
|
68
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).
|
69
92
|
|
70
93
|
```bash
|
71
|
-
|
72
|
-
|
73
|
-
# or push the latest gem
|
74
|
-
ls *.gem | sort -r | head -1 | xargs gem push
|
94
|
+
rake publish
|
95
|
+
rake clean
|
75
96
|
```
|
76
97
|
|
77
98
|
## Contributing
|
data/STORIES.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# K Log
|
2
2
|
|
3
|
-
>
|
3
|
+
> K Log provides console logging helpers and formatters
|
4
4
|
|
5
5
|
As a Developer, I need formatted console logs, so that information presents clearly
|
6
6
|
|
@@ -8,26 +8,19 @@ As a Developer, I need formatted console logs, so that information presents clea
|
|
8
8
|
|
9
9
|
### Stories next on list
|
10
10
|
|
11
|
-
As a Developer, I
|
11
|
+
As a Developer, I need formatted console logs, so that information presents clearly
|
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/USAGE.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# K Log
|
2
2
|
|
3
|
-
>
|
3
|
+
> K Log provides console logging helpers and formatters
|
4
4
|
|
5
5
|
As a Developer, I need formatted console logs, so that information presents clearly
|
6
6
|
|
@@ -8,12 +8,14 @@ As a Developer, I need formatted console logs, so that information presents clea
|
|
8
8
|
|
9
9
|
### Sample Classes
|
10
10
|
|
11
|
-
####
|
11
|
+
#### Setup KLog
|
12
12
|
|
13
|
-
|
13
|
+
Pass a standard Logger to KLog and then setup an alias for easier access
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
KLog.logger = Logger.new($stdout)
|
17
|
+
KLog.logger.level = Logger::DEBUG
|
18
|
+
KLog.logger.formatter = KLog::LogFormatter.new
|
19
|
+
|
20
|
+
L = KLog::LogUtil.new(KLog.logger)
|
19
21
|
```
|
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
|
|
@@ -22,8 +22,8 @@ files_changed = `git diff-index --name-only HEAD --`.split
|
|
22
22
|
files_changed -= ['Gemfile']
|
23
23
|
files_changed -= ['Gemfile.lock']
|
24
24
|
files_changed -= ['.gitignore']
|
25
|
-
files_changed -= ['README.md']
|
26
25
|
|
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,352 @@
|
|
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 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
|
152
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
225
|
|
215
|
-
|
216
|
-
end
|
217
|
-
|
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
|
-
|
226
|
-
#----------------------------------------------------------------------------------------------------
|
227
|
-
# Internal Methods
|
228
|
-
#----------------------------------------------------------------------------------------------------
|
229
228
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
L.kv('First Name', 'David')
|
238
|
-
L.kv('Last Name', 'Cruwys')
|
239
|
-
L.kv('Age', 45)
|
240
|
-
L.kv('Sex', 'male')
|
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
|
241
236
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
save_progress = L.progress
|
237
|
+
#----------------------------------------------------------------------------------------------------
|
238
|
+
# Internal Methods
|
239
|
+
#----------------------------------------------------------------------------------------------------
|
246
240
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
241
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
242
|
+
def self.examples
|
243
|
+
examples_simple
|
244
|
+
# examples_complex
|
245
|
+
end
|
246
|
+
def self.examples_simple
|
247
|
+
L.debug 'some debug message'
|
248
|
+
L.info 'some info message'
|
249
|
+
L.warn 'some warning message'
|
250
|
+
L.error 'some error message'
|
251
|
+
L.fatal 'some fatal message'
|
252
|
+
|
253
|
+
L.kv('First Name', 'David')
|
254
|
+
L.kv('Last Name', 'Cruwys')
|
255
|
+
L.kv('Age', 45)
|
256
|
+
L.kv('Sex', 'male')
|
257
|
+
|
258
|
+
L.heading('Heading')
|
259
|
+
L.subheading('Sub Heading')
|
260
|
+
L.section_heading('Section Heading')
|
261
|
+
end
|
262
|
+
def self.examples_complex
|
263
|
+
L.block ['Line 1', 12, 'Line 3', true, 'Line 5']
|
251
264
|
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
265
|
+
L.progress(0, 'Section 1')
|
266
|
+
L.progress
|
267
|
+
L.progress
|
268
|
+
save_progress = L.progress
|
256
269
|
|
257
|
-
|
258
|
-
|
259
|
-
|
270
|
+
L.progress(10, 'Section 2')
|
271
|
+
L.progress
|
272
|
+
L.progress
|
273
|
+
L.progress
|
260
274
|
|
261
|
-
|
262
|
-
|
275
|
+
L.progress(save_progress, 'Section 1')
|
276
|
+
L.progress
|
277
|
+
L.progress
|
278
|
+
L.progress
|
263
279
|
|
264
|
-
|
280
|
+
L.line
|
281
|
+
L.line(20)
|
282
|
+
L.line(20, character: '-')
|
265
283
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
284
|
+
yaml1 = {}
|
285
|
+
yaml1['title'] = 'Software Architect'
|
286
|
+
yaml1['age'] = 45
|
287
|
+
yaml1['name'] = 'David'
|
270
288
|
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
289
|
+
yaml3 = {}
|
290
|
+
yaml3['title'] = 'Developer'
|
291
|
+
yaml3['age'] = 20
|
292
|
+
yaml3['name'] = 'Jin'
|
275
293
|
|
276
|
-
|
294
|
+
L.yaml(yaml1)
|
277
295
|
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
296
|
+
yaml2 = OpenStruct.new
|
297
|
+
yaml2.title = 'Software Architect'
|
298
|
+
yaml2.age = 45
|
299
|
+
yaml2.name = 'David'
|
282
300
|
|
283
|
-
|
301
|
+
L.yaml(yaml2)
|
284
302
|
|
285
|
-
|
303
|
+
mixed_yaml_array = [yaml1, yaml2]
|
286
304
|
|
287
|
-
|
288
|
-
|
305
|
+
# This fails because we don't correctly pre-process the array
|
306
|
+
L.yaml(mixed_yaml_array)
|
289
307
|
|
290
|
-
|
308
|
+
hash_yaml_array = [yaml1, yaml3]
|
291
309
|
|
292
|
-
|
310
|
+
L.yaml(hash_yaml_array)
|
293
311
|
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
312
|
+
begin
|
313
|
+
raise 'Here is an error'
|
314
|
+
rescue StandardError => e
|
315
|
+
L.exception(e)
|
316
|
+
end
|
298
317
|
end
|
299
|
-
|
318
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
300
319
|
|
301
|
-
|
320
|
+
private
|
302
321
|
|
303
|
-
|
304
|
-
|
305
|
-
|
322
|
+
def debug_multi_lines(lines)
|
323
|
+
lines.each do |line|
|
324
|
+
debug(line)
|
325
|
+
end
|
306
326
|
end
|
307
|
-
end
|
308
327
|
|
309
|
-
|
310
|
-
|
311
|
-
|
328
|
+
def info_multi_lines(lines)
|
329
|
+
lines.each do |line|
|
330
|
+
info(line)
|
331
|
+
end
|
312
332
|
end
|
313
|
-
end
|
314
333
|
|
315
|
-
|
316
|
-
|
317
|
-
|
334
|
+
def warn_multi_lines(lines)
|
335
|
+
lines.each do |line|
|
336
|
+
warn(line)
|
337
|
+
end
|
318
338
|
end
|
319
|
-
end
|
320
339
|
|
321
|
-
|
322
|
-
|
323
|
-
|
340
|
+
def error_multi_lines(lines)
|
341
|
+
lines.each do |line|
|
342
|
+
error(line)
|
343
|
+
end
|
324
344
|
end
|
325
|
-
end
|
326
345
|
|
327
|
-
|
328
|
-
|
329
|
-
|
346
|
+
def fatal_multi_lines(lines)
|
347
|
+
lines.each do |line|
|
348
|
+
fatal(line)
|
349
|
+
end
|
330
350
|
end
|
331
351
|
end
|
332
352
|
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.8
|
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
|