class SweetNotifications::LogSubscriber
LogSubscriber
with runtime calculation and improved logging
Public Class Methods
color(odd, even = nil)
click to toggle source
Set colors for logging title and duration
# File lib/sweet_notifications/log_subscriber.rb, line 61 def color(odd, even = nil) self.odd_color = odd self.even_color = even || odd end
event(command, runtime: true, &block)
click to toggle source
Define an event subscriber
@param command [Symbol] event name @param runtime [Boolean] aggregate runtime for this event @yield [ActiveSupport::Notifications::Event] handle event
# File lib/sweet_notifications/log_subscriber.rb, line 71 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
new()
click to toggle source
Calls superclass method
# File lib/sweet_notifications/log_subscriber.rb, line 10 def initialize super @odd = false end
reset_runtime()
click to toggle source
Reset aggregated runtime
@return Numeric previous runtime value
# File lib/sweet_notifications/log_subscriber.rb, line 54 def reset_runtime rt = runtime self.runtime = 0 rt end
runtime()
click to toggle source
Fetch aggregated runtime form request specific store
# File lib/sweet_notifications/log_subscriber.rb, line 47 def runtime RequestStore.store["#{@name}_runtime"] || 0 end
runtime=(value)
click to toggle source
Store aggregated runtime form request specific store
# File lib/sweet_notifications/log_subscriber.rb, line 42 def runtime=(value) RequestStore.store["#{@name}_runtime"] = value end
Protected Class Methods
inherited(base)
click to toggle source
Calls superclass method
# File lib/sweet_notifications/log_subscriber.rb, line 80 def inherited(base) super base.class_eval do @name ||= SecureRandom.hex end end
Public Instance Methods
message(event, label, body)
click to toggle source
Format a message for logging
@param event [ActiveSupport::Notifications::Event] subscribed event @param label [String] label for log messages @param body [String] the rest @return [String] formatted message for logging
Examples¶ ↑
event :test do |event| message(event, 'Test', 'message body') end # => " Test (0.00ms) message body"
# File lib/sweet_notifications/log_subscriber.rb, line 28 def message(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