module Derelict::Utils::Logger

Provides a method to retrieve a logger

Public Instance Methods

logger(options = {}) click to toggle source

Retrieves the logger for this class

# File lib/derelict/utils/logger.rb, line 10
def logger(options = {})
  options = {:type => :internal}.merge(options)

  case options[:type].to_sym
    when :external
      external_logger
    when :internal
      find_or_create_logger(logger_name)
    else raise InvalidType.new options[:type]
  end
end

Private Instance Methods

external_logger() click to toggle source

Gets the “external” logger, used to print to stdout

# File lib/derelict/utils/logger.rb, line 37
def external_logger
  @@external ||= find_or_create_logger("external").tap do |external|
    logger.debug "Created external logger instance"
    external.add(Log4r::Outputter.stdout.tap do |outputter|
      outputter.formatter = RawFormatter.new
    end)
  end
end
find_or_create_logger(fullname) click to toggle source

Finds or creates a Logger with a particular fullname

# File lib/derelict/utils/logger.rb, line 32
def find_or_create_logger(fullname)
  Log4r::Logger[fullname.to_s] || Log4r::Logger.new(fullname.to_s)
end
logger_name() click to toggle source

Retrieves the name of the logger for this class

By default, the name of the logger is just the lowercase version of the class name.

# File lib/derelict/utils/logger.rb, line 50
def logger_name
  if self.is_a? Module
    self.name.downcase
  else
    self.class.name.downcase
  end
end
shell_log_block() click to toggle source

A block that can be passed to execute to log the output

# File lib/derelict/utils/logger.rb, line 24
def shell_log_block
  Proc.new do |stdout, stderr|
    # Only stdout or stderr is populated, the other will be nil
    logger(:type => :external).info(stdout || stderr)
  end
end