mattstauffer / Torch

Examples of using each Illuminate component in non-Laravel applications
MIT License
1.85k stars 210 forks source link

How to write to the log using DB::listen() ? #197

Open asundust opened 3 years ago

asundust commented 3 years ago

How to write to the log using DB::listen() ?

lwohlhart commented 2 years ago

Hey, I just ran into the same question:

If you have registered an EventDispatcher on the Capsule Manager you can register an event listener:

$eventDispatcher = new Dispatcher(new Container);
$capsule->setEventDispatcher($eventDispatcher);

# or alternatively if the capsule is defined already
$eventDispatcher = $capsule->getEventDispatcher();

$eventDispatcher->listen(\Illuminate\Database\Events\QueryExecuted::class,
    function (\Illuminate\Database\Events\QueryExecuted $query)  {
        // $query->sql;
        // $query->bindings;
        // $query->time;
    }
);

hth