laravel / octane

Supercharge your Laravel application's performance.
https://laravel.com/docs/octane
MIT License
3.71k stars 284 forks source link

Context gets cleared before logging #907

Closed Lotuashvili closed 3 weeks ago

Lotuashvili commented 3 weeks ago

Octane Version

2.3.7

Laravel Version

11.10

PHP Version

8.3

What server type are you using?

Swoole

Server Version

AWS

Database Driver & Version

MySQL 8

Description

While using Laravel Octane and Context variables, it gets cleared before the log entry is created. I can successfully access my data by Context::get('name') in the app, but it's not being included in logs.

I tried to run this application without Octane (php artisan serve), and the context is working as expected.

Steps To Reproduce

Create a test route in web.php: (As shown here)

use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\Log;

Route::get('test', function () {
    Context::add('global_context', 'value');

    Log::info('test', ['local_data' => 'local']);

    return response('test');
});

In laravel.log file, you will get only local_data.

[2024-06-15 10:45:51] local.INFO: test {"local_data":true}
sy-records commented 3 weeks ago

🤔️ Maybe in octane you need to use Log::shareContext(['global_context' => 'value']);

Lotuashvili commented 3 weeks ago

@sy-records Yeah that works!

driesvints commented 3 weeks ago

Thanks @sy-records

Lotuashvili commented 3 weeks ago

@driesvints I didn't close this issue on purpose, because that is a workaround and not a solution/fix. In this case I really needed it for logging purpose, but I will also need Context as a data storing mechanism for different parts of application. It's still going to be cleared before getting to logs. So I would really suggest to debug why it's being cleared by Octane.

driesvints commented 3 weeks ago

@Lotuashvili ah gotcha. I thought you were happy with the workaround. I'll try to see if this one is solvable.

timacdonald commented 3 weeks ago

Thanks for reporting this one.

I've opened a PR to fix this issue: https://github.com/laravel/octane/pull/910