diff --git a/config/laravel_fluentd_logger.php b/config/laravel_fluentd_logger.php index f726379..017a375 100644 --- a/config/laravel_fluentd_logger.php +++ b/config/laravel_fluentd_logger.php @@ -27,5 +27,12 @@ 'request_log' => true, 'db_query_log' => true, 'queue_log' => true, + 'console_commands_log' => true, + ], + + 'console_commands_log' => [ + 'excluded' => [ +// 'migrate' + ], ], ]; diff --git a/src/LaravelFluentdLoggerServiceProvider.php b/src/LaravelFluentdLoggerServiceProvider.php index 69b1cf3..994a425 100644 --- a/src/LaravelFluentdLoggerServiceProvider.php +++ b/src/LaravelFluentdLoggerServiceProvider.php @@ -2,11 +2,13 @@ namespace Vmorozov\LaravelFluentdLogger; +use Illuminate\Console\Events\CommandFinished; use Illuminate\Contracts\Queue\Job; use Illuminate\Queue\Events\JobFailed; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Queue; use Spatie\LaravelPackageTools\Package; @@ -51,6 +53,10 @@ private function initTracing(): void if ($config['features_enabled']['queue_log'] ?? true) { $this->initQueueJobsLog(); } + + if ($config['features_enabled']['console_commands_log'] ?? true) { + $this->initConsoleCommandsLog(); + } } private function initQueueJobsLog(): void @@ -125,4 +131,38 @@ private function registerLogDriver(): void return new FluentLogManager($app); }); } + + private function initConsoleCommandsLog(): void + { + if (!$this->app->runningInConsole()) { + return; + } + + $excludedCommands = config('laravel_fluentd_logger.console_commands_log.excluded', []); + + Event::listen(CommandFinished::class, function (CommandFinished $event) use ($excludedCommands) { + $signature = $event->command; + + if (!$signature || in_array($signature, $excludedCommands)) { + return; + } + + $timeFinished = microtime(true); + + $executionTime = defined('LARAVEL_START') ? + $timeFinished - LARAVEL_START : + 0; + $executionTime = round($executionTime * 1000); + + $memoryPeak = memory_get_peak_usage(true) / 1048576; + + Log::info('Console command executed: ' . $signature, [ + 'signature' => $signature, + 'execution_time_ms' => $executionTime, + 'peak_memory_usage' => $memoryPeak, + 'input' => $event->input->getArguments(), + 'exit_code' => $event->exitCode, + ]); + }); + } } diff --git a/src/Logs/FluentHandler.php b/src/Logs/FluentHandler.php index adb82e9..9b8b81e 100644 --- a/src/Logs/FluentHandler.php +++ b/src/Logs/FluentHandler.php @@ -70,6 +70,7 @@ protected function write(array $record): void '@message' => $record['message'], '@context' => $this->getContext($record['context']), '@extra' => $record['extra'], + '@host' => (string)gethostname(), ] ); }