Created
November 2, 2010 11:31
-
-
Save cheeyeo/659510 to your computer and use it in GitHub Desktop.
Simple logger using Ruby's built in Singleton module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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