ruby_smart-simple_logger 1.3.0 → 1.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 101c7a9ab2862c9ff3e7ace74f812a20a522dfa7fa8c6fcdf9f9accc53fd29e2
4
- data.tar.gz: 1e1f7d6e1c9cbfe48a21cc8cceaa7cd37de7712363ae78e80bcff87aa6c5ca5a
3
+ metadata.gz: a08f3caf3bd643806e098079f29a399c3b99bf5d84528d1e2c034276b99ca2c9
4
+ data.tar.gz: 2a3caee2427ae5d8a51d7931749ac61c338af688c56993f48b9f412dffa85b98
5
5
  SHA512:
6
- metadata.gz: 875fc9ea64929088e6a445f43b1e3a0c7b1c92de106a53c8112c68a3eb28ab5ea081c2a1e21d7d672d7e3109c68e68a83283be41364d65b2c450b6932408e15a
7
- data.tar.gz: 9be8b0019a5afb3b3a9050dcb6139d9cf2ffe9fbd89549ebb717e8e4a2b59ee981dad95948400836a78e8b8205a324dbd2a35dea6638567143e37cbc93425e32
6
+ metadata.gz: ccac78a543d4f6a20d15b86f044754fef1478bb7636607d228e359fa56093838dc1805bdc7147baa8845f97227cf06cc4ace949b3e14ec82745d2ee94c61c4bb
7
+ data.tar.gz: 44b6f4000abfff0f7b0f84eafd9caddf2708892576b6a8ed1d826490ae10795d8aa484ce70829884b23195cef7c52dce51e0801ef55af7321ca812e3527f2195
data/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
  [![Coverage Status](https://coveralls.io/repos/github/ruby-smart/simple_logger/badge.svg?branch=main&kill_cache=1)](https://coveralls.io/github/ruby-smart/simple_logger?branch=main)
10
10
  [![Tests](https://github.com/ruby-smart/simple_logger/actions/workflows/ruby.yml/badge.svg)](https://github.com/ruby-smart/simple_logger/actions/workflows/ruby.yml)
11
11
 
12
- A simple, multifunctional logging library for Ruby.
12
+ A simple, multifunctional logging library for Ruby (and Rails).
13
13
  It features a fast, customizable logging with multi-device support (e.g. log to STDOUT AND file).
14
14
  Special (PRE-defined) scenes can be used for interactive CLI and better logging visibility.
15
15
 
@@ -36,7 +36,7 @@ Or install it yourself as:
36
36
  ## Enhancements
37
37
  * PRE-defined scenes to fastly create a simple, structured CLI output. _(see [Scenes](#scenes))_
38
38
  * Better log-visibility with masked output through scenes
39
- * ```awesome_print``` gem compatibility for a prettified object debug
39
+ * `awesome_print` gem compatibility for a prettified object debug
40
40
  * Multi-device support (write to logfile & to STDOUT & to ... & to ...)
41
41
  * 'klass_logger' instances for easier access _(see [klass_logger](#klass_logger_Usage))_
42
42
 
@@ -198,10 +198,11 @@ While you can just build a new logger _(or use the klass_logger)_ without any ar
198
198
 
199
199
  ### nil Builtin
200
200
 
201
- A ```nil``` builtin will auto-detect the best logging solution for you.
202
- For CLI or windowed programs it'll just send the logs to ```STDOUT```.
203
- For rails-applications it'll send to the current ```Rails.logger``` instance.
204
- Otherwise it'll store logs temporary in memory _(accessible through the #logs method)_
201
+ A `nil` builtin will auto-detect the best logging solution for you:
202
+ * For CLI or windowed programs it'll just send the logs to `STDOUT`.
203
+ * For Debugging _(e.g. IDE-related debugging gems)_ it'll send the logs to the `Debugger`.
204
+ * For rails-applications it'll send to the current `Rails.logger` instance.
205
+ * Otherwise it'll store logs temporary in memory _(accessible through the #logs method)_
205
206
 
206
207
  **Example:**
207
208
  ```ruby
@@ -211,7 +212,7 @@ logger.debug "some debug"
211
212
 
212
213
  ### stdout / stderr Builtin
213
214
 
214
- A ```:stdout / :stderr``` builtin will send to ```STDOUT / STDERR``` and uses a colored output by default.
215
+ A `:stdout / :stderr` builtin will send to `STDOUT / STDERR` and uses a colored output by default.
215
216
 
216
217
  **Example:**
217
218
  ```ruby
@@ -224,9 +225,34 @@ logger = ::SimpleLogger.new(:stdout)
224
225
  logger.debug "some debug"
225
226
  ```
226
227
 
228
+ ### debugger Builtin
229
+
230
+ A `:debugger` builtin will send to the `Debugger` (e.g. your IDE's debugging gem)
231
+
232
+ **Example:**
233
+ ```ruby
234
+ logger = ::SimpleLogger.new(:debugger)
235
+
236
+ # > will be shown within your debugging gem
237
+ logger.debug "some debug"
238
+ ```
239
+
240
+
241
+ ### null Builtin
242
+
243
+ A `:null` builtin will silently swallow all logging data (so it will not be send).
244
+
245
+ **Example:**
246
+ ```ruby
247
+ logger = ::SimpleLogger.new(:null)
248
+
249
+ # >
250
+ logger.debug "some debug"
251
+ ```
252
+
227
253
  ### rails Builtin
228
254
 
229
- A ```:rails``` builtin will always send to the ```Rails.logger``` instance.
255
+ A `:rails` builtin will always send to the `Rails.logger` instance.
230
256
 
231
257
  **Example:**
232
258
  ```ruby
@@ -238,9 +264,9 @@ logger.debug "some debug"
238
264
 
239
265
  ### proc Builtin
240
266
 
241
- A ```:proc``` builtin will call the provided proc _(through ```options[:proc]```)_ everytime a log will be written.
267
+ A `:proc` builtin will call the provided proc _(through `options[:proc]```)_ everytime a log will be written.
242
268
 
243
- The data will be provided as array _( ```[severity, time, progname, data]``` )_.
269
+ The data will be provided as array _( `[severity, time, progname, data]` )_.
244
270
 
245
271
  **Example:**
246
272
  ```ruby
@@ -255,7 +281,7 @@ logger.debug "some debug"
255
281
 
256
282
  ### memory Builtin
257
283
 
258
- A ```:memory``` builtin will always store the logged data within an _instance variable_ and can be accessed through the ```#logs``` or ```#logs_to_h``` methods.
284
+ A `:memory` builtin will always store the logged data within an _instance variable_ and can be accessed through the `#logs` or `#logs_to_h` methods.
259
285
 
260
286
  **Example:**
261
287
  ```ruby
@@ -276,7 +302,7 @@ logger.logs
276
302
 
277
303
  ### String Builtin
278
304
 
279
- Providing a ```String``` will always create and write to a new logfile.
305
+ Providing a `String` will always create and write to a new logfile.
280
306
 
281
307
  **Example:**
282
308
  ```ruby
@@ -313,7 +339,7 @@ noformat_logger.debug "some debug without color and mask - uses the default form
313
339
 
314
340
  ### Module Builtin
315
341
 
316
- Providing a ```module``` will also create and write to a new logfile.
342
+ Providing a `module` will also create and write to a new logfile.
317
343
  The path depends on the provided module name.
318
344
 
319
345
  **Example:**
@@ -331,13 +357,13 @@ logger.debug "some debug"
331
357
 
332
358
  ### other Builtin
333
359
 
334
- Providing any other Object must respond to ```#write```.
360
+ Providing any other Object must respond to `#write```.
335
361
 
336
362
  -----
337
363
 
338
364
  ## Formats
339
365
 
340
- The default formatter _(if no other was provided through ```opts[:formatter```)_ will provide the following PRE-defined formats:
366
+ The default formatter _(if no other was provided through `opts[:formatter```)_ will provide the following PRE-defined formats:
341
367
  _Also prints a colored output by default._
342
368
 
343
369
  ### default Format
@@ -447,7 +473,7 @@ logger = ::SimpleLogger.new(:stdout, payload: false)
447
473
  ### format
448
474
 
449
475
  Provide a other format.
450
- Possible values: ```:default, :passthrough, :plain, :memory, :datalog```
476
+ Possible values: `:default, :passthrough, :plain, :memory, :datalog```
451
477
  ```ruby
452
478
  logger = ::SimpleLogger.new(format: :default)
453
479
  logger = ::SimpleLogger.new(:memory, format: :passthrough)
@@ -465,7 +491,7 @@ logger.debug "debug 2"
465
491
 
466
492
  ### proc _(:proc-builtin ONLY)_
467
493
 
468
- Provide a callback for the ```:proc``` builtin.
494
+ Provide a callback for the `:proc` builtin.
469
495
  ```ruby
470
496
  logger = ::SimpleLogger.new(:proc, proc: lambda{|data| ... })
471
497
  ```
@@ -528,7 +554,7 @@ logger.debug({a: {custom: 'object'}})
528
554
 
529
555
  ### inspector
530
556
 
531
- Provide a other ```inspector``` method for the data-debug.
557
+ Provide a other `inspector` method for the data-debug.
532
558
 
533
559
  ```ruby
534
560
  logger = ::SimpleLogger.new(inspector: :to_s)
@@ -541,13 +567,13 @@ logger.debug({ a: 1, b: 2 })
541
567
 
542
568
  ## _defaults_
543
569
 
544
- Device default options are still available: ```shift_age, shift_size, progname, datetime_format, shift_period_suffix, binmode```
570
+ Device default options are still available: `shift_age, shift_size, progname, datetime_format, shift_period_suffix, binmode```
545
571
 
546
572
  -----
547
573
 
548
574
  ## Scenes
549
575
 
550
- The following PRE-defined scenes are available. _(You can define your own scenes by using the class method ```.scene```)_
576
+ The following PRE-defined scenes are available. _(You can define your own scenes by using the class method `.scene```)_
551
577
 
552
578
  ### debug(data, subject = 'Debug')
553
579
  ```ruby
@@ -768,6 +794,7 @@ end
768
794
  - line
769
795
  - print
770
796
  - nl
797
+ - model _(rails only)_
771
798
 
772
799
  -----
773
800
 
@@ -784,7 +811,7 @@ This project is intended to be a safe, welcoming space for collaboration, and co
784
811
 
785
812
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
786
813
 
787
- A copy of the [LICENSE](docs/LICENSE.txt) can be found @ the docs.
814
+ A copy of the [LICENSE](LICENSE.txt) can be found @ the docs.
788
815
 
789
816
  ## Code of Conduct
790
817
 
data/docs/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # RubySmart::SimpleLogger - CHANGELOG
2
2
 
3
+ ## [1.5.0] - 2024-09-12
4
+ * **[add]** `SimpleLogger.scene?`-method to check for registered scene options
5
+ * **[ref]** scene options to **keyword**-args _(**WARNING:** This may break existing calls to the scene methods)_
6
+ * **[fix]** `model` scene not calling related scene methods
7
+ * **[fix]** `subject` parameter for default severity-methods not cast as string _(now any object may be provided - which calls `#to_s` method)_
8
+
9
+ ## [1.4.0] - 2024-07-31
10
+ * **[add]** 'null'-device / builtin
11
+ * **[add]** 'debugger'-builtin to send logs to the debugging gem
12
+ * **[add]** new logging method `model` _(for rails applications only)_
13
+ * **[ref]** `nil`-builtin to detect `Debugger` after checking for stdout
14
+ * **[ref]** `mask`-length to 120 _(was 100 by default)_
15
+ * **[ref]** `ruby_smart-support`-gem dependency to 1.5
16
+ * **[fix]** exception _(to build a new device)_ if a Logger was provided
17
+ * **[fix]** mask-reference manipulation on inherited classes
18
+
3
19
  ## [1.3.0] - 2023-08-15
4
20
  * **[add]** exception message within `processed`-scene
5
21
  * **[add]** new logging option `tag`, to prefix a log-string with a [TAG]
@@ -4,7 +4,6 @@ module RubySmart
4
4
  module SimpleLogger
5
5
  module Devices
6
6
  class MemoryDevice
7
-
8
7
  attr_reader :logs
9
8
  attr_reader :status
10
9
 
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySmart
4
+ module SimpleLogger
5
+ module Devices
6
+ class NullDevice
7
+ attr_reader :status
8
+
9
+ def initialize
10
+ @status = true
11
+ end
12
+
13
+ def write(*)
14
+ nil
15
+ end
16
+
17
+ alias_method :<<, :write
18
+
19
+ # disables writing
20
+ def close
21
+ @status = false
22
+ end
23
+
24
+ # enables writing
25
+ def reopen
26
+ @status = true
27
+ end
28
+
29
+ # clears all logs
30
+ def clear!
31
+ nil
32
+ end
33
+
34
+ # returns logs
35
+ # @return [Array] logs
36
+ def logs
37
+ []
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -132,11 +132,19 @@ module RubySmart
132
132
  when nil # builtin is nil - resolve optimal device for current environment
133
133
  if ::ThreadInfo.stdout?
134
134
  _resolve_device(:stdout, opts)
135
+ elsif ::ThreadInfo.debugger?
136
+ _resolve_device(:debugger, opts)
135
137
  elsif ::ThreadInfo.rails? && ::Rails.logger
136
138
  _resolve_device(:rails, opts)
137
139
  else
138
140
  _resolve_device(:memory, opts)
139
141
  end
142
+ when :null
143
+ ::RubySmart::SimpleLogger::Devices::NullDevice.new
144
+ when :debugger
145
+ raise "Unable to build SimpleLogger with 'debugger' builtin for not initialized Debugger!" unless ThreadInfo.debugger?
146
+
147
+ _resolve_device(::Debugger.logger, opts)
140
148
  when :stdout
141
149
  STDOUT
142
150
  when :stderr
@@ -148,9 +156,9 @@ module RubySmart
148
156
  if ThreadInfo.console? && ::Rails.logger.instance_variable_get(:@logdev).dev != STDOUT
149
157
  ::RubySmart::SimpleLogger::Devices::MultiDevice
150
158
  .register(_resolve_device(:stdout, opts))
151
- .register(::Rails.logger.instance_variable_get(:@logdev).dev)
159
+ .register(_resolve_device(::Rails.logger, opts))
152
160
  else
153
- ::Rails.logger.instance_variable_get(:@logdev).dev
161
+ _resolve_device(::Rails.logger, opts)
154
162
  end
155
163
  when :proc
156
164
  # force overwrite opts
@@ -171,6 +179,8 @@ module RubySmart
171
179
  # force overwrite opts
172
180
  opts[:clr] = false
173
181
  _logdev(opts, builtin)
182
+ when ::Logger
183
+ builtin.instance_variable_get(:@logdev).dev
174
184
  else
175
185
  _logdev(opts, builtin)
176
186
  end
@@ -360,19 +370,6 @@ module RubySmart
360
370
  res_or_clr.to_sym
361
371
  end
362
372
  end
363
-
364
- # resolves subject & opts from provided args.
365
- # returns provided default subject, if not in args.
366
- # @param [Object] args
367
- # @param [String] subject
368
- # @return [Array]
369
- def _scene_subject_with_opts(args, subject = '')
370
- if args[0].is_a?(Hash)
371
- [subject, args[0]]
372
- else
373
- [args[0] || subject, args[1] || {}]
374
- end
375
- end
376
373
  end
377
374
  end
378
375
  end
@@ -13,17 +13,25 @@ module RubySmart
13
13
  # @option [String] :char - the character to be used as mask
14
14
  # @option [Integer] :length - the mask length (amount of mask chars be line)
15
15
  # @option [Symbol] :clr - the color to be used by printing the mask
16
- self.mask = { char: '=', length: 100, clr: :blue }
16
+ self.mask = { char: '=', length: 120, clr: :blue }
17
17
  end
18
18
  end
19
19
 
20
20
  module ClassMethods
21
21
  def mask
22
- class_variable_get('@@mask')
22
+ @mask
23
23
  end
24
24
 
25
25
  def mask=(mask)
26
- class_variable_set('@@mask', mask)
26
+ @mask = mask
27
+ end
28
+
29
+ # prevents to overwrite parent class through inheriting subclasses
30
+ def inherited(subclass)
31
+ super
32
+
33
+ # dup existing values
34
+ subclass.mask = self.mask.dup
27
35
  end
28
36
  end
29
37
 
@@ -27,6 +27,13 @@ module RubySmart
27
27
  class_variable_set('@@scenes', scenes)
28
28
  end
29
29
 
30
+ # returns true, if provided *key* is a registered scene option
31
+ # @param [Symbol] key
32
+ # @return [Boolean]
33
+ def scene?(key)
34
+ scenes.key?(key)
35
+ end
36
+
30
37
  # registers a new scene by provided key & options
31
38
  # also defines this method by provided block
32
39
  #
@@ -64,11 +71,14 @@ module RubySmart
64
71
 
65
72
  private
66
73
 
67
- # resolves scene options by provided key & merges them with additional options
74
+ # resolves scene options by provided key & merges them with additional *opts*
68
75
  # @param [Symbol] key
69
76
  # @param [Array<Hash>] opts
70
- def _scene_opt(key, *opts)
71
- _opt((scenes[key] || {}), *opts)
77
+ def _scene_opts(key, **opts)
78
+ {
79
+ **(scenes[key] || {}),
80
+ **opts
81
+ }
72
82
  end
73
83
  end
74
84
  end
@@ -9,7 +9,7 @@ module RubySmart
9
9
 
10
10
  module VERSION
11
11
  MAJOR = 1
12
- MINOR = 3
12
+ MINOR = 5
13
13
  TINY = 0
14
14
  PRE = nil
15
15
 
@@ -11,6 +11,7 @@ require_relative 'extensions/timer'
11
11
 
12
12
  require_relative 'devices/memory_device'
13
13
  require_relative 'devices/multi_device'
14
+ require_relative 'devices/null_device'
14
15
  require_relative 'devices/proc_device'
15
16
 
16
17
  require_relative 'formatter'
@@ -11,9 +11,8 @@ module RubySmart
11
11
  # > ================================================= [Debug] ================================================
12
12
  # > "DEBUGGED DATA" <- analyzed by awesome_print#ai method
13
13
  # > ==========================================================================================================
14
- base.scene :debug, { level: :debug, inspect: true, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, *args|
15
- subject, opts = _scene_subject_with_opts(args, 'Debug')
16
- self.log data, _scene_opt(:debug, { subject: subject }, opts)
14
+ base.scene :debug, { level: :debug, inspect: true, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Debug', **opts|
15
+ self.log data, _scene_opts(:debug, subject: subject.to_s, **opts)
17
16
  end
18
17
 
19
18
  # info method (BASE)
@@ -23,9 +22,8 @@ module RubySmart
23
22
  # > ================================================= [Info] =================================================
24
23
  # > DATA
25
24
  # > ==========================================================================================================
26
- base.scene :info, { level: :info, mask: { clr: :cyan }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, *args|
27
- subject, opts = _scene_subject_with_opts(args, 'Info')
28
- self.log data, _scene_opt(:info, { subject: subject }, opts)
25
+ base.scene :info, { level: :info, mask: { clr: :cyan }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Info', **opts|
26
+ self.log data, _scene_opts(:info, subject: subject.to_s, **opts)
29
27
  end
30
28
 
31
29
  # warn method (BASE)
@@ -35,9 +33,8 @@ module RubySmart
35
33
  # > ================================================= [Warn] =================================================
36
34
  # > DATA
37
35
  # > ==========================================================================================================
38
- base.scene :warn, { level: :warn, mask: { clr: :yellow }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, *args|
39
- subject, opts = _scene_subject_with_opts(args, 'Warn')
40
- self.log data, _scene_opt(:warn, { subject: subject }, opts)
36
+ base.scene :warn, { level: :warn, mask: { clr: :yellow }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Warn', **opts|
37
+ self.log data, _scene_opts(:warn, subject: subject.to_s, **opts)
41
38
  end
42
39
 
43
40
  # error method (BASE)
@@ -47,9 +44,8 @@ module RubySmart
47
44
  # > ================================================ [Error] =================================================
48
45
  # > DATA
49
46
  # > ==========================================================================================================
50
- base.scene :error, { level: :error, mask: { clr: :red }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data,*args|
51
- subject, opts = _scene_subject_with_opts(args, 'Error')
52
- self.log data, _scene_opt(:error, { subject: subject }, opts)
47
+ base.scene :error, { level: :error, mask: { clr: :red }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Error', **opts|
48
+ self.log data, _scene_opts(:error, subject: subject.to_s, **opts)
53
49
  end
54
50
 
55
51
  # fatal method (BASE)
@@ -59,9 +55,8 @@ module RubySmart
59
55
  # > ================================================ [Fatal] =================================================
60
56
  # > DATA
61
57
  # > ==========================================================================================================
62
- base.scene :fatal, { level: :fatal, mask: { clr: :bg_red }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, *args|
63
- subject, opts = _scene_subject_with_opts(args, 'Fatal')
64
- self.log data, _scene_opt(:fatal, { subject: subject }, opts)
58
+ base.scene :fatal, { level: :fatal, mask: { clr: :bg_red }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Fatal', **opts|
59
+ self.log data, _scene_opts(:fatal, subject: subject.to_s, **opts)
65
60
  end
66
61
 
67
62
  # unknown method (BASE)
@@ -71,9 +66,8 @@ module RubySmart
71
66
  # > =============================================== [Unknown] ================================================
72
67
  # > DATA
73
68
  # > ==========================================================================================================
74
- base.scene :unknown, { level: :unknown, mask: { clr: :gray }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, *args|
75
- subject, opts = _scene_subject_with_opts(args, 'Unknown')
76
- self.log data, _scene_opt(:unknown, { subject: subject }, opts)
69
+ base.scene :unknown, { level: :unknown, mask: { clr: :gray }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Unknown', **opts|
70
+ self.log data, _scene_opts(:unknown, subject: subject.to_s, **opts)
77
71
  end
78
72
 
79
73
  # success method
@@ -83,9 +77,8 @@ module RubySmart
83
77
  # > ================================================ [Success] ================================================
84
78
  # > DATA
85
79
  # > ===========================================================================================================
86
- base.scene :success, { level: :success, mask: { clr: :green }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, *args|
87
- subject, opts = _scene_subject_with_opts(args, 'Success')
88
- self.log data, _scene_opt(:success, { subject: subject }, opts)
80
+ base.scene :success, { level: :success, mask: { clr: :green }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Success', **opts|
81
+ self.log data, _scene_opts(:success, subject: subject.to_s, **opts)
89
82
  end
90
83
 
91
84
  # header method
@@ -95,11 +88,11 @@ module RubySmart
95
88
  # > ===========================================================================================================
96
89
  # > ================================================ <Subject> ================================================
97
90
  # > ===========================================================================================================
98
- base.scene :header, { level: :debug, payload: [:mask, [:mask, ' <%{subject}> '], :mask] } do |subject, opts = {}|
91
+ base.scene :header, { level: :debug, payload: [:mask, [:mask, ' <%{subject}> '], :mask] } do |subject, **opts|
99
92
  # autostart a timer method, if required
100
93
  self.timer(:start, :default) if opts[:timer]
101
94
 
102
- self.log nil, _scene_opt(:header, { subject: subject }, opts)
95
+ self.log nil, _scene_opts(:header, subject: subject.to_s, **opts)
103
96
  end
104
97
 
105
98
  # footer method
@@ -109,8 +102,8 @@ module RubySmart
109
102
  # > ===========================================================================================================
110
103
  # > ================================================ >Subject< ================================================
111
104
  # > ===========================================================================================================
112
- base.scene :footer, { level: :debug, payload: [:mask, [:mask, ' >%{subject}< '], :mask] } do |subject, opts = {}|
113
- self.log nil, _scene_opt(:footer, { subject: subject }, opts)
105
+ base.scene :footer, { level: :debug, payload: [:mask, [:mask, ' >%{subject}< '], :mask] } do |subject, **opts|
106
+ self.log nil, _scene_opts(:footer, subject: subject.to_s, **opts)
114
107
 
115
108
  # clears & prints timer
116
109
  self.desc("duration: #{self.timer(:clear, :default, :humanized => true)}") if opts[:timer]
@@ -123,8 +116,8 @@ module RubySmart
123
116
  # > --------------------------------------------------------------------------------
124
117
  # > #----------------------------------- Subject ----------------------------------#
125
118
  # > --------------------------------------------------------------------------------
126
- base.scene :topic, { level: :debug, mask: { char: '-', length: 95, clr: :blueish }, payload: [:mask, [:mask, '%{title}'], :mask] } do |subject, opts = {}|
127
- opts = _scene_opt(:topic, opts)
119
+ base.scene :topic, { level: :debug, mask: { char: '-', length: 95, clr: :blueish }, payload: [:mask, [:mask, '%{title}'], :mask] } do |subject, **opts|
120
+ opts = _scene_opts(:topic, **opts)
128
121
  txt = " #{subject} ".center(opts[:mask][:length] - 2, opts[:mask][:char])
129
122
  opts[:title] = "##{txt}#"
130
123
 
@@ -137,8 +130,8 @@ module RubySmart
137
130
  #
138
131
  # > # Subject
139
132
  # > ----------------------------------------------------------------------
140
- base.scene :theme, { level: :debug, clr: :purple, mask: { char: '-', length: 85, clr: :purple }, payload: [[:txt, '# %{subject}'], :mask] } do |subject, opts = {}|
141
- self.log nil, _scene_opt(:theme, { subject: subject }, opts)
133
+ base.scene :theme, { level: :debug, clr: :purple, mask: { char: '-', length: 85, clr: :purple }, payload: [[:txt, '# %{subject}'], :mask] } do |subject, **opts|
134
+ self.log nil, _scene_opts(:theme, subject: subject.to_s, **opts)
142
135
  end
143
136
 
144
137
  # theme_result method
@@ -148,9 +141,9 @@ module RubySmart
148
141
  # > ----------------------------------------------------------------------
149
142
  # > -> Result
150
143
  # >
151
- base.scene :theme_result, { level: :debug, mask: { char: '-', length: 85, clr: :purple }, payload: [:mask, [:txt, '-> %{result}'], ''] } do |result, status = nil, opts = {}|
144
+ base.scene :theme_result, { level: :debug, mask: { char: '-', length: 85, clr: :purple }, payload: [:mask, [:txt, '-> %{result}'], ''] } do |result, status = nil, **opts|
152
145
  res_or_clr = status.nil? ? result : status
153
- self.log nil, _scene_opt(:theme_result, { result: result, clr: _res_clr(res_or_clr) }, opts)
146
+ self.log nil, _scene_opts(:theme_result, result: result, clr: _res_clr(res_or_clr), **opts)
154
147
  end
155
148
 
156
149
  # theme_line method
@@ -158,8 +151,8 @@ module RubySmart
158
151
  # prints: colored line with no text
159
152
  #
160
153
  # > ----------------------------------------------------------------------
161
- base.scene :theme_line, { level: :debug, mask: { char: '-', length: 85, clr: :purple }, payload: [:mask] } do |opts = {}|
162
- self.log nil, _scene_opt(:theme_line, opts)
154
+ base.scene :theme_line, { level: :debug, mask: { char: '-', length: 85, clr: :purple }, payload: [:mask] } do |**opts|
155
+ self.log nil, _scene_opts(:theme_line, **opts)
163
156
  end
164
157
 
165
158
  # desc method
@@ -168,8 +161,8 @@ module RubySmart
168
161
  #
169
162
  # > "description"
170
163
  # >
171
- base.scene :desc, { level: :debug, clr: :purple, payload: [[:txt, '%{description}']] } do |description, opts = {}|
172
- self.log nil, _scene_opt(:desc, { description: description.to_s }, opts)
164
+ base.scene :desc, { level: :debug, clr: :purple, payload: [[:txt, '%{description}']] } do |description, **opts|
165
+ self.log nil, _scene_opts(:desc, description: description.to_s, **opts)
173
166
  end
174
167
 
175
168
  # job method
@@ -179,8 +172,8 @@ module RubySmart
179
172
  #
180
173
  # > - Job name =>
181
174
  # ________________________________________________________________ <- 64 chars
182
- base.scene :job, { level: :debug, clr: :cyan, nl: false, length: 64, payload: [[:concat, ['- ', [:txt, '%{name}'], ' => ']]] } do |name, opts = {}, &block|
183
- self.log nil, _scene_opt(:job, { name: name }, opts)
175
+ base.scene :job, { level: :debug, clr: :cyan, nl: false, length: 64, payload: [[:concat, ['- ', [:txt, '%{name}'], ' => ']]] } do |name, **opts, &block|
176
+ self.log nil, _scene_opts(:job, name: name, **opts)
184
177
  self.result(*block.call) if block_given?
185
178
  end
186
179
 
@@ -191,8 +184,8 @@ module RubySmart
191
184
  #
192
185
  # > * Subjob name =>
193
186
  # ______________________________________________________________ <- 62 chars
194
- base.scene :sub_job, { level: :debug, clr: :cyan, nl: false, length: 62, payload: [[:concat, [' * ', [:txt, '%{name}'], ' => ']]] } do |name, opts = {}, &block|
195
- self.log nil, _scene_opt(:sub_job, { name: name }, opts)
187
+ base.scene :sub_job, { level: :debug, clr: :cyan, nl: false, length: 62, payload: [[:concat, [' * ', [:txt, '%{name}'], ' => ']]] } do |name, **opts, &block|
188
+ self.log nil, _scene_opts(:sub_job, name: name, **opts)
196
189
  self.result(*block.call) if block_given?
197
190
  end
198
191
 
@@ -201,9 +194,9 @@ module RubySmart
201
194
  # prints: colored result
202
195
  #
203
196
  # > Result
204
- base.scene :result, { level: :debug, payload: [[:txt, '%{result}']] } do |result, status = nil, opts = {}|
197
+ base.scene :result, { level: :debug, payload: [[:txt, '%{result}']] } do |result, status = nil, **opts|
205
198
  res_or_clr = status.nil? ? result : status
206
- self.log nil, _scene_opt(:result, { result: result, clr: _res_clr(res_or_clr) }, opts)
199
+ self.log nil, _scene_opts(:result, result: result, clr: _res_clr(res_or_clr), **opts)
207
200
  end
208
201
 
209
202
  # job_result method
@@ -211,9 +204,9 @@ module RubySmart
211
204
  # prints: job with combined colored result
212
205
  #
213
206
  # > - Job name => Result
214
- base.scene :job_result, { level: :debug } do |name, result, status = nil, opts = {}|
215
- self.job(name, opts)
216
- self.result(result, status, opts)
207
+ base.scene :job_result, { level: :debug } do |name, result, status = nil, **opts|
208
+ self.job(name, **opts)
209
+ self.result(result, status, **opts)
217
210
  end
218
211
 
219
212
  # sub_job_result method
@@ -221,9 +214,9 @@ module RubySmart
221
214
  # prints: sub_job with combined colored result
222
215
  #
223
216
  # > * Subjob name => Result
224
- base.scene :sub_job_result, { level: :debug } do |name, result, status = nil, opts = {}|
225
- self.sub_job(name, opts)
226
- self.result(result, status, opts)
217
+ base.scene :sub_job_result, { level: :debug } do |name, result, status = nil, **opts|
218
+ self.sub_job(name, **opts)
219
+ self.result(result, status, **opts)
227
220
  end
228
221
 
229
222
  # line method
@@ -231,8 +224,8 @@ module RubySmart
231
224
  # prints: just a line with data
232
225
  #
233
226
  # > DATA
234
- base.scene :line, { level: :debug } do |data, opts = {}|
235
- self.log data, _scene_opt(:line, opts)
227
+ base.scene :line, { level: :debug } do |data, **opts|
228
+ self.log data, _scene_opts(:line, **opts)
236
229
  end
237
230
 
238
231
  # print method
@@ -240,8 +233,8 @@ module RubySmart
240
233
  # prints: prints data without a newline
241
234
  #
242
235
  # > DATA
243
- base.scene :print, { level: :debug, nl: false } do |data, opts = {}|
244
- self.log data, _scene_opt(:print, opts)
236
+ base.scene :print, { level: :debug, nl: false } do |data, **opts|
237
+ self.log data, _scene_opts(:print, **opts)
245
238
  end
246
239
 
247
240
  # nl method
@@ -250,8 +243,8 @@ module RubySmart
250
243
  #
251
244
  # >
252
245
  # >
253
- base.scene :nl, { level: :debug } do |opts = {}|
254
- self.log '', _scene_opt(:nl, opts)
246
+ base.scene :nl, { level: :debug } do |**opts|
247
+ self.log '', _scene_opts(:nl, **opts)
255
248
  end
256
249
 
257
250
  # spec method
@@ -263,7 +256,7 @@ module RubySmart
263
256
  # "other" => ? (yellow)
264
257
  #
265
258
  # > .FFF...??...F....F...F..???....F...??
266
- base.scene :spec, { level: :debug, nl: false, payload: [[:txt, '%{result}']] } do |status, opts = {}|
259
+ base.scene :spec, { level: :debug, nl: false, payload: [[:txt, '%{result}']] } do |status, **opts|
267
260
  result = if status.is_a?(TrueClass)
268
261
  '.'
269
262
  elsif status.is_a?(FalseClass)
@@ -272,7 +265,7 @@ module RubySmart
272
265
  status = :yellow
273
266
  '?'
274
267
  end
275
- self.log nil, _scene_opt(:spec, { result: result, clr: _res_clr(status) }, opts)
268
+ self.log nil, _scene_opts(:spec, result: result, clr: _res_clr(status), **opts)
276
269
  end
277
270
 
278
271
  # progress method
@@ -283,7 +276,7 @@ module RubySmart
283
276
  # > - Progress of Step 1 [ 40%] ===================>------------------------------
284
277
  # ________________________________________________ <- 48 chars
285
278
  # 50 chars -> __________________________________________________
286
- base.scene :progress, { level: :debug, payload: [[:txt, '- %{name} [%{perc}%] %{progress}']] } do |name, perc, opts = {}|
279
+ base.scene :progress, { level: :debug, payload: [[:txt, '- %{name} [%{perc}%] %{progress}']] } do |name, perc, **opts|
287
280
  pmask_length = 50
288
281
 
289
282
  # convert and fix progress
@@ -298,7 +291,7 @@ module RubySmart
298
291
 
299
292
  progress_string = _clr(('=' * pmask_left_length) + '>', :green) + _clr('-' * pmask_right_length, :red)
300
293
  perc_string = perc.to_s.rjust(3, ' ')
301
- self.log nil, _scene_opt(:progress, { name: _clr(_lgth(name, 48), :cyan), perc: perc_string, progress: progress_string }, opts)
294
+ self.log nil, _scene_opts(:progress, name: _clr(_lgth(name, 48), :cyan), perc: perc_string, progress: progress_string, **opts)
302
295
  end
303
296
 
304
297
  # processed method
@@ -309,7 +302,7 @@ module RubySmart
309
302
  # ╟ doing some cool log
310
303
  # ╟ doing some extra log
311
304
  # ╚ END ❯ job [SUCCESS] (duration: 4.34223)
312
- base.scene :processed, { level: :debug } do |name, opts = {}, &block|
305
+ base.scene :processed, { level: :debug } do |name, **opts, &block|
313
306
  # increase level
314
307
  lvl = processed_lvl(:up)
315
308
 
@@ -326,7 +319,7 @@ module RubySmart
326
319
  self.timer(:start, timer_key)
327
320
 
328
321
  # send START name as +data+ - the full log line is created through the +_pcd+ method.
329
- self.log(name, _scene_opt(:processed, opts, { pcd: :start }))
322
+ self.log(name, _scene_opts(:processed, **opts, pcd: :start))
330
323
 
331
324
  # run the provided block and resolve result
332
325
  result_str = case block.call
@@ -345,7 +338,7 @@ module RubySmart
345
338
  result_str ||= ''
346
339
 
347
340
  # send END name with result & possible time as +data+ - the full log line is created through the +_pcd+ method.
348
- self.log("#{name} #{result_str}#{(timer_key ? "(#{self.timer(:clear, timer_key, humanized: true)})" : '')}", _scene_opt(:processed, opts, { pcd: :end }))
341
+ self.log("#{name} #{result_str}#{(timer_key ? "(#{self.timer(:clear, timer_key, humanized: true)})" : '')}", _scene_opts(:processed, **opts, pcd: :end ))
349
342
 
350
343
  # reduce level
351
344
  processed_lvl(:down)
@@ -353,6 +346,33 @@ module RubySmart
353
346
 
354
347
  true
355
348
  end
349
+
350
+ # model method
351
+ # log level @ error/success/info
352
+ # prints: ActiveRecord::Base related data, depending on the models "save" state (also shows possible errors)
353
+ base.scene :model do |model, status = nil, **opts|
354
+ # build model-logging string
355
+ mdl_string = "#{model.id.present? ? "##{model.id} - " : ''}#{model.to_s[0..49]}"
356
+
357
+ # resolve model's status
358
+ status = ((model.persisted? && model.errors.empty?) ? (model.previous_changes.blank? ? :skipped : :success) : :error) if status.nil?
359
+
360
+ # switch between status
361
+ case status
362
+ when :success
363
+ # show verbose logging for updated records
364
+ if opts[:verbose] != false && !model.previously_new_record?
365
+ self.success("#{mdl_string} (#{model.previous_changes.inspect})", tag: "#{model.class.name.upcase}|UPDATED", **opts)
366
+ else
367
+ self.success(mdl_string, tag: "#{model.class.name.upcase}|#{(model.previously_new_record? ? 'CREATED' : 'UPDATED')}", **opts)
368
+ end
369
+ when :error
370
+ msg_string = model.errors.full_messages.join(', ')
371
+ self.error("#{mdl_string} (#{msg_string.present? ? msg_string : '-'})", tag: "#{model.class.name.upcase}|ERROR", **opts)
372
+ else
373
+ self.info(mdl_string, tag: "#{model.class.name.upcase}|#{status}", **opts)
374
+ end
375
+ end
356
376
  end
357
377
  end
358
378
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ['Tobias Gonsior']
9
9
  spec.email = ['info@ruby-smart.org']
10
10
 
11
- spec.summary = "A simple, multifunctional logging library for Ruby."
11
+ spec.summary = "A simple, multifunctional logging library for Ruby (and Rails)."
12
12
  spec.description = <<~DESC
13
13
  RubySmart::SimpleLogger is a fast, customizable logging library with multi-device support,
14
14
  special (PRE-defined) scenes for better logging visibility.
@@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
33
33
 
34
34
  spec.require_paths = ['lib']
35
35
 
36
- spec.add_dependency 'ruby_smart-support', '~> 1.2'
36
+ spec.add_dependency 'ruby_smart-support', '~> 1.5'
37
37
 
38
38
  spec.add_development_dependency 'awesome_print', '~> 1.9'
39
39
  spec.add_development_dependency 'coveralls_reborn', '~> 0.25'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_smart-simple_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Gonsior
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-14 00:00:00.000000000 Z
11
+ date: 2024-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby_smart-support
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.2'
19
+ version: '1.5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.2'
26
+ version: '1.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: awesome_print
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -135,19 +135,20 @@ files:
135
135
  - ".rspec"
136
136
  - ".yardopts"
137
137
  - Gemfile
138
+ - LICENSE.txt
138
139
  - README.md
139
140
  - Rakefile
140
141
  - bin/console
141
142
  - bin/setup
142
143
  - docs/CHANGELOG.md
143
144
  - docs/CODE_OF_CONDUCT.md
144
- - docs/LICENSE.txt
145
145
  - lib/ruby_smart-debugger.rb
146
146
  - lib/ruby_smart-simple_logger.rb
147
147
  - lib/ruby_smart/simple_logger.rb
148
148
  - lib/ruby_smart/simple_logger/core_ext/ruby/string.rb
149
149
  - lib/ruby_smart/simple_logger/devices/memory_device.rb
150
150
  - lib/ruby_smart/simple_logger/devices/multi_device.rb
151
+ - lib/ruby_smart/simple_logger/devices/null_device.rb
151
152
  - lib/ruby_smart/simple_logger/devices/proc_device.rb
152
153
  - lib/ruby_smart/simple_logger/extensions/helper.rb
153
154
  - lib/ruby_smart/simple_logger/extensions/logs.rb
@@ -189,8 +190,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
190
  - !ruby/object:Gem::Version
190
191
  version: '0'
191
192
  requirements: []
192
- rubygems_version: 3.4.10
193
+ rubygems_version: 3.5.14
193
194
  signing_key:
194
195
  specification_version: 4
195
- summary: A simple, multifunctional logging library for Ruby.
196
+ summary: A simple, multifunctional logging library for Ruby (and Rails).
196
197
  test_files: []
File without changes