When using ActiveRecord one can see all SQL queries executed when page is being loaded. How to achieve the same with Mongoid 5?
One of the best tools in a rails developers arsenal is the rails console, being extremely useful for brainstorming, debugging, and testing. Having it log active record queries directly to the console can improve readability and convenience over looking through the development logs to see what SQL queries have been run.
To write in the current log use the logger. (debug|info|warn|error|fatal|unknown) method from within a controller, model, or mailer: logger. debug "Person attributes hash: #{@person.
Rails uses six different log levels: debug, info, warn, error, fatal, and unknown. Each level defines how much information your application will log: Debug: diagnostic information for developers and system administrators, including database calls or inspecting object attributes.
Rails is configured to create separate log files for each of the three default environments: development, test and production. By default it puts these log files in the log/ directory of your project. So if you open up that folder you'll see a development. log and test.
You can customize Mongoid logging level as described in the documentation. You can also configure it in the main Rails application.
module MyApplication
class Application < Rails::Application
config.mongoid.logger = Logger.new($stdout, :warn)
end
end
If you want to reuse the same Rails logger, simply assign Rails.logger
(just make sure to assign it after the Rails.logger
is initialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With