Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.