Skip to content
This repository was archived by the owner on Apr 19, 2025. It is now read-only.

Commit 3e63676

Browse files
committed
added logging of successful artisan commands
1 parent 56366a7 commit 3e63676

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

config/laravel_fluentd_logger.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,12 @@
2727
'request_log' => true,
2828
'db_query_log' => true,
2929
'queue_log' => true,
30+
'console_commands_log' => true,
31+
],
32+
33+
'console_commands_log' => [
34+
'excluded' => [
35+
// 'migrate'
36+
],
3037
],
3138
];

src/LaravelFluentdLoggerServiceProvider.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Vmorozov\LaravelFluentdLogger;
44

5+
use Illuminate\Console\Events\CommandFinished;
56
use Illuminate\Contracts\Queue\Job;
67
use Illuminate\Queue\Events\JobFailed;
78
use Illuminate\Queue\Events\JobProcessed;
89
use Illuminate\Queue\Events\JobProcessing;
910
use Illuminate\Support\Facades\DB;
11+
use Illuminate\Support\Facades\Event;
1012
use Illuminate\Support\Facades\Log;
1113
use Illuminate\Support\Facades\Queue;
1214
use Spatie\LaravelPackageTools\Package;
@@ -51,6 +53,10 @@ private function initTracing(): void
5153
if ($config['features_enabled']['queue_log'] ?? true) {
5254
$this->initQueueJobsLog();
5355
}
56+
57+
if ($config['features_enabled']['console_commands_log'] ?? true) {
58+
$this->initConsoleCommandsLog();
59+
}
5460
}
5561

5662
private function initQueueJobsLog(): void
@@ -125,4 +131,38 @@ private function registerLogDriver(): void
125131
return new FluentLogManager($app);
126132
});
127133
}
134+
135+
private function initConsoleCommandsLog(): void
136+
{
137+
if (!$this->app->runningInConsole()) {
138+
return;
139+
}
140+
141+
$excludedCommands = config('laravel_fluentd_logger.console_commands_log.excluded', []);
142+
143+
Event::listen(CommandFinished::class, function (CommandFinished $event) use ($excludedCommands) {
144+
$signature = $event->command;
145+
146+
if (!$signature || in_array($signature, $excludedCommands)) {
147+
return;
148+
}
149+
150+
$timeFinished = microtime(true);
151+
152+
$executionTime = defined('LARAVEL_START') ?
153+
$timeFinished - LARAVEL_START :
154+
0;
155+
$executionTime = round($executionTime * 1000);
156+
157+
$memoryPeak = memory_get_peak_usage(true) / 1048576;
158+
159+
Log::info('Console command executed: ' . $signature, [
160+
'signature' => $signature,
161+
'execution_time_ms' => $executionTime,
162+
'peak_memory_usage' => $memoryPeak,
163+
'input' => $event->input->getArguments(),
164+
'exit_code' => $event->exitCode,
165+
]);
166+
});
167+
}
128168
}

src/Logs/FluentHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected function write(array $record): void
7070
'@message' => $record['message'],
7171
'@context' => $this->getContext($record['context']),
7272
'@extra' => $record['extra'],
73+
'@host' => (string)gethostname(),
7374
]
7475
);
7576
}

0 commit comments

Comments
 (0)