123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Monitoring::CommandLogSubscriber

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/mongo/monitoring/command_log_subscriber.rb

Overview

Subscribes to command events and logs them.

Since:

  • 2.1.0

Constant Summary

::Mongo::Loggable - Included

PREFIX

Class Method Summary

Instance Attribute Summary

Instance Method Summary

::Mongo::Loggable - Included

#log_debug

Convenience method to log debug messages with the standard prefix.

#log_error

Convenience method to log error messages with the standard prefix.

#log_fatal

Convenience method to log fatal messages with the standard prefix.

#log_info

Convenience method to log info messages with the standard prefix.

#log_warn

Convenience method to log warn messages with the standard prefix.

#logger

Get the logger instance.

#_mongo_log_prefix, #format_message

Constructor Details

.new(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

[ GitHub ]

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

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

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)

    options The options.

Since:

  • 2.1.0

[ GitHub ]

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

attr_reader :options

#truncating?Boolean (readonly, private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 127

def truncating?
  @truncating ||= (options[:truncate_logs] != false)
end

Instance Method Details

#failed(event)

Handle the command failed event.

Examples:

Handle the event.

subscriber.failed(event)

Parameters:

  • event (CommandFailedEvent)

    The event.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 91

def failed(event)
  if logger.debug?
    log_debug("#{prefix(event)} | FAILED | #{event.message} | #{event.duration}s")
  end
end

#format_command(args) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 99

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) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 107

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

#started(event)

Handle the command started event.

Examples:

Handle the event.

subscriber.started(event)

Parameters:

  • event (CommandStartedEvent)

    The event.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 58

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)

Handle the command succeeded event.

Examples:

Handle the event.

subscriber.succeeded(event)

Parameters:

  • event (CommandSucceededEvent)

    The event.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 77

def succeeded(event)
  if logger.debug?
    log_debug("#{prefix(event)} | SUCCEEDED | #{'%.3f' % event.duration}s")
  end
end

#truncate(command) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring/command_log_subscriber.rb', line 123

def truncate(command)
  ((s = command.inspect).length > LOG_STRING_LIMIT) ? "#{s[0..LOG_STRING_LIMIT]}..." : s
end