Skip to content

Instantly share code, notes, and snippets.

@gakshay
Forked from cheeyeo/singleton_logger.rb
Created May 22, 2012 21:48
Show Gist options
  • Save gakshay/2771857 to your computer and use it in GitHub Desktop.
Save gakshay/2771857 to your computer and use it in GitHub Desktop.
Simple logger using Ruby's built in Singleton module
require 'singleton'
class SimpleLogger
include Singleton
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.instance
logger.level = SimpleLogger::INFO
logger.info('Doing the first thing')
logger.info('Doing the second thing')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment