Permitindo colocar os logs em fila.

production
José Neto 2 years ago
parent b90cd58e2c
commit 5d9f1465d7

@ -0,0 +1,80 @@
<?php
namespace Ae3\LaravelLogsLayer\app\Jobs;
use Ae3\LaravelLogsLayer\app\Exceptions\InvalidArgumentException;
use GuzzleHttp\Exception\GuzzleException;
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 ProcessLog implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* @var string
*/
protected string $channel;
/**
* @var string
*/
protected string $level;
/**
* @var string
*/
protected string $message;
/**
* @var array
*/
protected array $data;
/**
* @param string $channel
* @param string $level
* @param string $message
* @param array $data
*/
public function __construct(
string $channel,
string $level,
string $message,
array $data
)
{
$this->level = $level;
$this->message = $message;
$this->data = $data;
$this->channel = $channel;
}
/**
* Determine the time at which the job should timeout.
*/
public function retryUntil(): \DateTime
{
return now()->addMinutes(config('laravel-logs-layer.queue.retry_until_in_minutes', 60));
}
/**
* Calculate the number of seconds to wait before retrying the job.
*
* @return array<int, int>
*/
public function backoff(): array
{
return array_map('intval', config('laravel-logs-layer.queue.backoff', ['5','10','20','40']));
}
/**
* @return void
*/
public function handle(): void
{
$level = $this->level;
Log::channel($this->channel)->$level($this->message, $this->data);
}
}

@ -4,6 +4,7 @@ namespace Ae3\LaravelLogsLayer\app\Services;
use Ae3\LaravelLogsLayer\app\Containers\LogDataContainer;
use Ae3\LaravelLogsLayer\app\DataTransferObjects\ExceptionContextDTO;
use Ae3\LaravelLogsLayer\app\Jobs\ProcessLog;
use Illuminate\Support\Facades\Log;
use Throwable;
@ -208,6 +209,10 @@ abstract class AbstractLogService implements Contracts\LogServiceInterface
*/
protected function log(string $level, string $message, array $data): void
{
Log::channel($this->getLogChannel())->$level($message, $data);
if (config('laravel-logs-layer.queue.enabled', false)){
Log::channel($this->getLogChannel())->$level($message, $data);
}
dispatch(new ProcessLog($this->getLogChannel(), $level, $message, $data));
}
}

@ -9,5 +9,10 @@
*/
return [
'queue' => [
'enabled' => env('LOG_QUEUE_ENABLED', false),
'retry_until_in_minutes' => env('LOG_QUEUE_RETRY_UNTIL_IN_MINUTES', 60),
'backoff' => explode(',', env('LOG_QUEUE_BACKOFF', '15'))
],
'sensitive_data' => env('LOGS_LAYER_SENSITIVE_DATA', 'password,password_confirmation,token,api_token,api_key,access_token,refresh_token,authorization_code,client_secret'),
];

Loading…
Cancel
Save