Skip to content

Instantly share code, notes, and snippets.

@acfatah
Forked from cheeyeo/singleton_logger.rb
Created December 19, 2022 03:21
Show Gist options
  • Save acfatah/6738689b093ca3628872c3c2892191fc to your computer and use it in GitHub Desktop.
Save acfatah/6738689b093ca3628872c3c2892191fc 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