How to log every query with mgo in standard output? I set logger but it shows a lot of information without actual queries.
An answer from Gustavo Niemeyer, author of mgo : http://grokbase.com/t/gg/mgo-users/152571ky82/how-to-show-query-log#20150209zwzki7mxjfigdzuqp245wskkl4
There are two ways you can handle this issue:
By enabling MongoDB logging
This is independent from the driver (mgo in this case), and can be enabled in the shell or by running the respective command via mgo:
http://docs.mongodb.org/manual/reference/method/db.setProfilingLevel/
By enabling mgo logging
You can do this by creating a Logger via the standard package's log.New function and providing it to mgo's SetLogger function:
http://golang.org/pkg/log/#New, http://gopkg.in/mgo.v2#SetLogger
Use mgo.SetDebug to increase the verbosity:
So if you already have the Logger set, enable the debug mode.
I couldn't get mgo to log queries using SetLogger and SetDebug. Instead, I solved this by marshaling to json string and then printing:
q = bson.M{}
jsonString, _ := json.Marshal(q)
fmt.Printf("mgo query: %s
", jsonString)
You can also copy/paste the output of this into a standard mongo client if you need to debug a query.