Laravel-mongodb: Logging not working?

Created on 25 Sep 2015  路  6Comments  路  Source: jenssegers/laravel-mongodb

I've used this library in L4 without any issues regarding logging.

        \DB::listen(function($sql, $bindings, $time) {
            \Log::info($sql);
        });

Would simply log all the queries to the log file. However, I'm running it on L5 right now and I don't see any of the mongo queries. I do see all my mysql queries though.
Any clue on what I could've missed?

Most helpful comment

To those using multiple database connections (mysql + mongodb in my case), you just need to enable the query log on the specific mongodb connection you want to use, i.e. in boot method of my app service provider:

DB::connection('mongodb')->enableQueryLog();
DB::connection('mongodb')->listen(function ($sql, $bindings, $time) {
    Log::info($sql . var_export($bindings, true));
});

All 6 comments

I wondered the same thing, and I dug into the source code. Unfortunately there does not seem to be any logging.

This is a disappointment for me. I am new to Mongo (and NoSQL in general), and being able to see what exactly is going into my queries would help immensely for learning it, and for understanding why they're returning unexpected results or errors.

This may be related.... I was attempting to use clockwork/debug bar to show all my database queries. I noticed that I wasn't getting anything in the listen callback, or was DB::getQueryLog() yielding anything. I found that the log was disabled and I just needed to enable it.

DB::enableQueryLog();

Both the callback and the query log array are working after this.

Hi @ohmance -- You're getting Mongo queries in the logfile now too? Or just MySQL queries logged? I ask because when I dug into the code for laravel-mongodb, it did not appear that there was any logging-related code at all. But I might have missed something.

Yes, I am only using a mongo db connection in this site. I assume that since Moloquent is a superclass of Eloquent, logging is probably being handled in the Eloquent code since many of the Moloquent methods build off of the Eloquent ones.

To those using multiple database connections (mysql + mongodb in my case), you just need to enable the query log on the specific mongodb connection you want to use, i.e. in boot method of my app service provider:

DB::connection('mongodb')->enableQueryLog();
DB::connection('mongodb')->listen(function ($sql, $bindings, $time) {
    Log::info($sql . var_export($bindings, true));
});
\DB::listen(function($sql) {
    \Log::info($sql->sql);
    \Log::info($sql->bindings);
    \Log::info($sql->time);
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomartailored picture tomartailored  路  3Comments

naveedyasin picture naveedyasin  路  3Comments

HassanIbrahim picture HassanIbrahim  路  3Comments

ricardofontanelli picture ricardofontanelli  路  3Comments

imrannazirbhat picture imrannazirbhat  路  3Comments