Module: RightSupport::Log::Mixin
- Included in:
- DB::CassandraModel, Net::LB::HealthCheck, Net::RequestBalancer, Notifier::Base, Stats::Exceptions
- Defined in:
- lib/right_support/log/mixin.rb
Overview
A mixin that facilitates access to a logger for classes that want logging functionality.
Basic Usage
Your class must opt into logging by including the mixin:
class AwesomenessProcessor
include RightSupport::Log::Mixin
Having opted in, your class now has a #logger instance method, as well as a .logger class method, which allows logging from either instance or class methods:
def self.prepare_awesomeness_for_processing(input)
logger.info "Preparing a #{input.class.name} for additional awesomeness"
end
def process_awesomeness(input)
input = self.class.prepare_awesomeness_for_processing(input)
logger.info "Processing #{input.size} units of awesomeness"
end
Controlling Where Log Messages Go
By default, your class shares a Logger object with all other classes that include the mixin. This process-wide default logger can be set or retrieved using module-level accessors:
# default_logger starts out as a NullLogger; you probably want to set it to something different
puts "Current logger: "+ RightSupport::Log::Mixin.default_logger.class
RightSupport::Log::Mixin.default_logger = SyslogLogger.new('my program')
It is good form to set the default logger; however, if your class needs additional or different logging functionality, you can override the logger on a per-class level:
AwesomenessProcessor.logger = Logger.new(File.open('awesomeness.log', 'w'))
Finally, you can override the logger on a per-instance level for truly fine-grained control. This is generally useless, but just in case:
processor = AwesomenessProcessor.new
processor.logger = Logger.new(File.open("#{processor.object_id}.log", 'w'))
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Constant Summary collapse
- UNDELEGATED =
List of base classes to which a class-level .logger call is NEVER delegated, even if the base class object responds to .logger; this protects against other metaprogramming that defines Class#logger or Object#logger.
[Class, Object]
- Decorator =
A decorator class which will be wrapped around any logger that is provided to any of the setter methods. This ensures that ExceptionLogger’s methods will always be available to anyone who uses this mixin for logging.
RightSupport::Log::ExceptionLogger
Class Method Summary collapse
Class Method Details
.default_logger ⇒ Object
102 103 104 |
# File 'lib/right_support/log/mixin.rb', line 102 def self.default_logger @default_logger ||= Decorator.new(RightSupport::Log::NullLogger.new) end |
.default_logger=(logger) ⇒ Object
106 107 108 109 |
# File 'lib/right_support/log/mixin.rb', line 106 def self.default_logger=(logger) logger = Decorator.new(logger) unless logger.nil? || logger.is_a?(Decorator) @default_logger = logger end |
.included(base) ⇒ Object
111 112 113 114 |
# File 'lib/right_support/log/mixin.rb', line 111 def self.included(base) base.extend(ClassMethods) base.__send__(:include, InstanceMethods) end |