You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using Laravel 10 and for logs delivery I am using queues. The log system works fine, for example if I run it from a command, or a controller:
$logger = new Logger('mychannel);
$logger->pushHandler(new LogtailHandler('xxxx'));
$logger->info('message', $array_data);
To improve the user experience, I decided to use queues for log management:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Monolog\Logger;
use Logtail\Monolog\LogtailHandler;
class LogJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct(
public string $channel,
public string $level,
public string $message,
public array $data
) {
}
/**
* Execute the job.
*/
public function handle(): void
{
if ($this->channel == 'ychannel') {
$logger = new Logger($this->channel);
$logger->pushHandler(new LogtailHandler('dddd'));
}
if ($this->channel == 'xchanel') {
$logger = new Logger($this->channel);
$logger->pushHandler(new LogtailHandler('zxy'));
}
if ($this->level == 'INFO') {
$logger->info($this->message, $this->data);
}
if ($this->level == 'ERROR') {
$logger->error($this->message, $this->data);
}
if ($this->level == 'WARNING') {
$logger->warning($this->message, $this->data);
}
}
}
// dispatch a log on los queue
LogJob::dispatch('ychannel', 'INFO', 'User Added now',
['data' => ['email' => 'xx@xx.com', 'country_id' => 456], ])->onQueue('log_queue');
The queue runs correctly, but the logs do not be delivered to the logtail server.
Looking at how the queue works, it seems that the queue finishes the job first, before monolog finishes sending the log.
monolog-logtail 3.0
php 8.2
Laravel 10.x
Any tips on how to handle this situation?
The text was updated successfully, but these errors were encountered:
That's because logs are written in batch and buffer automatically writes only when php terminates on end of the request.
You can you synchronous handler, but better solution is to call Monolog\Logger->reset() when logs should be written.
It may make sense to add this feature even into Laravel queing system internally, to reset logger after each message
I am using Laravel 10 and for logs delivery I am using queues. The log system works fine, for example if I run it from a command, or a controller:
To improve the user experience, I decided to use queues for log management:
The queue runs correctly, but the logs do not be delivered to the logtail server.
Looking at how the queue works, it seems that the queue finishes the job first, before monolog finishes sending the log.
Any tips on how to handle this situation?
The text was updated successfully, but these errors were encountered: