Created
May 26, 2014 10:06
-
-
Save flavio/1f80e323326432cfdb0f to your computer and use it in GitHub Desktop.
Simple way to share a logger across a bunch of files without using a global variable
This file contains hidden or 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 'forwardable' | |
| require 'singleton' | |
| require 'logger' | |
| module Silvio | |
| class Logger | |
| include Singleton | |
| extend Forwardable | |
| def_delegators :@logger, :debug, :info, :warn, :error, :fatal | |
| def initialize | |
| @logger = ::Logger.new(STDOUT) | |
| @logger.level = ::Logger::DEBUG | |
| end | |
| end | |
| module Logging | |
| def logger | |
| Silvio::Logger.instance | |
| end | |
| end | |
| class Foo | |
| include Silvio::Logging | |
| def do_something | |
| logger.debug("going to do something cool") | |
| logger.error("shit happened") | |
| end | |
| end | |
| end | |
| logger = Silvio::Logger.instance | |
| logger.debug('foo') | |
| Silvio::Foo.new.do_something |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment