Class: SweetNotifications::LogSubscriber
- Inherits:
-
ActiveSupport::LogSubscriber
- Object
- ActiveSupport::LogSubscriber
- SweetNotifications::LogSubscriber
- Defined in:
- lib/sweet_notifications/log_subscriber.rb
Overview
LogSubscriber with runtime calculation and improved logging
Class Method Summary collapse
-
.color(odd, even = nil) ⇒ Object
Set colors for logging title and duration.
-
.event(command, runtime: true) {|ActiveSupport::Notifications::Event| ... } ⇒ Object
Define an event subscriber.
-
.reset_runtime ⇒ Object
Reset aggregated runtime.
-
.runtime ⇒ Object
Fetch aggregated runtime form request specific store.
-
.runtime=(value) ⇒ Object
Store aggregated runtime form request specific store.
Instance Method Summary collapse
-
#initialize ⇒ LogSubscriber
constructor
A new instance of LogSubscriber.
-
#message(event, label, body) ⇒ String
Format a message for logging.
Constructor Details
#initialize ⇒ LogSubscriber
Returns a new instance of LogSubscriber.
10 11 12 13 |
# File 'lib/sweet_notifications/log_subscriber.rb', line 10 def initialize super @odd = false end |
Class Method Details
.color(odd, even = nil) ⇒ Object
Set colors for logging title and duration
60 61 62 63 |
# File 'lib/sweet_notifications/log_subscriber.rb', line 60 def color(odd, even = nil) self.odd_color = odd self.even_color = even || odd end |
.event(command, runtime: true) {|ActiveSupport::Notifications::Event| ... } ⇒ Object
Define an event subscriber
70 71 72 73 74 75 |
# File 'lib/sweet_notifications/log_subscriber.rb', line 70 def event(command, runtime: true, &block) define_method command do |event| self.class.runtime += event.duration if runtime instance_exec(event, &block) if block end end |
.reset_runtime ⇒ Object
Reset aggregated runtime
54 55 56 57 |
# File 'lib/sweet_notifications/log_subscriber.rb', line 54 def reset_runtime rt, self.runtime = runtime, 0 rt end |
.runtime ⇒ Object
Fetch aggregated runtime form request specific store
47 48 49 |
# File 'lib/sweet_notifications/log_subscriber.rb', line 47 def runtime RequestStore.store["#{@name}_runtime"] || 0 end |
.runtime=(value) ⇒ Object
Store aggregated runtime form request specific store
42 43 44 |
# File 'lib/sweet_notifications/log_subscriber.rb', line 42 def runtime=(value) RequestStore.store["#{@name}_runtime"] = value end |
Instance Method Details
#message(event, label, body) ⇒ String
Format a message for logging
Examples
event :test do |event|
(event, 'Test', 'message body')
end
# => " Test (0.00ms) message body"
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sweet_notifications/log_subscriber.rb', line 28 def (event, label, body) @odd = !@odd label_color = @odd ? odd_color : even_color format( ' %s (%.2fms) %s', color(label, label_color, true), event.duration, color(body, nil, !@odd) ) end |