Last active
December 15, 2015 06:29
-
-
Save mdespuits/5217148 to your computer and use it in GitHub Desktop.
Singleton Logger wrapper including a progname
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
require_relative './example_logger' | |
class ExampleClass | |
def initialize | |
ExampleLogger.info("Initializing ExampleClass") | |
end | |
end | |
ExampleClass.new |
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
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