ruby_smart-simple_logger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +38 -0
  3. data/.gitignore +25 -0
  4. data/.rspec +3 -0
  5. data/.yardopts +5 -0
  6. data/Gemfile +7 -0
  7. data/README.md +739 -0
  8. data/Rakefile +8 -0
  9. data/bin/console +8 -0
  10. data/bin/setup +8 -0
  11. data/docs/CHANGELOG.md +17 -0
  12. data/docs/CODE_OF_CONDUCT.md +84 -0
  13. data/docs/LICENSE.txt +21 -0
  14. data/lib/debugger.rb +20 -0
  15. data/lib/ruby_smart/simple_logger/core_ext/ruby/string.rb +43 -0
  16. data/lib/ruby_smart/simple_logger/devices/memory_device.rb +43 -0
  17. data/lib/ruby_smart/simple_logger/devices/multi_device.rb +69 -0
  18. data/lib/ruby_smart/simple_logger/devices/proc_device.rb +37 -0
  19. data/lib/ruby_smart/simple_logger/extensions/helper.rb +259 -0
  20. data/lib/ruby_smart/simple_logger/extensions/logs.rb +26 -0
  21. data/lib/ruby_smart/simple_logger/extensions/mask.rb +53 -0
  22. data/lib/ruby_smart/simple_logger/extensions/scene.rb +77 -0
  23. data/lib/ruby_smart/simple_logger/extensions/severity.rb +62 -0
  24. data/lib/ruby_smart/simple_logger/extensions/simple_log.rb +224 -0
  25. data/lib/ruby_smart/simple_logger/extensions/timer.rb +63 -0
  26. data/lib/ruby_smart/simple_logger/formatter.rb +153 -0
  27. data/lib/ruby_smart/simple_logger/gem_version.rb +23 -0
  28. data/lib/ruby_smart/simple_logger/klass_logger.rb +45 -0
  29. data/lib/ruby_smart/simple_logger/logger.rb +74 -0
  30. data/lib/ruby_smart/simple_logger/scenes.rb +288 -0
  31. data/lib/ruby_smart/simple_logger/version.rb +12 -0
  32. data/lib/ruby_smart/simple_logger.rb +25 -0
  33. data/lib/ruby_smart-simple_logger.rb +3 -0
  34. data/lib/simple_logger.rb +7 -0
  35. data/ruby_smart-simple_logger.gemspec +43 -0
  36. metadata +167 -0
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ruby_smart/simple_logger"
6
+ require "irb"
7
+
8
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/docs/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # RubySmart::SimpleLogger - CHANGELOG
2
+
3
+ ## [1.0.0] - 2023-01-29
4
+ * **[add]** full documentation
5
+ * **[add]** add colors, if gem `awesome_print` ist missing
6
+ * **[add]** klass_logger_opts
7
+ * **[add]** builtin / opts support for Logger.new
8
+ * **[add]** MemoryDevice, MultiDevice, ProcDevice
9
+ * **[ref]** docs & code comments
10
+ * **[ref]** cleanup & remove unused code
11
+ * **[ref]** Logger#simple_log - to use customized inspector method
12
+ * **[ref]** update gem dependencies
13
+ * **[fix]** minor bugs & log forwarding
14
+
15
+ ## [0.1.0] - 2023-01-24
16
+ * Initial commit
17
+ * docs, version, structure
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at tg@reimbursement.institute. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/docs/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ruby Smart
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/lib/debugger.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "gem_info"
4
+ require 'ruby_smart/simple_logger'
5
+
6
+ # to prevent compatibility issues with the ruby-debug-ide gem (uses same Namespace 'Debugger')
7
+ GemInfo.safe_require 'ruby-debug-ide'
8
+
9
+ # try to load 'awesome_print', if available
10
+ GemInfo.safe_require 'awesome_print'
11
+
12
+ class Debugger
13
+ extend ::RubySmart::SimpleLogger::KlassLogger
14
+
15
+ # overwrite existing "debug" method (if Debase was loaded)
16
+ def self.debug(*args)
17
+ return false if args.none?
18
+ klass_logger.debug(*args)
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ unless String.method_defined? "bg_black"
4
+ class String
5
+ # add string background colors
6
+ #
7
+ # ANSI color codes:
8
+ # \e => escape
9
+ # 40 => color bg base
10
+ #
11
+ # For HTML coloring we use <kbd> tag instead of <span> to require monospace font.
12
+ %w(black red green yellow blue purple cyan white).each_with_index do |color, i|
13
+ define_method "bg_#{color}" do |*html|
14
+ html[0] ? %Q|<kbd style="background:#{color}">#{self}</kbd>| : "\e[#{40 + i}m#{self}\e[0m"
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ # only as comment - if we decide to remove 'awesome_print'
21
+ unless String.method_defined? "black"
22
+ class String
23
+ # add string colors
24
+ #
25
+ # ANSI color codes:
26
+ # \e => escape
27
+ # 1;30 => color base
28
+ #
29
+ # For HTML coloring we use <kbd> tag instead of <span> to require monospace font.
30
+ %w(gray red green yellow blue purple cyan white).each_with_index do |color, i|
31
+ define_method color do |*html|
32
+ html[0] ? %Q|<kbd style="color:#{color}">#{self}</kbd>| : "\e[1;#{30 + i}m#{self}\e[0m"
33
+ end
34
+
35
+ define_method "#{color}ish" do |*html|
36
+ html[0] ? %Q|<kbd style="color:#{color}">#{self}</kbd>| : "\e[0;#{30 + i}m#{self}\e[0m"
37
+ end
38
+ end
39
+
40
+ alias :black :grayish
41
+ alias :pale :whiteish
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySmart
4
+ module SimpleLogger
5
+ module Devices
6
+ class MemoryDevice
7
+
8
+ attr_reader :logs
9
+ attr_reader :status
10
+
11
+ def initialize
12
+ @logs = []
13
+ @status = true
14
+ end
15
+
16
+ # adds data to the logs
17
+ # @param [Object] data
18
+ def write(data)
19
+ return false unless status
20
+
21
+ @logs << data
22
+ end
23
+
24
+ alias_method :<<, :write
25
+
26
+ # disables writing
27
+ def close
28
+ @status = false
29
+ end
30
+
31
+ # enables writing
32
+ def reopen
33
+ @status = true
34
+ end
35
+
36
+ # clears all logs
37
+ def clear!
38
+ @logs = []
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySmart
4
+ module SimpleLogger
5
+ module Devices
6
+ class MultiDevice
7
+
8
+ attr_reader :devices
9
+ attr_reader :status
10
+
11
+ def initialize
12
+ @devices = []
13
+ @status = true
14
+ end
15
+
16
+ # pass data to the devices
17
+ # @param [Object] data
18
+ def write(data)
19
+ return false unless status
20
+
21
+ devices.each do |device|
22
+ if device[:formatter]
23
+ device[:dev].write(device[:formatter].(*data))
24
+ else
25
+ device[:dev].write(data)
26
+ end
27
+ end
28
+ end
29
+
30
+ alias_method :<<, :write
31
+
32
+ # disables writing
33
+ def close
34
+ @status = false
35
+ end
36
+
37
+ # enables writing
38
+ def reopen
39
+ @status = true
40
+ end
41
+
42
+ # clears all devices
43
+ def clear!
44
+ @devices = []
45
+ end
46
+
47
+ # registers a new device.
48
+ # CHAINABLE
49
+ #
50
+ # @param [Object] dev
51
+ def register(dev, formatter = nil)
52
+ @devices << {
53
+ dev: dev,
54
+ formatter: formatter
55
+ }
56
+
57
+ self
58
+ end
59
+
60
+ # returns logs from the first loggable device
61
+ # @return [Array] logs
62
+ def logs
63
+ logdev = devices.detect { |device| device[:dev].respond_to?(:logs) }
64
+ logdev.nil? ? [] : logdev[:dev].logs
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySmart
4
+ module SimpleLogger
5
+ module Devices
6
+ class ProcDevice
7
+
8
+ attr_reader :status
9
+
10
+ def initialize(proc)
11
+ @proc = proc
12
+ @status = true
13
+ end
14
+
15
+ # pass data to the callback
16
+ # @param [Object] data
17
+ def write(data)
18
+ return false unless status
19
+
20
+ @proc.call(data)
21
+ end
22
+
23
+ alias_method :<<, :write
24
+
25
+ # disables writing
26
+ def close
27
+ @status = false
28
+ end
29
+
30
+ # enables writing
31
+ def reopen
32
+ @status = true
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,259 @@
1
+ # frozen_string_literal: false
2
+
3
+ # use from ruby_smart-support
4
+ require 'thread_info'
5
+ require 'fileutils'
6
+
7
+ module RubySmart
8
+ module SimpleLogger
9
+ module Extensions
10
+ module Helper
11
+
12
+ private
13
+
14
+ # prepare builtin defaults
15
+ # @param [Hash] opts
16
+ def _opts_builtin(opts)
17
+ builtin = opts.delete(:builtin)
18
+
19
+ case builtin
20
+ when nil
21
+ # device is nil - resolve optimal device
22
+ opts[:device] ||= if ::ThreadInfo.stdout?
23
+ # colorize output
24
+ opts[:clr] = true if opts[:clr].nil?
25
+
26
+ STDOUT
27
+ elsif ::ThreadInfo.rails? && ::Rails.logger
28
+ ::RubySmart::SimpleLogger::Devices::MultiDevice.new.register(::Rails.logger.instance_variable_get(:@logdev).dev)
29
+ else
30
+ ::RubySmart::SimpleLogger::Devices::MemoryDevice.new
31
+ end
32
+ when :stdout
33
+ opts[:device] ||= STDOUT
34
+ # colorize output
35
+ opts[:clr] = true if opts[:clr].nil?
36
+ when :stderr
37
+ opts[:device] ||= STDERR
38
+ # colorize output
39
+ opts[:clr] = true if opts[:clr].nil?
40
+ when :rails
41
+ opts[:device] ||= ::RubySmart::SimpleLogger::Devices::MultiDevice.new.register(::Rails.logger.instance_variable_get(:@logdev).dev)
42
+ when :proc
43
+ # auto sets related opts for proc device
44
+ opts[:payload] = false
45
+ opts[:format] ||= :passthrough
46
+
47
+ # force set device
48
+ opts[:device] ||= ::RubySmart::SimpleLogger::Devices::ProcDevice.new(opts[:proc])
49
+ when :memory
50
+ # auto sets related opts for memory device
51
+ opts[:payload] = false
52
+
53
+ # set device
54
+ opts[:device] ||= if opts[:stdout]
55
+ # IMPORTANT: There will be a 'default' formatter created, which will passthrough all data to the device
56
+ opts[:format] ||= :passthrough
57
+
58
+ # special case handling to additionally stdout logs
59
+ ::RubySmart::SimpleLogger::Devices::MultiDevice.new.
60
+ register(::RubySmart::SimpleLogger::Devices::MemoryDevice.new, ::RubySmart::SimpleLogger::Formatter.new(format: :memory, nl: false)).
61
+ register(STDOUT, ::RubySmart::SimpleLogger::Formatter.new(format: :default, nl: true, clr: (opts[:clr] != nil)))
62
+ else
63
+ opts[:format] ||= :memory
64
+ ::RubySmart::SimpleLogger::Devices::MemoryDevice.new
65
+ end
66
+ else
67
+ # forward provided device ONLY if unset in opts
68
+ opts[:device] ||= builtin
69
+ end
70
+ end
71
+
72
+ # resolve the final log-device from provided param
73
+ # @param [Object] device
74
+ # @return [Object]
75
+ def _logdev(device)
76
+ if device.is_a?(Module)
77
+ devstring = device.to_s
78
+ logfile = (devstring.respond_to?(:underscore) ? devstring.underscore : _underscore(device.to_s)) + '.log'
79
+ # check for rails
80
+ if ::ThreadInfo.rails?
81
+ file_location = File.join(Rails.root, 'log', logfile)
82
+ else
83
+ file_location = File.join('log', logfile)
84
+ end
85
+
86
+ # resolve path to create a folder
87
+ file_path = File.dirname(file_location)
88
+ FileUtils.mkdir_p(file_path) unless File.directory?(file_path)
89
+
90
+ file_location
91
+ elsif device.is_a?(String)
92
+ device = "log/#{device}" unless device[0] == '/'
93
+
94
+ # resolve path to create a folder
95
+ file_path = File.dirname(device)
96
+ FileUtils.mkdir_p(file_path) unless File.directory?(file_path)
97
+
98
+ device
99
+ elsif device.respond_to?(:write)
100
+ device
101
+ else
102
+ raise "SimpleLogger :: device '#{device}' must respond to 'write'!"
103
+ end
104
+ end
105
+
106
+ # this is a 1:1 copy of +String#underscore+ @ activesupport gem
107
+ def _underscore(camel_cased_word)
108
+ return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
109
+ word = camel_cased_word.to_s.gsub("::", "/")
110
+ word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)((?=a)b)(?=\b|[^a-z])/) { "#{$1 && '_' }#{$2.downcase}" }
111
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
112
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
113
+ word.tr!("-", "_")
114
+ word.downcase!
115
+ word
116
+ end
117
+
118
+ # build log-device & options on initialize
119
+ # @param [Hash] opts
120
+ # @return [Array<logdev,opts>] logdev, opts
121
+ def _init_opts(opts)
122
+ # clean & rewrite opts (especially device) to a real logdev
123
+ _opts_builtin(opts)
124
+
125
+ # set mask from options
126
+ self.mask(opts[:mask]) if opts[:mask]
127
+
128
+ # disable mask color if explicit disabled
129
+ self.mask(clr: false) if opts[:clr] == false
130
+
131
+ # reduce mask-size to window size
132
+ if ::ThreadInfo.windowed? && ::ThreadInfo.winsize[1] < self.mask[:length]
133
+ self.mask(length: ::ThreadInfo.winsize[1])
134
+ end
135
+
136
+ # initialize a default rails-dependent output
137
+ if ::ThreadInfo.rails?
138
+ opts[:level] ||= (::Rails.env.production? ? :info : :debug)
139
+ end
140
+
141
+ # clean & rewrite level (possible symbols) to real level
142
+ opts[:level] = _level(opts[:level] || :debug)
143
+
144
+ # provide custom formatter and forward special opts (format, nl, clr)
145
+ opts[:formatter] ||= ::RubySmart::SimpleLogger::Formatter.new({ format: :plain, nl: false, clr: false }.merge(opts.slice(:format, :nl, :clr)))
146
+
147
+ # ignore payload and send data directly to the logdev
148
+ @ignore_payload = true if opts[:payload].is_a?(FalseClass)
149
+
150
+ # set the inspector to be used for data inspection
151
+ @inspector = opts[:inspector]
152
+
153
+ # disable inspector
154
+ @inspector = :to_s if opts[:inspect] == false
155
+
156
+ # # build logdev
157
+ # opts[:logdev] = _logdev(opts[:device])
158
+
159
+ # simple return opts
160
+ opts
161
+ end
162
+
163
+ # merge all provided hashes into one single hash
164
+ #
165
+ # @example
166
+ # _opt({level: :debug, formatter: :my, payload: []}, {payload: [:_], ins: true})
167
+ # > {level: :debug, formatter: :my, payload: [:_], ins: true}
168
+ #
169
+ # @param [Hash] opts
170
+ # @return [Hash] opts
171
+ def _opt(*opts)
172
+ # IMPORTANT: Do not remove first empty hash!
173
+ opts.unshift {}
174
+
175
+ # merge each single provided option
176
+ opts.reduce({}) do |m, opt|
177
+ opt.each do |k, v|
178
+ if v.is_a?(Hash) && m[k].is_a?(Hash)
179
+ m[k] = m[k].merge(v)
180
+ else
181
+ m[k] = v
182
+ end
183
+ end
184
+ m
185
+ end
186
+ end
187
+
188
+ # colorizes a provided string
189
+ # returns the string if no color was provided or invalid
190
+ #
191
+ # @example
192
+ # _clr('Test',nil)
193
+ # > "Test"
194
+ #
195
+ # _clr('Test2', :red)
196
+ # > "\e[1;31mTest2\e[0m"
197
+ #
198
+ # _clr('Test3', :not_a_valid_color)
199
+ # > "Test3"
200
+ #
201
+ # @param [String] str
202
+ # @param [String, Symbol, nil] color
203
+ # @return [String] colored string
204
+ def _clr(str, color)
205
+ return str unless color && str.respond_to?(color)
206
+ str.send(color) rescue str
207
+ end
208
+
209
+ # force string to exact length
210
+ #
211
+ # @example
212
+ # _lgth('test', 10)
213
+ # > "test "
214
+ #
215
+ # _lgth('very long teststring', 10)
216
+ # > "very long "
217
+ #
218
+ # _lgth('very', 10,'-')
219
+ # > "very------"
220
+ #
221
+ # @param [String] str
222
+ # @param [Integer] length
223
+ # @param [String] padstr - optional (default: ' ')
224
+ # @return [String] str
225
+ def _lgth(str, length, padstr = ' ')
226
+ str.to_s.ljust(length, padstr)[0..(length - 1)]
227
+ end
228
+
229
+ # returns a Symbol by provided result or color
230
+ #
231
+ # @example
232
+ # _res_clr(true)
233
+ # > :green
234
+ #
235
+ # _res_clr('0')
236
+ # > :red
237
+ #
238
+ # _res_clr(:yellow)
239
+ # > :yellow
240
+ #
241
+ # _res_clr('not_really_a_color')
242
+ # > :not_really_a_color
243
+ #
244
+ # @param [Boolean, String, Integer, Symbol] res_or_clr
245
+ # @return [Symbol] color
246
+ def _res_clr(res_or_clr)
247
+ case res_or_clr
248
+ when true, 1, '1'
249
+ :green
250
+ when false, 0, '0'
251
+ :red
252
+ else
253
+ res_or_clr.to_sym
254
+ end
255
+ end
256
+ end
257
+ end
258
+ end
259
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySmart
4
+ module SimpleLogger
5
+ module Extensions
6
+ module Logs
7
+ # returns the logdev logs
8
+ # @return [Array]
9
+ def logs
10
+ return [] unless logdev.dev.respond_to?(:logs)
11
+ logdev.dev.logs
12
+ end
13
+
14
+ # transforms the logs-array into a hash of logs, grouped by level (:error, :success, ...)
15
+ # @return [Hash] logs
16
+ def logs_to_h
17
+ logs.reduce({}) do |m, log|
18
+ m[log[0]] ||= []
19
+ m[log[0]] << log[2]
20
+ m
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end