Module: RightSupport::Log::Mixin

Included in:
Net::RequestBalancer, 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(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

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

+ (Object) default_logger



90
91
92
# File 'lib/right_support/log/mixin.rb', line 90

def self.default_logger
  @default_logger ||= Decorator.new(RightSupport::Log::NullLogger.new)
end

+ (Object) default_logger=(logger)



94
95
96
97
# File 'lib/right_support/log/mixin.rb', line 94

def self.default_logger=(logger)
  logger = Decorator.new(logger) unless logger.nil? || logger.is_a?(Decorator)
  @default_logger = logger
end

+ (Object) included(base)



99
100
101
102
# File 'lib/right_support/log/mixin.rb', line 99

def self.included(base)
  base.extend(ClassMethods)
  base.__send__(:include, InstanceMethods)
end