Class: MongoRecord::LogDevice
Overview
A destination for Ruby’s built-in Logger class. It writes log messages to a Mongo database collection. Each item in the collection consists of two fields (besides the _id): time
and msg
. time
is automatically generated when write
is called.
If we are running outside of the cloud, all log messages are echoed to $stderr.
The collection is capped, which means after the limit is reached old records are deleted when new ones are inserted. See the new method and the Mongo documentation for details.
Example:
logger = Logger.new(MongoRecord::LogDevice('my_log_name'))
The database connection defaults to the global $db. You can set the connection using MongoRecord::LogDevice.connection= and read it with MongoRecord::LogDevice.connection.
# Set the connection to something besides $db
MongoRecord::LogDevice.connection = connect('my-database')
Constant Summary collapse
- DEFAULT_CAP_SIZE =
(10 * 1024 * 1024)
- @@connection =
nil
Class Method Summary collapse
-
.connection ⇒ Object
Return the database connection.
-
.connection=(val) ⇒ Object
Set the database connection.
Instance Method Summary collapse
-
#close ⇒ Object
Close the log.
-
#initialize(name, options = {}) ⇒ LogDevice
constructor
name
is the name of the Mongo database collection that will hold all log messages. -
#write(str) ⇒ Object
Write a log message to the database.
Constructor Details
#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, = {}) @collection_name = name [:capped] = true [:size] ||= DEFAULT_CAP_SIZE [:size] = DEFAULT_CAP_SIZE if [: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, ) @console = [:stderr] end |
Class Method Details
.connection ⇒ Object
Return the database connection. The default value is $db
.
49 50 51 52 53 |
# File 'lib/mongo_record/log_device.rb', line 49 def connection conn = @@connection || $db raise "connection not defined" unless conn conn end |
.connection=(val) ⇒ Object
Set the database connection. If the connection is set to nil
, then $db
will be used.
57 58 59 |
# File 'lib/mongo_record/log_device.rb', line 57 def connection=(val) @@connection = val end |
Instance Method Details
#close ⇒ Object
Close the log. This method is a sham. Nothing happens. You may continue to use this LogDevice.
108 109 |
# File 'lib/mongo_record/log_device.rb', line 108 def close end |
#write(str) ⇒ Object
Write a log message to the database. We save the message and a timestamp.
101 102 103 104 |
# File 'lib/mongo_record/log_device.rb', line 101 def write(str) $stderr.puts str if @console self.class.connection.collection(@collection_name).insert({:time => Time.now, :msg => str}) end |