47

I have this little test script:

require 'mongo'

mongo_client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test')
mongo_client[:collection].insert_one({a: 1})

An this is the console output:

$ ruby test.rb
D, [2015-05-17T21:12:05.504986 #25257] DEBUG -- : MONGODB | Adding 127.0.0.1:27017 to the cluster. | runtime: 0.0212ms
D, [2015-05-17T21:12:05.531238 #25257] DEBUG -- : MONGODB | COMMAND | namespace=admin.$cmd selector={:ismaster=>1} flags=[] limit=-1 skip=0 project=nil | runtime: 24.5481ms
D, [2015-05-17T21:12:05.554532 #25257] DEBUG -- : MONGODB | COMMAND | namespace=test.$cmd selector={:insert=>"collection", :documents=>[{:a=>1, :_id=><BSON::ObjectId:0x21935660 data=5558e80553657262a9000000>}], :writeConcern=>{:w=>1}, :ordered=>true} flags=[] limit=-1 skip=0 project=nil | runtime: 21.1718ms

I want to disable those log messages, I don't want a dirty STDOUT. I didn't found any option for this in the ruby driver, and also I've tried to edit /etc/mongod.conf with these directives (but it didn't fix it):

verbose = false
diaglog = 0

Any idea? I don't know what else I can try!

Simon B.
  • 2,530
  • 24
  • 30
raulmarcosl
  • 793
  • 1
  • 6
  • 10
  • For future visitors, if you want beautiful colored logging similar to active record, you might want to look at [the mongo beautiful logger gem](https://github.com/redline-gh/mongo_beautiful_logger/) – Ibraheem Ahmed Jul 12 '20 at 23:41

3 Answers3

108

This logging is coming from the Ruby Mongo driver. The default logging level seems to be Logger::DEBUG. Change it to something higher to disable the debug output:

Mongo::Logger.logger.level = Logger::FATAL

To make the driver log to a logfile instead:

Mongo::Logger.logger       = Logger.new('mongo.log')
Mongo::Logger.logger.level = Logger::INFO

Note that if you're using the Mongoid ODM, then you may want to adjust logging there too:

Mongoid.logger       = Logger.new('mongoid.log')
Mongoid.logger.level = Logger::INFO 

For Rails + Mongoid in application.rb:

config.mongoid.logger = Logger.new(Rails.root + '/log/mongoid.log', :warn)

# ...or change the logging level without a new file destination
config.mongoid.logger.level = Logger::INFO
Casper
  • 33,403
  • 4
  • 84
  • 79
  • 1
    This didn't disable queries from being logged in my console. Adding Mongoid.logger.level = Logger::INFO did the trick. – Ekkstein Dec 01 '16 at 10:47
  • That didn't work for me on Mongoid 6. This is what worked when I put it in application.rb Mongoid::Config.logger = Logger.new("#{Rails.root}/log/mongo.log") – Prabhakar Sep 20 '17 at 02:57
  • For anyone coming here looking for an answer for Rails and Mongoid in general, this may be set in `mongoid.yml` as well: https://docs.mongodb.com/mongoid/master/tutorials/mongoid-installation/ – FeifanZ Dec 14 '17 at 18:11
  • 1
    The .logger = ... didn't work in Rails 5.1 (mongo 2.5, mongoid 6.2) but `config.mongoid.logger = Logger.new(Rails.root + '/tmp/mongo.log', :warn)` in an development.rb from https://docs.mongodb.com/mongoid/master/tutorials/mongoid-rails/ finally cleaned up my console! – Simon B. Jan 25 '18 at 21:03
  • 1
    Note - To just set the log level in Rails (without changing the destination): `config.mongoid.logger.level = Logger::INFO` – mltsy Jun 05 '18 at 21:28
  • Rails.root + '/something/something' -> /something/something in rails 6. Remove leading / from the string solved my issue. – Ekkstein Feb 24 '20 at 15:36
  • FYI `Mongo::Logger.logger.level = Logger::FATAL` also works for newer versions of MongoMapper – smtlaissezfaire Oct 24 '20 at 23:58
3

To disable the debug output for Ruby Mongo Driver(mongoid) we can add it specific environment file as

config.mongoid.logger.level = Logger::INFO

Pooja Mane
  • 477
  • 3
  • 5
0

The other answers didn't work for me.

This works for newer Ruby / Rails Versions:

config.mongoid.logger = Logger.new(Rails.root.join('log/mongoid.log'), level: :warn)
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115