Created
July 6, 2012 03:10
-
-
Save blanchma/3057853 to your computer and use it in GitHub Desktop.
Simple logger module
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 | |
# config_logger ::file => "foo.log" | |
# end | |
module CaptainLogger | |
def self.included(base) | |
class << base | |
attr_accessor :logger | |
end | |
base.send :extend, ClassMethods | |
end | |
module ClassMethods | |
def config_logger(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_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