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

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

Open
genivaldosilva opened this issue Oct 20, 2023 · 2 comments
Open

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

genivaldosilva opened this issue Oct 20, 2023 · 2 comments

Comments

@genivaldosilva
Copy link

genivaldosilva commented Oct 20, 2023

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
{
    /**
     * Create a custom Monolog instance.
     */
    public function __invoke(array $config): Logger
    {
        $hosts = ['host-to-elastic:9200'];
        $username = 'elastic';
        $password = 'password';
        $index = 'your-index-name';
        $type = '_doc';

        $client = ClientBuilder::create()
            ->setHosts($hosts)
            ->setBasicAuthentication($username, $password)
            ->setHttpClient(new \Symfony\Component\HttpClient\Psr18Client)
            ->build()
            ->setAsync(true);

        $options = [
            'index' => $index,
            'type' => $type,
            'ignore_error' => true,
        ];

        $handler = new ElasticsearchHandler($client, $options, $config['level'], true);

        $logger = new Logger(config('app.env'));
        $logger->setHandlers([$handler]);

        return $logger;
    }
}
  1. Create a test job - App\Jobs\TestJob
<?php

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.');
    }
}
  1. Add test routes - routes/web.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
  1. Run the queue:
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

  • Operating System: Docker - PHP-8.2-alpine
  • PHP Version: 8.2
  • ES-PHP client version: ^8.10
  • Elasticsearch version: docker image elasticsearch:7.17.13
@genivaldosilva genivaldosilva changed the title Logs are not sent in Laravel queues Logs are not sent in Laravel queues when use async log Oct 20, 2023
@ezimuel
Copy link
Contributor

ezimuel commented Nov 3, 2023

@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
Copy link
Author

Hi @ezimuel!
Same problem.

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

No branches or pull requests

2 participants