0

I would like to log laravel queries. I know that we can listen to laravel queries using the following code:

Event::listen('illuminate.query', function ($query, $bindings, $time, $name) {
    // Log queries
};

I would like to place above code in an artisan command so that I only listen to queries when this program is running, and not all the time. I wrote the following code.

<?php
// ... namespace and use statements

class QueryListener extends Command
{
    protected $signature = 'pots:query-listen';
    protected $description = 'Runs forever and logs all database queries.';

    public function __construct()
    {
        parent::__construct();
    }

    private function logDatabaseQueries()
    {
        Event::listen('illuminate.query', function ($query, $bindings, $time, $name) {
            Log::info(PHP_EOL.$query.PHP_EOL);
        });
    }

    public function handle()
    {
        $this->logDatabaseQueries();

        echo 'Listening for database queries: Type \'stop\' to stop execution.'.PHP_EOL;
        $stopExecution = false;

        while ($stopExecution === false) {
            $handle = fopen('php://stdin', 'r');
            $line = fgets($handle);

            if (strtolower(trim($line)) === 'stop') {
                fclose($handle);
                echo 'Aborting script.'.PHP_EOL;
                $stopExecution = true;
            }
        }
    }
}

My expectation was that as soon as I run this command using artisan, the logDatabaseQueries() method will start listening for events. However this is not working because I never receive any query events. Am I thinking in the right direction?

Rash
  • 7,677
  • 1
  • 53
  • 74
  • I am no PHP expert, but after much consideration and google searches, I think my question above does not make sense at all. My expectation was that when the console command runs, it should catch all query events generated by any request. But each new request will initialize a new application on a separate thread. The console command is probably running in a separate thread itself. So I don't think that these independent threads will share any data between them. Leaving this question open for PHP/Laravel Gurus to comment. – Rash Jun 07 '16 at 00:08

2 Answers2

5

You can listen to DB query using this:

DB::listen(function($query) { 

  // Log this
  \Log::info($query->sql); 


});
geckob
  • 7,680
  • 5
  • 30
  • 39
  • I checked it, its still not working. I think the problem is that when artisan runs a command, it maybe spawns a separate thread. Thus this thread will not receive events from another thread. As a matter of fact, if I google "listen for events on artisan console", I don't see any results, so that I why I am asking if I am thinking in the right direction or not? If I listen for events from within my system, then I can print all the logs fine. Its only when running from a console command, the events are not being received. – Rash Jun 06 '16 at 16:11
  • Just use $this->info(...); – Lachezar Todorov Aug 17 '20 at 14:26
0

For Those How Are Interested Listen for illuminate queries

// routes web.php
      \Event::listen('illuminate.query', function($sql) {
        var_dump($sql);
        \Log::info($sql);
    });

And For More And Advanced Lisen Here

Zymawy
  • 1,511
  • 16
  • 15