Skip to content

Instantly share code, notes, and snippets.

@cosenmarco
Created October 23, 2011 15:52
Show Gist options
  • Save cosenmarco/1307502 to your computer and use it in GitHub Desktop.
Save cosenmarco/1307502 to your computer and use it in GitHub Desktop.
A simple singleton logger
# Author: Marco Cosentino <cosentino.ma _AT_ gmail.com>
require 'singleton'
require 'logger'
class SLogger
include Singleton
attr_accessor :log
["fatal","error","warn","info","debug"].each do |msg|
code = "def SLogger.#{msg}(*args) SLogger.instance.log.#{msg}(args) end"
SLogger.class_eval code
end
def initialize
@log = Logger.new(STDERR)
@log.level = Logger::WARN
end
def self.logfile=(file)
SLogger.instance.log = Logger.new(file)
end
def self.loglevel=(level)
SLogger.instance.log.level = level
end
protected
def finalize
@log.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment