Class: ActiveSupport::LogSubscriber
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Subscriber
|
|
Instance Chain:
self,
Subscriber
|
|
Inherits: |
ActiveSupport::Subscriber
|
Defined in: | activesupport/lib/active_support/log_subscriber.rb, activesupport/lib/active_support/log_subscriber/test_helper.rb |
Overview
LogSubscriber
is an object set to consume Notifications
with the sole purpose of logging them. The log subscriber dispatches notifications to a registered object based on its given namespace.
An example would be Active Record log subscriber responsible for logging queries:
module ActiveRecord
class LogSubscriber < ActiveSupport::LogSubscriber
attach_to :active_record
def sql(event)
info "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}"
end
end
end
ActiveRecord::LogSubscriber.logger
must be set as well, but it is assigned automatically in a Rails environment.
After configured, whenever a "sql.active_record"
notification is published, it will properly dispatch the event (ActiveSupport::Notifications::Event) to the sql
method.
Being an Notifications
consumer, LogSubscriber
exposes a simple interface to check if instrumented code raises an exception. It is common to log a different message in case of an error, and this can be achieved by extending the previous example:
module ActiveRecord
class LogSubscriber < ActiveSupport::LogSubscriber
def sql(event)
exception = event.payload[:exception]
if exception
exception_object = event.payload[:exception_object]
error "[ERROR] #{event.payload[:name]}: #{exception.join(', ')} " \
"(#{exception_object.backtrace.first})"
else
# standard logger code
end
end
end
end
LogSubscriber
also has some helpers to deal with logging. For example, .flush_all! will ensure that all logs are flushed, and it is called in ::Rails::Rack::Logger
after a request finishes.
Constant Summary
-
BLACK =
ANSI sequence colors
"\e[30m"
-
BLUE =
# File 'activesupport/lib/active_support/log_subscriber.rb', line 78"\e[34m"
-
CYAN =
# File 'activesupport/lib/active_support/log_subscriber.rb', line 80"\e[36m"
-
GREEN =
# File 'activesupport/lib/active_support/log_subscriber.rb', line 76"\e[32m"
-
LEVEL_CHECKS =
Internal use only
# File 'activesupport/lib/active_support/log_subscriber.rb', line 86{ debug: -> (logger) { !logger.debug? }, info: -> (logger) { !logger.info? }, error: -> (logger) { !logger.error? }, }
-
MAGENTA =
# File 'activesupport/lib/active_support/log_subscriber.rb', line 79"\e[35m"
-
MODES =
ANSI sequence modes
{ clear: 0, bold: 1, italic: 3, underline: 4, }
-
RED =
# File 'activesupport/lib/active_support/log_subscriber.rb', line 75"\e[31m"
-
WHITE =
# File 'activesupport/lib/active_support/log_subscriber.rb', line 81"\e[37m"
-
YELLOW =
# File 'activesupport/lib/active_support/log_subscriber.rb', line 77"\e[33m"
Class Attribute Summary
- .colorize_logging (also: #colorize_logging) rw
- .log_levels rw Internal use only
- .log_levels? ⇒ Boolean rw Internal use only
- .logger rw Internal use only
- .logger=(value) rw Internal use only
Subscriber
- Inherited
Class Method Summary
- .attach_to Internal use only
-
.flush_all!
Internal use only
Flush all log_subscribers’ logger.
- .log_subscribers Internal use only
- .new ⇒ LogSubscriber constructor Internal use only
- .fetch_public_methods(subscriber, inherit_all) private Internal use only
- .set_event_levels private Internal use only
- .subscribe_log_level(method, level) private Internal use only
Subscriber
- Inherited
.attach_to | Attach the subscriber to a namespace. |
.detach_from | Detach the subscriber from a namespace. |
.method_added | Adds event subscribers for all new methods added to the class. |
.new, .subscribers, .add_event_subscriber, .fetch_public_methods, .find_attached_subscriber, .invalid_event?, .pattern_subscribed?, .prepare_pattern, .remove_event_subscriber |
Instance Attribute Summary
- #colorize_logging rw
- #event_levels=(value) writeonly Internal use only
Subscriber
- Inherited
Instance Method Summary
- #call(event) Internal use only
- #logger Internal use only
- #publish_event(event) Internal use only
- #silenced?(event) ⇒ Boolean Internal use only
-
#color(text, color, mode_options = {})
private
Internal use only
Set color by using a symbol or one of the defined constants.
- #log_exception(name, e) private Internal use only
- #mode_from(options) private Internal use only
Subscriber
- Inherited
Constructor Details
.new ⇒ LogSubscriber
# File 'activesupport/lib/active_support/log_subscriber.rb', line 133
def initialize super @event_levels = {} end
Class Attribute Details
.colorize_logging (rw) Also known as: #colorize_logging
[ GitHub ]# File 'activesupport/lib/active_support/log_subscriber.rb', line 83
mattr_accessor :colorize_logging, default: true
.log_levels (rw)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 84
class_attribute :log_levels, instance_accessor: false, default: {} # :nodoc:
.log_levels? ⇒ Boolean
(rw)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 84
class_attribute :log_levels, instance_accessor: false, default: {} # :nodoc:
.logger (rw)
.logger=(value) (rw)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 105
attr_writer :logger
Class Method Details
.attach_to
# File 'activesupport/lib/active_support/log_subscriber.rb', line 99
def attach_to(...) # :nodoc: result = super set_event_levels result end
.fetch_public_methods(subscriber, inherit_all) (private)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 117
def fetch_public_methods(subscriber, inherit_all) subscriber.public_methods(inherit_all) - LogSubscriber.public_instance_methods(true) end
.flush_all!
Flush all log_subscribers’ logger.
.log_subscribers
# File 'activesupport/lib/active_support/log_subscriber.rb', line 107
def log_subscribers subscribers end
.set_event_levels (private)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 121
def set_event_levels if subscriber subscriber.event_levels = log_levels.transform_keys { |k| "#{k}.#{namespace}" } end end
.subscribe_log_level(method, level) (private)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 127
def subscribe_log_level(method, level) self.log_levels = log_levels.merge(method => LEVEL_CHECKS.fetch(level)) set_event_levels end
Instance Attribute Details
#colorize_logging (rw)
[ GitHub ]# File 'activesupport/lib/active_support/log_subscriber.rb', line 83
mattr_accessor :colorize_logging, default: true
#event_levels=(value) (writeonly)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 158
attr_writer :event_levels # :nodoc:
Instance Method Details
#call(event)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 146
def call(event) super if logger rescue => e log_exception(event.name, e) end
#color(text, color, mode_options = {}) (private)
Set color by using a symbol or one of the defined constants. Set modes by specifying bold, italic, or underline options. Inspired by Highline, this method will automatically clear formatting at the end of the returned ::String
.
# File 'activesupport/lib/active_support/log_subscriber.rb', line 172
def color(text, color, = {}) # :doc: return text unless colorize_logging color = self.class.const_get(color.upcase) if color.is_a?(Symbol) mode = mode_from( ) clear = "\e[#{MODES[:clear]}m" "#{mode}#{color}#{text}#{clear}" end
#log_exception(name, e) (private)
#logger
# File 'activesupport/lib/active_support/log_subscriber.rb', line 138
def logger LogSubscriber.logger end
#mode_from(options) (private)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 180
def mode_from( ) modes = MODES.values_at(* .compact_blank.keys) "\e[#{modes.join(";")}m" if modes.any? end
#publish_event(event)
# File 'activesupport/lib/active_support/log_subscriber.rb', line 152
def publish_event(event) super if logger rescue => e log_exception(event.name, e) end