Created
June 4, 2014 15:45
-
-
Save adamcrown/2b3390e165f0a0f51347 to your computer and use it in GitHub Desktop.
Easy global log for Ruby
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
# Usage: | |
# Log.info 'done' | |
# Log.debug @var.inspect | |
# Log.error 'OH NOES!!!1' | |
# etc. | |
module Log | |
def self.method_missing(meth, *args, &block) | |
if logger.respond_to? meth | |
logger.send meth, *args, &block | |
else | |
super | |
end | |
end | |
private | |
def self.logger | |
@logger ||= Logger.new(log_file).tap do |logger| | |
logger.formatter = Logger::Formatter.new | |
end | |
end | |
def self.log_file | |
file = File.expand_path('../../log/output.log', __FILE__) | |
dir = File.dirname(file) | |
Dir.mkdir(dir) unless Dir.exists?(dir) | |
file | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment