Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pail regenerate session when DB::listen is used #48

Open
lripardo opened this issue Dec 15, 2024 · 1 comment
Open

Pail regenerate session when DB::listen is used #48

lripardo opened this issue Dec 15, 2024 · 1 comment

Comments

@lripardo
Copy link

lripardo commented Dec 15, 2024

Laravel Package Version

1.2.1

Laravel Version

11.33.2

PHP Version

8.3.13

Database Driver & Version

No response

Description

I would like to use Pail to monitor in real time the queries that are being executed in my web system. Following documentation in: https://laravel.com/docs/11.x/database#listening-for-query-events

<?php

namespace App\Providers;

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        //
    }

    public function boot(): void
    {
        if ($this->app->isLocal()) {
            Log::debug('Starting DB Logging...');

            DB::listen(function (QueryExecuted $query) {
                Log::debug('Query: {query} | Time: {time}.', [
                    'query' => $query->toRawSql(),
                    'time' => $query->time,
                ]);
            });
        }
    }
}

The session id is changed during any request and the web server sometimes returns an HTTP No Content,
Investigating the case in the code of Pail version 1.2.1, on Handler.php file lines 95 and 96 we have:
https://github.com/laravel/pail/blob/v1.2.1/src/Handler.php#L95
https://github.com/laravel/pail/blob/v1.2.1/src/Handler.php#L96

Changing

default => [
    'type' => 'http',
    'method' => request()->method(),
    'path' => request()->path(),
    'auth_id' => Auth::id(),
    'auth_email' => Auth::user() instanceof User ? Auth::user()->email : null, // @phpstan-ignore property.notFound
]

By

default => [
    'type' => 'http',
    'method' => request()->method(),
    'path' => request()->path(),
    'auth_id' => '',
    'auth_email' => '', // @phpstan-ignore property.notFound
]

The problem is gone.

According to our friend Gonçalo in Issue 40 (#40) these lines make new queries in the database, infinitely activating the handler DB ::listen.

If this is the correct way to monitor queries in real time using Pail, I believe this should be a bug that should be investigated.

Steps To Reproduce

Any new Laravel projects with use DB::listen according with documentation and have Log:: inside to print queries.

Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants