Viewing mongo queries in rails console

636 views
Skip to first unread message

Chris

unread,
Aug 11, 2011, 3:20:00 AM8/11/11
to MongoMapper
With ActiveRecord I could run the following so that when I'm in the
rails console I can see the corresponding MySQL query for every
command:

ActiveRecord::Base.logger = Logger.new(STDOUT)

Is there an equivalent line to see the mongo query for every
MongoMapper command in the rails console?

John Nunemaker

unread,
Aug 14, 2011, 8:15:28 PM8/14/11
to mongo...@googlegroups.com
The equivalent is at the driver level. When you create a new connection, simply pass a logger instance.

MongoMapper.connection = Mongo::Connection.new('localhost', 27017, :logger => Rails.logger)

> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en

Jon Kern

unread,
Aug 14, 2011, 9:13:32 PM8/14/11
to mongo...@googlegroups.com
and if you just want to watch it locally, you can use some handy
commands at the mongo console to turn it on and off.

>db.setProfilingLevel(2,20)

jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA


John Nunemaker said the following on 8/14/11 8:15 PM:

Chris

unread,
Aug 15, 2011, 8:57:30 PM8/15/11
to MongoMapper
Hey John,

I've set that line and the mongo logs show up in my development.log,
but not inline in the rails console when I run User.first, for
example. Do you know of a way to get mongo logging to show up in the
rails console?

Thanks!

Ben Marini

unread,
Aug 15, 2011, 11:31:10 PM8/15/11
to mongo...@googlegroups.com
This'll give you the output you want:
https://github.com/bmarini/mongo-rails-instrumentation/tree/ar-style-logging

In your Gemfile:
gem 'mongo-rails-instrumentation', :git => "https://github.com/bmarini/mongo-rails-instrumentation", :branch => "ar-style-logging"


Still waiting on my pull request to make it to the official gem, but I've been using this in my projects with no issues.

Ben Marini

unread,
Aug 15, 2011, 11:34:17 PM8/15/11
to MongoMapper
Actually this doesn't give you logging in the console...but it does
colorize your mongo queries in the rails logs! :)

On Aug 15, 8:31 pm, Ben Marini <bmar...@gmail.com> wrote:
> This'll give you the output you want:https://github.com/bmarini/mongo-rails-instrumentation/tree/ar-style-...

John Nunemaker

unread,
Aug 16, 2011, 10:48:38 AM8/16/11
to mongo...@googlegroups.com
Mongo::Connection.new('localhost', 27017, :logger => Logger.new(STDOUT)
Reply all
Reply to author
Forward
0 new messages