Sha256: eefc6a1345edd3c773e732c08e5e6ed8cf642aebd04a2bbab3ffb9434e7f9f87

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/log_switch/version')
require "logger"

# LogSwitch allows for extending a class/module with a logger and, most
# importantly, allows for turning off logging programmatically.  See the
# +README.rdoc+ for more info.
module LogSwitch

  # Use to turn logging on or off.
  attr_writer :log

  # Tells whether logging is turned on or not.
  def log?
    @log != false
  end

  # Set this to the Logger you want to use.
  attr_writer :logger

  # Defaults to a +Logger+ writing to STDOUT.
  def logger
    @logger ||= ::Logger.new STDOUT
  end

  # Set the log level so you don't have to pass it in on your call.
  attr_writer :log_level

  # @return [Symbol] The current default log level.  Starts off as :debug.
  def log_level
    @log_level ||= :debug
  end

  # Logs a message using the level provided.  If no level provided, use
  # +@log_level+.
  #
  # @param [String] message The message to log.
  # @param [Symbol] level The log level to send to your Logger.
  def log(message, level=log_level)
    message.each_line { |line| logger.send level, line.chomp if log? }
  end

  # Sets back to defaults.
  def reset_config!
    self.log = true
    self.logger = ::Logger.new STDOUT
    self.log_level = :debug
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
log_switch-0.1.4 lib/log_switch.rb
log_switch-0.1.3 lib/log_switch.rb