Module: MongoMapper::Plugins::QueryLogger::ClassMethods

Defined in:
lib/mongo_mapper/plugins/query_logger.rb

Instance Method Summary collapse

Instance Method Details

#count(options = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/mongo_mapper/plugins/query_logger.rb', line 22

def count(options={})
  criteria, query_options = to_query(options)
  log(query_options.inspect, 'count') do 
    super
  end
end

#find_many(options = {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/mongo_mapper/plugins/query_logger.rb', line 15

def find_many(options={})
  criteria, query_options = to_query(options)
  log(query_options.inspect, 'find_many') do
    super
  end
end

#find_one(options = {}) ⇒ Object



8
9
10
11
12
13
# File 'lib/mongo_mapper/plugins/query_logger.rb', line 8

def find_one(options={})
  criteria, query_options = to_query(options)
  log(query_options.inspect, 'find_one') do
    super
  end
end

#format_log_entry(message, dump = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mongo_mapper/plugins/query_logger.rb', line 48

def format_log_entry(message, dump = nil)
  if ActiveRecord::Base.colorize_logging
    message_color, dump_color = "4;36;1", "0;1"

    log_entry = "  \e[#{message_color}m#{message}\e[0m   "
    log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump
    log_entry
  else
    "%s  %s" % [message, dump]
  end
end

#log(query, name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mongo_mapper/plugins/query_logger.rb', line 36

def log(query, name)
  if block_given?
    result = nil
    ms = Benchmark.ms { result = yield }
    log_info(query, name, ms)
    result
  else
    log_info(query, name, 0)
    nil
  end
end

#log_info(query, name, ms) ⇒ Object



29
30
31
32
33
34
# File 'lib/mongo_mapper/plugins/query_logger.rb', line 29

def log_info(query, name, ms)
  if self.logger && self.logger.info?
    name = '%s (%.1fms)' % [name || 'query', ms]
    self.logger.info(format_log_entry(name, query))
  end
end