Class: Mongoid::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/mongoid/log_subscriber.rb

Overview

A Log subscriber to the moped queries

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#debug(prefix, operations, runtime) ⇒ Object

Log the provided operations.

Examples:

Delegates the operation to moped so it can log it.

subscriber.debug("MOPED", {}, 30)

Parameters:

  • prefix (String)

    The prefix for all operations in the log.

  • ops (Array)

    The operations.

  • runtime (String)

    The runtime in formatted ms.

Since:

  • 4.0.0



36
37
38
# File 'lib/mongoid/log_subscriber.rb', line 36

def debug(prefix, operations, runtime)
  Moped::Loggable.log_operations(prefix, operations, runtime)
end

#loggerLogger

Get the logger.

Examples:

Get the logger.

subscriber.logger

Returns:

  • (Logger)

    The logger.

Since:

  • 4.0.0



48
49
50
# File 'lib/mongoid/log_subscriber.rb', line 48

def logger
  Moped.logger
end

#query(event) ⇒ Object

Log the query operation on moped

Since:

  • 4.0.0



11
12
13
14
15
16
17
# File 'lib/mongoid/log_subscriber.rb', line 11

def query(event)
  return unless logger.debug?

  payload = event.payload
  runtime = ("%.4fms" % event.duration)
  debug(payload[:prefix], payload[:ops], runtime)
end

#query_cache(event) ⇒ Object

Since:

  • 4.0.0



19
20
21
22
23
24
25
# File 'lib/mongoid/log_subscriber.rb', line 19

def query_cache(event)
  return unless logger.debug?

  database, collection, selector = event.payload[:key]
  operation = "%-12s database=%s collection=%s selector=%s" % ["QUERY CACHE", database, collection, selector.inspect]
  logger.debug operation
end