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 43
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 83
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 55
def started(event)
  if logger.debug?
    log_debug("#{prefix(event)} | 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 69
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 91
def format_command(args)
  begin
    truncating? ? truncate(args) : args.inspect
  rescue Exception
    '<Unable to inspect arguments>'
  end
end
prefix(event) click to toggle source
# File lib/mongo/monitoring/command_log_subscriber.rb, line 99
def prefix(event)
  "#{event.address.to_s} | #{event.database_name}.#{event.command_name}"
end
truncate(command) click to toggle source
# File lib/mongo/monitoring/command_log_subscriber.rb, line 103
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 107
def truncating?
  @truncating ||= (options[:truncate_logs] != false)
end