From 5d9f1465d743a2b3c8c5f548bc13dbbb43fdfcc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Neto?= Date: Tue, 9 Jan 2024 22:18:13 -0400 Subject: [PATCH] Permitindo colocar os logs em fila. --- src/app/Jobs/ProcessLog.php | 80 +++++++++++++++++++++++++ src/app/Services/AbstractLogService.php | 7 ++- src/config/config.php | 5 ++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/app/Jobs/ProcessLog.php diff --git a/src/app/Jobs/ProcessLog.php b/src/app/Jobs/ProcessLog.php new file mode 100644 index 0000000..22bf349 --- /dev/null +++ b/src/app/Jobs/ProcessLog.php @@ -0,0 +1,80 @@ +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 + */ + 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); + } +} \ No newline at end of file diff --git a/src/app/Services/AbstractLogService.php b/src/app/Services/AbstractLogService.php index 39e794c..d6e96e1 100644 --- a/src/app/Services/AbstractLogService.php +++ b/src/app/Services/AbstractLogService.php @@ -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)); } } \ No newline at end of file diff --git a/src/config/config.php b/src/config/config.php index 3d4744c..3897ebe 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -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'), ];