class Mongo::Monitoring::CommandLogSubscriber

Subscribes to command events and logs them.

@since 2.1.0

Constants

LOG_STRING_LIMIT

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

@since 2.1.0

Attributes

options[R]

@return [ Hash ] options The options.

Public Class Methods

new(options = {}) click to toggle source

Create the new log subscriber.

@example Create the log subscriber.

CommandLogSubscriber.new

@param [ Hash ] options The options.

@option options [ Logger ] :logger An optional custom logger.

@since 2.1.0

# File lib/mongo/monitoring/command_log_subscriber.rb, line 45
def initialize(options = {})
  @options = options
end

Public Instance Methods

failed(event) click to toggle source

Handle the command failed event.

@example Handle the event.

subscriber.failed(event)

@param [ CommandFailedEvent ] event The event.

@since 2.1.0

# File lib/mongo/monitoring/command_log_subscriber.rb, line 90
def failed(event)
  if logger.debug?
    log_debug("#{prefix(event)} | FAILED | #{event.message} | #{event.duration}s")
  end
end
started(event) click to toggle source

Handle the command started event.

@example Handle the event.

subscriber.started(event)

@param [ CommandStartedEvent ] event The event.

@since 2.1.0

# File lib/mongo/monitoring/command_log_subscriber.rb, line 57
def started(event)
  if 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
end
succeeded(event) click to toggle source

Handle the command succeeded event.

@example Handle the event.

subscriber.succeeded(event)

@param [ CommandSucceededEvent ] event The event.

@since 2.1.0

# File lib/mongo/monitoring/command_log_subscriber.rb, line 76
def succeeded(event)
  if logger.debug?
    log_debug("#{prefix(event)} | SUCCEEDED | #{'%.3f' % event.duration}s")
  end
end

Private Instance Methods

format_command(args) click to toggle source
# File lib/mongo/monitoring/command_log_subscriber.rb, line 98
def format_command(args)
  begin
    truncating? ? truncate(args) : args.inspect
  rescue Exception
    '<Unable to inspect arguments>'
  end
end
prefix(event, connection_generation: nil, connection_id: nil, server_connection_id: nil ) click to toggle source
# File lib/mongo/monitoring/command_log_subscriber.rb, line 106
def prefix(event, connection_generation: nil, connection_id: nil,
  server_connection_id: nil
)
  extra = [connection_generation, connection_id].compact.join(':')
  if extra == ''
    extra = nil
  else
    extra = "conn:#{extra}"
  end
  if server_connection_id
    extra += " sconn:#{server_connection_id}"
  end
  "#{event.address.to_s} req:#{event.request_id}#{extra && " #{extra}"} | " +
    "#{event.database_name}.#{event.command_name}"
end
truncate(command) click to toggle source
# File lib/mongo/monitoring/command_log_subscriber.rb, line 122
def truncate(command)
  ((s = command.inspect).length > LOG_STRING_LIMIT) ? "#{s[0..LOG_STRING_LIMIT]}..." : s
end
truncating?() click to toggle source
# File lib/mongo/monitoring/command_log_subscriber.rb, line 126
def truncating?
  @truncating ||= (options[:truncate_logs] != false)
end