Skip to content

Instantly share code, notes, and snippets.

@adamcrown
Created June 4, 2014 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrown/2b3390e165f0a0f51347 to your computer and use it in GitHub Desktop.
Save adamcrown/2b3390e165f0a0f51347 to your computer and use it in GitHub Desktop.
Easy global log for Ruby
# 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