Method: MongoRecord::LogDevice#initialize

Defined in:
lib/mongo_record/log_device.rb

#initialize(name, options = {}) ⇒ LogDevice

name is the name of the Mongo database collection that will hold all log messages. options is a hash that may have the following entries:

:size - Optional. The max size of the collection, in bytes. If it is nil or negative then DEFAULT_CAP_SIZE is used.

:max - Optional. Specifies the maximum number of log records, after which the oldest items are deleted as new ones are inserted.

:stderr - Optional. If not nil then all log messages will be copied to $stderr.

Note: a non-nil :max requires a :size value. The collection will never grow above :size. If you leave :size nil then it will be DEFAULT_CAP_SIZE.

Note: once a capped collection has been created, you can’t redefine the size or max falues for that collection. To do so, you must drop and recreate (or let a LogDevice object recreate) the collection.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mongo_record/log_device.rb', line 83

def initialize(name, options = {})
  @collection_name = name
  options[:capped] = true
  options[:size] ||= DEFAULT_CAP_SIZE
  options[:size] = DEFAULT_CAP_SIZE if options[:size] <= 0

  # It's OK to call createCollection if the collection already exists.
  # Size and max won't change, though.
  #
  # Note we can't use the name "create_collection" because a DB JSObject
  # does not have normal keys and returns collection objects as the
  # value of all unknown names.
  self.class.connection.create_collection(@collection_name, options)

  @console = options[:stderr]
end