Class: Mongo::Monitoring::CommandLogSubscriber

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/mongo/monitoring/command_log_subscriber.rb

Overview

Subscribes to command events and logs them.

Since:

  • 2.1.0

Constant Summary collapse

LOG_STRING_LIMIT =

Constant for the max number of characters to print when inspecting a query field.

Since:

  • 2.1.0

250

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initialize(options = {}) ⇒ CommandLogSubscriber

Create the new log subscriber.

Examples:

Create the log subscriber.

CommandLogSubscriber.new

Parameters:

  • options (Hash) (defaults to: {})

    The options.

Options Hash (options):

  • :logger (Logger)

    An optional custom logger.

Since:

  • 2.1.0



44
45
46
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 44

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns options The options.

Returns:

  • (Hash)

    options The options.

Since:

  • 2.1.0



26
27
28
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 26

def options
  @options
end

Instance Method Details

#failed(event) ⇒ Object

Handle the command failed event.

Examples:

Handle the event.

subscriber.failed(event)

Parameters:

  • event (CommandFailedEvent)

    The event.

Since:

  • 2.1.0



88
89
90
91
92
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 88

def failed(event)
  return unless logger.debug?

  log_debug("#{prefix(event)} | FAILED | #{event.message} | #{event.duration}s")
end

#started(event) ⇒ Object

Handle the command started event.

Examples:

Handle the event.

subscriber.started(event)

Parameters:

  • event (CommandStartedEvent)

    The event.

Since:

  • 2.1.0



56
57
58
59
60
61
62
63
64
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 56

def started(event)
  return unless logger.debug?

  _prefix = prefix(event,
                   connection_generation: event.connection_generation,
                   connection_id: event.connection_id,
                   server_connection_id: event.server_connection_id)
  log_debug("#{_prefix} | STARTED | #{format_command(event.command)}")
end

#succeeded(event) ⇒ Object

Handle the command succeeded event.

Examples:

Handle the event.

subscriber.succeeded(event)

Parameters:

  • event (CommandSucceededEvent)

    The event.

Since:

  • 2.1.0



74
75
76
77
78
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 74

def succeeded(event)
  return unless logger.debug?

  log_debug("#{prefix(event)} | SUCCEEDED | #{'%.3f' % event.duration}s")
end