Created
March 11, 2009 23:11
-
-
Save jashmenn/77810 to your computer and use it in GitHub Desktop.
Module for easy logging in Ruby
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
# Module for easy logging in Ruby | |
# | |
# Example: | |
# | |
# class MyClass | |
# include HasLogger | |
# | |
# # ... | |
# def do_something | |
# logger.info("I just did something!") | |
# end | |
# end | |
module HasLogger | |
def self.included(mod) | |
mod.extend(ClassMethods) | |
mod.send :include, InstanceMethods | |
end | |
module ClassMethods | |
end | |
module InstanceMethods | |
def logger | |
@logger ||= begin | |
logger = Logger.new(@logdev || STDOUT) | |
logger.formatter = Logger::Formatter.new | |
logger.datetime_format = "%Y-%m-%d %H:%M:%S" | |
logger | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment