Sha256: 70b89c398fc8efbf106851e7b8c96d21733f1e12a49773583254f17f79ea3216

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

# This class handles logging for the caches and their stores.
class Cachetastic::Logger
  
  attr_accessor :options
  attr_accessor :cache_name
  
  def initialize(options, cache_name)
    self.options = options
    self.cache_name = cache_name
    self.options.each_pair do |n, opts|
      opts["level"] = (opts["level"] ||= "info").to_sym
    end
  end
  
  LOG_LEVELS = [:fatal, :error, :warn, :info, :debug]
  
  LOG_LEVELS.each do |level|
    define_method(level) do |*args|
      lm = "[CACHE] [#{level.to_s.upcase}]\t#{Time.now.strftime("%m/%d/%y %H:%m:%S")}"
      exs = []
      args.each do |arg|
        if arg.is_a?(Exception)
          exs << arg
          continue
        end
        lm << "\t" << arg.to_s 
      end
      exs.each do |ex|
        lm << "\n#{ex.message}\n" << ex.backtrace.join("\n")
      end
      self.options.each_pair do |n, opts|
        if LOG_LEVELS.index(opts["level"]) >= LOG_LEVELS.index(level)
          case opts["type"]
          when "file"
            File.open(opts["file"], "a") {|f| f.puts(lm)} 
          when "console"
            puts lm
          end
        end
      end
    end
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cachetastic-1.0.7 lib/cachetastic_logger.rb
cachetastic-1.0.8 lib/cachetastic_logger.rb
cachetastic-1.1.3 lib/cachetastic_logger.rb
cachetastic-1.1.8 lib/cachetastic_logger.rb