module MotherBrain::Logging

Public Class Methods

add_argument_header() click to toggle source

Places the arguments given surrounded by whitespace at the top of the logfile. This makes it easier to scan logs for the beginning of a command.

# File lib/mb/logging.rb, line 14
def add_argument_header
  return if dev == STDOUT

  logger.unknown nil
  logger.unknown nil
  logger.unknown ARGV.join(" ")
  logger.unknown nil
end
dev() click to toggle source

Returns the currrent logging device

@return [IO, nil]

# File lib/mb/logging.rb, line 26
def dev
  logdev.dev
end
filename() click to toggle source

Returns the filename of the current logger

@return [String, nil]

# File lib/mb/logging.rb, line 33
def filename
  logdev.filename
end
logger() click to toggle source

@return [Logger]

# File lib/mb/logging.rb, line 38
def logger
  @logger ||= setup
end
reset() click to toggle source

@return [nil]

# File lib/mb/logging.rb, line 43
def reset
  @logger = nil
  @preserved_options = nil
end
set_logger(obj) click to toggle source

@param [Logger, nil] obj

@return [Logger]

# File lib/mb/logging.rb, line 92
def set_logger(obj)
  @logger = (obj.nil? ? Logger.new('/dev/null') : obj)
end
setup(options = {}) click to toggle source

@option options [String, Integer] :level (INFO) @option options [String, IO] :location

@return [Logger]

# File lib/mb/logging.rb, line 52
def setup(options = {})
  options = options.keep_if { |key, value| value }
  options = preserve(options).reverse_merge(
    level: INFO,
    location: FileSystem.logs.join('application.log')
  )

  level    = options[:level].is_a?(String) ? options[:level].upcase : options[:level]
  location = options[:location]

  if %w[DEBUG INFO WARN ERROR FATAL].include?(level)
    level = const_get(level)
  end

  if %w[STDERR STDOUT].include?(location)
    location = location.constantize
  end

  unless [STDERR, STDOUT].include?(location)
    setup_logdir(location)
  end

  if jruby? && location.is_a?(Pathname)
    location = location.to_s
  end

  @logger = Ridley::Logging::Logger.new(location).tap do |log|
    log.level = level
    log.formatter = BasicFormat.new
  end

  Ridley.logger = @logger
  Celluloid.logger = ENV["DEBUG_CELLULOID"] ? @logger : nil

  @logger
end

Private Class Methods

logdev() click to toggle source
# File lib/mb/logging.rb, line 98
def logdev
  logger.instance_variable_get(:@logdev)
end
preserve(options) click to toggle source

Stores and returns an updated hash, so that setup can be called multiple times

@param [Hash] options

@return [Hash]

# File lib/mb/logging.rb, line 108
def preserve(options)
  @preserved_options ||= Hash.new
  @preserved_options.reverse_merge! options
end
setup_logdir(location) click to toggle source
# File lib/mb/logging.rb, line 113
def setup_logdir(location)
  FileUtils.mkdir_p(File.dirname(location), mode: 0755)
end

Public Instance Methods

log()
Alias for: logger
log_exception(ex) click to toggle source

Log an exception and it’s backtrace to FATAL

@param [Exception] ex

# File lib/mb/logging.rb, line 127
def log_exception(ex)
  ex = ex.respond_to?(:cause) ? ex.cause : ex

  log.fatal { "#{ex.class}: #{ex}" }
  log.fatal { ex.backtrace.join("\n") } unless ex.backtrace.nil?
end
logger() click to toggle source

@return [Logger]

# File lib/mb/logging.rb, line 119
def logger
  MB::Logging.logger
end
Also aliased as: log