Sha256: fe2bbcaa2f04990d53ca96681a0fcbe3c2531109394c9ba7ae11ea07c4c6335a

Contents?: true

Size: 1.12 KB

Versions: 12

Compression:

Stored size: 1.12 KB

Contents

# This class handles logging for the caches and their adapters.
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

12 entries across 12 versions & 1 rubygems

Version Path
cachetastic-1.3.0 lib/cachetastic_logger.rb
cachetastic-1.4.1 lib/cachetastic_logger.rb
cachetastic-1.6.0 lib/cachetastic_logger.rb
cachetastic-1.2.7.1 lib/cachetastic_logger.rb
cachetastic-1.3.1 lib/cachetastic_logger.rb
cachetastic-1.4.0 lib/cachetastic_logger.rb
cachetastic-1.4.2 lib/cachetastic_logger.rb
cachetastic-1.5.0 lib/cachetastic_logger.rb
cachetastic-1.7.0 lib/cachetastic_logger.rb
cachetastic-1.7.2 lib/cachetastic_logger.rb
cachetastic-1.7.3 lib/cachetastic_logger.rb
cachetastic-1.7.4 lib/cachetastic_logger.rb