module Cabin::Mixins::Logger

This module implements methods that act somewhat like Ruby’s Logger class It is included in Cabin::Channel

Constants

BACKTRACE_RE
LEVELS

Attributes

level[RW]

Public Class Methods

included(klass) click to toggle source
# File lib/cabin/mixins/logger.rb, line 7
def self.included(klass)
  klass.condition do |event, subscription|
    if subscription.nil?
      true
    else
      LEVELS[(subscription.options[:level] || :debug)] >= LEVELS[event[:level]].to_i
    end
  end
end

Public Instance Methods

level=(value) click to toggle source
# File lib/cabin/mixins/logger.rb, line 28
def level=(value)
  if value.respond_to?(:downcase)
    @level = value.downcase.to_sym
  else
    @level = value.to_sym
  end
end
log(message, data={}) click to toggle source
# File lib/cabin/mixins/logger.rb, line 93
def log(message, data={})
  _log(message, data)
end

Private Instance Methods

_log(message, data={}) click to toggle source
# File lib/cabin/mixins/logger.rb, line 97
def _log(message, data={})
  case message
    when Hash
      data.merge!(message)
    when Exception
      # message is an exception
      data[:message] = message.to_s
      data[:exception] = message.class
      data[:backtrace] = message.backtrace
    else
      data = { :message => message }.merge(data)
  end

  # Add extra debugging bits (file, line, method) if level is debug.
  debugharder(caller[2], data) if @level == :debug

  publish(data)
end
debugharder(callinfo, data) click to toggle source

This method is used to pull useful information about the caller of the logging method such as the caller’s file, method, and line number.

# File lib/cabin/mixins/logger.rb, line 118
def debugharder(callinfo, data)
  m = BACKTRACE_RE.match(callinfo)
  return unless m
  path, line, method = m[1..3]
  whence = $:.detect { |p| path.start_with?(p) }
  if whence
    # Remove the RUBYLIB path portion of the full file name
    file = path[whence.length + 1..-1]
  else
    # We get here if the path is not in $:
    file = path
  end
  
  data[:file] = file
  data[:line] = line
  data[:method] = method
end
log_with_level(level, message, data={}) click to toggle source
# File lib/cabin/mixins/logger.rb, line 87
def log_with_level(level, message, data={})
  # Invoke 'info?' etc to ask if we should act.
  data[:level] = level
  _log(message, data)
end