Created
July 6, 2012 14:20
-
-
Save blanchma/3060430 to your computer and use it in GitHub Desktop.
Captain Logger
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
# 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