Skip to content

Instantly share code, notes, and snippets.

@mdespuits
Last active December 15, 2015 06:29
Show Gist options
  • Save mdespuits/5217148 to your computer and use it in GitHub Desktop.
Save mdespuits/5217148 to your computer and use it in GitHub Desktop.
Singleton Logger wrapper including a progname
require_relative './example_logger'
class ExampleClass
def initialize
ExampleLogger.info("Initializing ExampleClass")
end
end
ExampleClass.new
require 'logger'
require 'singleton'
class ExampleLogger
include Singleton
attr_accessor :logger
def initialize
@logger = Logger.new(STDOUT)
@logger.formatter = proc do |sev, datetime, progname, msg|
"#{progname}, #{msg}\n"
end
end
def self.method_missing(method, *args, &blk)
if valid_method? method
instance.logger.progname = caller(1).first
instance.logger.send(method, *args, &blk)
else
super
end
end
def self.respond_to_missing?(method)
if valid_method? method
true
else
super
end
end
def self.valid_method?(method)
instance.logger.respond_to? method
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment