A logger : attr_accessor « Class « Ruby






A logger


class SimpleLogger
  attr_accessor :level

  ERROR = 1
  WARNING = 2
  INFO = 3

  def initialize
    @log = File.open("log.txt", "w")
    @level = WARNING
  end

  def error(msg)
    @log.puts(msg)
    @log.flush
  end

  def warning(msg)
    @log.puts(msg) if @level >= WARNING
    @log.flush
  end

  def info(msg)
    @log.puts(msg) if @level >= INFO
    @log.flush
  end
end
logger = SimpleLogger.new
logger.level = SimpleLogger::INFO
logger.info('Doing the first thing')
logger.info('Now doing the second thing')

 








Related examples in the same category

1.Creating Readable and Writable Attributes: use attr_accessor
2.attr_accessor add accessor to a class
3.Get new defined instance methods
4.Use attr_accessor to add a new attribute and set it