Module: RubySmart::SimpleLogger::Scenes

Included in:
Logger
Defined in:
lib/ruby_smart/simple_logger/scenes.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/ruby_smart/simple_logger/scenes.rb', line 6

def self.included(base)
  # debug method
  # log level @ debug
  # prints: prettified data by using the 'inspect' method
  #
  # > ================================================= [Debug] ================================================
  # > "DEBUGGED DATA" <- analyzed by awesome_print#ai method
  # > ==========================================================================================================
  base.scene :debug, { level: :debug, inspect: true, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Debug', **opts|
    self.log data, _scene_opts(:debug, subject: subject.to_s, **opts)
  end

  # info method (BASE)
  # log level @ info
  # prints: enclosed data
  #
  # > ================================================= [Info] =================================================
  # > DATA
  # > ==========================================================================================================
  base.scene :info, { level: :info, mask: { clr: :cyan }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Info', **opts|
    self.log data, _scene_opts(:info, subject: subject.to_s, **opts)
  end

  # warn method (BASE)
  # log level @ warn
  # prints: enclosed data
  #
  # > ================================================= [Warn] =================================================
  # > DATA
  # > ==========================================================================================================
  base.scene :warn, { level: :warn, mask: { clr: :yellow }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Warn', **opts|
    self.log data, _scene_opts(:warn, subject: subject.to_s, **opts)
  end

  # error method (BASE)
  # log level @ error
  # prints: enclosed data
  #
  # > ================================================ [Error] =================================================
  # > DATA
  # > ==========================================================================================================
  base.scene :error, { level: :error, mask: { clr: :red }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Error', **opts|
    self.log data, _scene_opts(:error, subject: subject.to_s, **opts)
  end

  # fatal method (BASE)
  # log level @ fatal
  # prints: enclosed data
  #
  # > ================================================ [Fatal] =================================================
  # > DATA
  # > ==========================================================================================================
  base.scene :fatal, { level: :fatal, mask: { clr: :bg_red }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Fatal', **opts|
    self.log data, _scene_opts(:fatal, subject: subject.to_s, **opts)
  end

  # unknown method (BASE)
  # log level @ unknown
  # prints: enclosed data
  #
  # > =============================================== [Unknown] ================================================
  # > DATA
  # > ==========================================================================================================
  base.scene :unknown, { level: :unknown, mask: { clr: :gray }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Unknown', **opts|
    self.log data, _scene_opts(:unknown, subject: subject.to_s, **opts)
  end

  # success method
  # log level @ success (sub-level of info)
  # prints: enclosed data
  #
  # > ================================================ [Success] ================================================
  # > DATA
  # > ===========================================================================================================
  base.scene :success, { level: :success, mask: { clr: :green }, payload: [[:mask, ' [%{subject}] '], :__data__, :mask] } do |data, subject = 'Success', **opts|
    self.log data, _scene_opts(:success, subject: subject.to_s, **opts)
  end

  # header method
  # log level @ debug
  # prints: prettified subject
  #
  # > ===========================================================================================================
  # > ================================================ <Subject> ================================================
  # > ===========================================================================================================
  base.scene :header, { level: :debug, payload: [:mask, [:mask, ' <%{subject}> '], :mask] } do |subject, **opts|
    # autostart a timer method, if required
    self.timer(:start, :default) if opts[:timer]

    self.log nil, _scene_opts(:header, subject: subject.to_s, **opts)
  end

  # footer method
  # log level @ debug
  # prints: prettified subject
  #
  # > ===========================================================================================================
  # > ================================================ >Subject< ================================================
  # > ===========================================================================================================
  base.scene :footer, { level: :debug, payload: [:mask, [:mask, ' >%{subject}< '], :mask] } do |subject, **opts|
    self.log nil, _scene_opts(:footer, subject: subject.to_s, **opts)

    # clears & prints timer
    self.desc("duration: #{self.timer(:clear, :default, :humanized => true)}") if opts[:timer]
  end

  # topic method
  # log level @ debug
  # prints: prettified subject
  #
  # > --------------------------------------------------------------------------------
  # > #----------------------------------- Subject ----------------------------------#
  # > --------------------------------------------------------------------------------
  base.scene :topic, { level: :debug, mask: { char: '-', length: 95, clr: :blueish }, payload: [:mask, [:mask, '%{title}'], :mask] } do |subject, **opts|
    opts         = _scene_opts(:topic, **opts)
    txt          = " #{subject} ".center(opts[:mask][:length] - 2, opts[:mask][:char])
    opts[:title] = "##{txt}#"

    self.log nil, opts
  end

  # theme method
  # log level @ debug
  # prints: prettified, colored subject
  #
  # > # Subject
  # > ----------------------------------------------------------------------
  base.scene :theme, { level: :debug, clr: :purple, mask: { char: '-', length: 85, clr: :purple }, payload: [[:txt, '# %{subject}'], :mask] } do |subject, **opts|
    self.log nil, _scene_opts(:theme, subject: subject.to_s, **opts)
  end

  # theme_result method
  # log level @ debug
  # prints: prettified, colored result
  #
  # > ----------------------------------------------------------------------
  # > -> Result
  # >
  base.scene :theme_result, { level: :debug, mask: { char: '-', length: 85, clr: :purple }, payload: [:mask, [:txt, '-> %{result}'], ''] } do |result, status = nil, **opts|
    self.log nil, _scene_opts(:theme_result, result: result, clr: _res_clr(status, result), **opts)
  end

  # theme_line method
  # log level @ debug
  # prints: colored line with no text
  #
  # > ----------------------------------------------------------------------
  base.scene :theme_line, { level: :debug, mask: { char: '-', length: 85, clr: :purple }, payload: [:mask] } do |**opts|
    self.log nil, _scene_opts(:theme_line, **opts)
  end

  # desc method
  # log level @ debug
  # prints: colored text
  #
  # > "description"
  # >
  base.scene :desc, { level: :debug, clr: :purple, payload: [[:txt, '%{description}']] } do |description, **opts|
    self.log nil, _scene_opts(:desc, description: description.to_s, **opts)
  end

  # job method
  # log level @ debug
  # prints: colored line with job name (on inline formatter it prevents a line-break)
  # calls the result method if a block was provided
  #
  # > - Job name                                                         =>
  #     ________________________________________________________________ <- 64 chars
  base.scene :job, { level: :debug, clr: :cyan, nl: false, length: 64, payload: [[:concat, ['- ', [:txt, '%{name}'], ' => ']]] } do |name, **opts, &block|
    self.log nil, _scene_opts(:job, name: name, **opts)
    self.result(*block.call) if block
  end

  # sub_job method
  # log level @ debug
  # prints: line with job name (on inline formatter it prevents a line-break)
  # calls the result method if a block was provided
  #
  # >   * Subjob name                                                    =>
  #       ______________________________________________________________ <- 62 chars
  base.scene :sub_job, { level: :debug, clr: :cyan, nl: false, length: 62, payload: [[:concat, ['  * ', [:txt, '%{name}'], ' => ']]] } do |name, **opts, &block|
    self.log nil, _scene_opts(:sub_job, name: name, **opts)
    self.result(*block.call) if block
  end

  # result method
  # log level @ debug
  # prints: colored result
  #
  # > Result
  base.scene :result, { level: :debug, payload: [[:txt, '%{result}']] } do |result, status = nil, **opts|
    self.log nil, _scene_opts(:result, result: result.to_s, clr: _res_clr(status, result), **opts)
  end

  # job_result method
  # log level @ debug
  # prints: job with combined colored result
  #
  # > - Job name                                                         => Result
  base.scene :job_result, { level: :debug } do |name, result, status = nil, **opts|
    self.job(name, **opts)
    self.result(result, status, **opts)
  end

  # sub_job_result method
  # log level @ debug
  # prints: sub_job with combined colored result
  #
  # >   * Subjob name                                                    => Result
  base.scene :sub_job_result, { level: :debug } do |name, result, status = nil, **opts|
    self.sub_job(name, **opts)
    self.result(result, status, **opts)
  end

  # line method
  # log level @ debug
  # prints: just a line with data
  #
  # > DATA
  base.scene :line, { level: :debug } do |data, **opts|
    self.log data, _scene_opts(:line, **opts)
  end

  # print method
  # log level @ debug
  # prints: prints data without a newline
  #
  # > DATA
  base.scene :print, { level: :debug, nl: false } do |data, **opts|
    self.log data, _scene_opts(:print, **opts)
  end

  # nl method
  # log level @ debug
  # prints: a line break without any data
  #
  # >
  # >
  base.scene :nl, { level: :debug } do |**opts|
    self.log '', _scene_opts(:nl, **opts)
  end

  # spec method
  # log level @ debug
  # prints: colored spec result string - depending on the status (on inline formatter it prevents a line-break)
  #
  # true      => . (green)
  # false     => F (red)
  # "other"   => ? (yellow)
  #
  # > .FFF...??...F....F...F..???....F...??
  base.scene :spec, { level: :debug, nl: false, payload: [[:txt, '%{result}']] } do |status, **opts|
    result = case status
             when true
               '.'
             when false
               'F'
             else
               status = nil # IMPORTANT: reset to nil will enforce a 'yellow' color
               '?'
             end
    self.log nil, _scene_opts(:spec, result: result, clr: _res_clr(status), **opts)
  end

  # progress method
  # log level @ debug
  # prints: a colored progress indicator
  #
  # > - Progress of Step 0                               [  0%] >-------------------------------------------------
  # > - Progress of Step 1                               [ 40%] ===================>------------------------------
  #     ________________________________________________ <- 48 chars
  #                                                 50 chars -> __________________________________________________
  base.scene :progress, { level: :debug, payload: [[:txt, '- %{name} [%{perc}%] %{progress}']] } do |name, perc, **opts|
    pmask_length = 50

    # convert and fix progress
    perc = perc.to_i
    perc = 0 if perc < 0
    perc = 100 if perc > 100

    pmask_left_length  = (pmask_length * perc / 100)
    # reduce 1 char for the arrow '>'
    pmask_left_length  -= 1 if pmask_left_length > 0
    pmask_right_length = pmask_length - pmask_left_length - 1

    progress_string = _clr(('=' * pmask_left_length) + '>', :green) + _clr('-' * pmask_right_length, :red)
    perc_string     = perc.to_s.rjust(3, ' ')
    self.log nil, _scene_opts(:progress, name: _clr(_lgth(name, 48), :cyan), perc: perc_string, progress: progress_string, **opts)
  end

  # processed method
  # log level @ debug
  # prints: a processed output with unicode box-chars (e.g. ║ )
  #
  # ╔ START ❯ job
  # ╟ doing some cool log
  # ╟ doing some extra log
  # ╚ END   ❯ job [SUCCESS] (duration: 4.34223)
  base.scene :processed, { level: :debug } do |name, **opts, &block|
    # increase level
    lvl = processed_lvl(:up)

    # resolve a new time-key.
    # The key depends on the current level - this should be possible, since processes on the same level should not be possible
    timer_key = if opts[:timer]
                  "processed_#{lvl}"
                else
                  nil
                end

    begin
      # starts new time (if key was created)
      self.timer(:start, timer_key)

      # send START name as +data+ - the full log line is created through the +_pcd+ method.
      self.log(name, _scene_opts(:processed, **opts, pcd: :start))

      # run the provided block and resolve result
      result_str = case block.call
                   when true
                     '[SUCCESS]'.bg_green + ' '
                   when false
                     '[FAIL]'.bg_red + ' '
                   else
                     ''
                   end
    rescue => e
      self.fatal("#{e.message} @ #{e.backtrace_locations&.first}") unless opts[:silent]
      # reraise exception
      raise
    ensure
      result_str ||= ''

      # send END name with result & possible time as +data+ - the full log line is created through the +_pcd+ method.
      self.log("#{name} #{result_str}#{(timer_key ? "(#{self.timer(:clear, timer_key, humanized: true)})" : '')}", _scene_opts(:processed, **opts, pcd: :end))

      # reduce level
      processed_lvl(:down)
    end

    true
  end

  # model method
  # log level @ error/success/info
  # prints: ActiveRecord::Base related data, depending on the models "save" state (also shows possible errors)
  base.scene :model do |model, status = nil, **opts|
    # build model-logging string
    mdl_string = "#{model.id.present? ? "##{model.id} - " : ''}#{model.to_s[0..49]}"

    # resolve model's status
    status     = ((model.persisted? && model.errors.empty?) ? (model.previous_changes.blank? ? :skipped : :success) : :error) if status.nil?

    # switch between status
    case status
    when :success
      # show verbose logging for updated records
      if opts[:verbose] && !model.previously_new_record?
        self.success("#{mdl_string} (#{model.previous_changes.inspect})", tag: "#{model.class.name.upcase}|UPDATED", **opts)
      else
        self.success(mdl_string, tag: "#{model.class.name.upcase}|#{(model.previously_new_record? ? 'CREATED' : 'UPDATED')}", **opts)
      end
    when :error
      msg_string = model.errors.full_messages.join(', ')
      self.error("#{mdl_string} (#{msg_string.present? ? msg_string : '-'})", tag: "#{model.class.name.upcase}|ERROR", **opts)
    else
      self.info(mdl_string, tag: "#{model.class.name.upcase}|#{status}", **opts)
    end
  end
end