Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shawndibble's avatar

Have enableQueryLog show me where the query exists.

So I am using enableQueryLog and it is showing me the queries that are being executed and how long they take. However I am wondering if there is a way to see the class and line number that executes said query. How do I see this information.

Side note, I am using the illuminate capsule/manager to grab this info so I can use querybuilder and eloquent in a non Laravel application.

class DatabaseConnection
{
    use CallStatically;

    public static function boot()
    {
        $capsule = new Manager();

        foreach(self::connectionSettings() as $name => $settings) {
            $capsule->addConnection($settings, $name);
        }

        $capsule->getConnection()->enableQueryLog();
        $capsule->setAsGlobal();

        $capsule->getConnection()->setEventDispatcher(new Dispatcher());
        $capsule->getConnection()->listen(fn ($query) => self::logQueries($query));

        $capsule->bootEloquent();
    }

    private function connectionSettings(): array
    {
        global $settings;

        return [
            "default" => [
                "driver" => "mysql",
                "host" => $settings['DB_HOST'],
                "database" => $settings['DB_NAME'],
                "username" => $settings['DB_USERNAME'],
                "password" => $settings['DB_PASSWORD']
            ]
        ];
    }

    public function logQueries(QueryExecuted $query)
    {
        dump($query);
        // $query->sql
        // $query->bindings
        // $query->time
    }
}
0 likes
4 replies
Sinnbeck's avatar

Why not just use debugbar or clockwork? Both shows it automatically

shawndibble's avatar

@Sinnbeck Our hosting team wants this data logged so they can review and evaluate long running queries. Being able to know what file/line made the call would help them.

Sinnbeck's avatar

@shawndibble ah ok. So check the source of debugbar and see how they grab it? I think that will be faster than trying to figure it out yourself

Please or to participate in this conversation.