Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log all request to rails console in Mongoid 5?

When using ActiveRecord one can see all SQL queries executed when page is being loaded. How to achieve the same with Mongoid 5?

like image 395
Maxim Filippov Avatar asked Oct 22 '15 12:10

Maxim Filippov


People also ask

Can you console log in rails?

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.

How do I use logger in Ruby on Rails?

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.

What is the log to check for errors in Rails?

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.

How does Rails logger work?

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.


1 Answers

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.

like image 130
Simone Carletti Avatar answered Nov 09 '22 23:11

Simone Carletti