Created
October 23, 2011 15:52
-
-
Save cosenmarco/1307502 to your computer and use it in GitHub Desktop.
A simple singleton logger
This file contains 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
# 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