elastic / elasticsearch-php

Official PHP client for Elasticsearch.
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
MIT License
5.25k stars 963 forks source link

Logs are not sent in Laravel queues when use async log #1374

Open genivaldosilva opened 8 months ago

genivaldosilva commented 8 months ago

Summary of problem or feature request

Logs from an asynchronous job in a Laravel queue are not sent to Elasticsearch until you shutdown the queue process.

This is because the php artisan queue:work does not close the process. Queue workers do not "reboot" the framework before processing each job.

Code snippet of problem

How to reproduce

  1. Create logging config (config/logging.php).
'elasticsearch' => [
    'driver' => 'custom',
    'via' => \App\Logging\CreateElasticsearchLogger::class,
    'level' => env('LOG_LEVEL', 'debug'),
],
  1. Create class \App\Logging\CreateElasticsearchLogger
    
    <?php

declare(strict_types=1);

namespace App\Logging;

use Elastic\Elasticsearch\ClientBuilder; use Monolog\Handler\ElasticsearchHandler; use Monolog\Logger;

class CreateElasticsearchLogger { /**

declare(strict_types=1);

namespace App\Jobs;

use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log;

class TestJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function handle(): void
{
    Log::info('The job has been executed.');
}

}

4. Add test routes - routes/web.php
```php
Route::get('test-log', function () {
    \Illuminate\Support\Facades\Log::info('The test log has been executed');
});

Route::get('test-log-job', function () {
    dispatch(new App\Jobs\TestJob());
});
  1. Config .env
    
    LOG_CHANNEL=elasticsearch
    LOG_LEVEL=debug

QUEUE_CONNECTION=redis

6. Run the queue:
```bash
php artisan queue:work redis
  1. Call GET /test-log You'll see: Screenshot from 2023-10-20 08-55-32

  2. Call GET /test-log-job You won't see, but when you kill the process:

Screenshot from 2023-10-20 08-45-00

kill 667

You will receive the log: Screenshot from 2023-10-20 08-59-47

System details

Screenshot from 2023-10-20 09-05-15

ezimuel commented 8 months ago

@genivaldosilva sorry for my late reply. I noticed that you are using Elasticsearch server version 7.17.13 and elasticsearch-php version 8.10. You should use the latest version 7.17.2 of elasticsearch-php since you are using servion 7 of the server. Please try and let me know, thanks.

genivaldosilva commented 8 months ago

Hi @ezimuel! Same problem.