Skip to content

Instantly share code, notes, and snippets.

@blanchma
Created July 6, 2012 14:20
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 blanchma/3060430 to your computer and use it in GitHub Desktop.
Save blanchma/3060430 to your computer and use it in GitHub Desktop.
Captain Logger
# Example
# class Foo
# include CaptainLogger
# log_to :file => "foo.log"
# end
module CaptainLogger
def self.included(base)
class << base
attr_accessor :logger
end
base.send :extend, ClassMethods
end
module ClassMethods
def log_to(opts={})
opts = {:level => "info", :keep => "weekly"}.merge(opts)
#log_file = File.open("log/#{opts[:file]}", "w")
#log_file.sync = true
self.logger ||= ::Logger.new("log/#{opts[:file]}")
self.logger.formatter ||= ::Logger::Formatter.new
self.logger.formatter = proc do |severity, datetime, progname, msg|
"[#{severity}] #{datetime.strftime("%Y-%m-%d %H:%M")}: #{msg}\n"
end
self.logger.level = eval("::Logger::#{opts[:level].upcase}")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment