Logging Configuration
Overview
In this guide, you can learn how to configure logging in your Mongoid application. When configuring logging, note that Mongoid provides a model layer on top of the Ruby driver, and the driver dispatches data operations to MongoDB. Therefore, some logging output in an application that uses Mongoid comes from Mongoid itself, and some comes from the driver.
Driver Logger
The Mongoid client is a Ruby driver client instance, so the logger of a Mongoid client is the Ruby driver logger, not the Mongoid logger. The following code creates a Mongoid client logger:
Mongoid.client(:default).logger
Depending on your application framework and how you configure Mongoid and the Ruby driver, they may use the same logger instance or different instances, potentially with different configurations.
Ruby on Rails Configuration
When used in a Ruby on Rails application, Mongoid by default inherits the logger and the log level from Rails. Mongoid sets the driver's logger to the same logger instance:
Rails.logger === Mongoid.logger # => true Mongoid.logger === Mongo::Logger.logger # => true
To change the log level, use the standard Rails configuration.
Place the following block in one of your environment configuration
files, such as config/environments/production.rb
:
Rails.application.configure do config.log_level = :debug end
Note
The log_level
Mongoid configuration option is not used when
Mongoid operates in a Rails application, because Mongoid inherits
Rails' log level.
To configure either the Mongoid or driver logger differently from the Rails logger, use an initializer as shown in the following code:
Rails.application.configure do config.after_initialize do # Change Mongoid log destination and level Mongoid.logger = Logger.new(STDERR).tap do |logger| logger.level = Logger::DEBUG end # Change driver log destination and level Mongo::Logger.logger = Logger.new(STDERR).tap do |logger| logger.level = Logger::DEBUG end end end
Note
There is no provision in the Ruby standard library Logger
to return the log device, such as the IO
object, that a logger is
using.
To make, for example, Mongoid or the Ruby driver log to the
standard Rails log file (log/development.log
) with a
different level from standard Rails logger (Rails.logger
), you
must open the file separately and pass the resulting IO
object to
the Logger
constructor.
Because Mongoid sets its own logger and the driver's logger to the same instance as the Rails logger, modifying any of the instances affects all the loggers. For example, the following code changes the log level for all three loggers:
Mongoid::Logger.logger.level = Logger::DEBUG
Standalone Configuration
When not loaded in a Ruby on Rails application, Mongoid respects the
log_level
top-level configuration option:
development: clients: default: ... options: log_level: :debug
You can also configure the log level in-line:
Mongoid.configure do |config| config.log_level = :debug end
The default log destination is standard error. To change the log destination, create a new logger instance as shown in the following code:
Mongoid.logger = Logger.new(STDERR).tap do |logger| logger.level = Logger::DEBUG end
To change the Ruby driver log level or destination, add the following block to your application file:
Mongo::Logger.logger = Logger.new(STDERR).tap do |logger| logger.level = Logger::DEBUG end
Note
Mongoid does not change the driver's logger when running in standalone mode.