Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log all queries that mgo fire in the application

Tags:

go

mgo

How to log every query with mgo in standard output? I set logger but it shows a lot of information without actual queries.

like image 489
Plastic Rabbit Avatar asked Dec 05 '25 02:12

Plastic Rabbit


1 Answers

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:

  1. 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/

  2. 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:

http://gopkg.in/mgo.v2#SetDebug

So if you already have the Logger set, enable the debug mode.

like image 54
HectorJ Avatar answered Dec 06 '25 17:12

HectorJ