Skip to content

Commit

Permalink
added logging of successful artisan commands
Browse files Browse the repository at this point in the history
  • Loading branch information
freezer278 committed Dec 14, 2022
1 parent 56366a7 commit 3e63676
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/laravel_fluentd_logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@
'request_log' => true,
'db_query_log' => true,
'queue_log' => true,
'console_commands_log' => true,
],

'console_commands_log' => [
'excluded' => [
// 'migrate'
],
],
];
40 changes: 40 additions & 0 deletions src/LaravelFluentdLoggerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
]);
});
}
}
1 change: 1 addition & 0 deletions src/Logs/FluentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected function write(array $record): void
'@message' => $record['message'],
'@context' => $this->getContext($record['context']),
'@extra' => $record['extra'],
'@host' => (string)gethostname(),
]
);
}
Expand Down

0 comments on commit 3e63676

Please sign in to comment.