Module: MongoidColors::Colorizer

Defined in:
lib/mongoid-colors/colorizer.rb

Class Method Summary collapse

Class Method Details

.parse(msg) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongoid-colors/colorizer.rb', line 42

def self.parse(msg)
  case msg
  when /^MONGODB \((.*)ms\) (.*)\['(.*)'\]\.(.*)$/
    {:duration => $1, :database => $2, :collection => $3, :query => $4}
  when /^MONGODB (.*)\['(.*)'\]\.(.*)$/
    {:database => $1, :collection => $2, :query => $3}
  when /^ *MOPED: (\S+:\S+) (\S+) +database=(\S+)( collection=(\S+))? (.*[^)])( \((.*)ms\))?$/
    res = {:host => $1, :operation => $2, :database => $3, :collection => $5, :query => $6, :duration => $8}
    if res[:operation] == 'COMMAND'
      begin
        command = eval(res[:query])
        if command[:count]
          res[:operation]  = 'COUNT'
          res[:collection] = command.delete(:count)
          res[:query]      = command.inspect
        end
        if command[:findAndModify]
          res[:operation]  = 'FIND AND MODIFY'
          res[:collection] = command.delete(:findAndModify)
          res[:query]      = command.inspect
        end
      rescue Exception
      end
    end
    res
  when /which could negatively impact client-side performance/
    :ignore
  when /COMMAND.*getlasterror/
    :ignore
  end
end

.setupObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongoid-colors/colorizer.rb', line 8

def self.setup
  return unless Mongoid.logger

  old_formatter = Mongoid.logger.formatter
  Mongoid.logger.formatter = lambda do |severity, datetime, progname, msg|
    m = parse(msg)
    return if m == :ignore
    return old_formatter.call(severity, datetime, progname, msg) if m.nil?

    m[:query].gsub!(/BSON::ObjectId\('([^']+)'\)/, '0x\1')
    m[:duration] = m[:duration].split('.')[0] if m[:duration]

    line = "".green + "MongoDB ".white
    line << "(#{m[:duration]}ms) " if m[:duration]

    if m[:database]
      if m[:collection]
        line << "[#{m[:database]}::#{m[:collection]}] "
      else
        line << "[#{m[:database]}] "
      end
    end

    line << case m[:operation]
    when /(QUERY|COUNT)/ then "#{m[:operation]} ".colorize(:green)
    when /(INSERT|UPDATE|MODIFY|DELETE)/ then "#{m[:operation]} ".red
    else "#{m[:operation]} "
    end

    line << CodeRay.scan(m[:query], :ruby).term
    line << "\n"
  end
end