diff --git a/app/Console/BaseCommand.php b/app/Console/BaseCommand.php index 86a511e1..4a4682c7 100644 --- a/app/Console/BaseCommand.php +++ b/app/Console/BaseCommand.php @@ -3,25 +3,42 @@ namespace App\Console; use Illuminate\Console\Command; -use Monolog\Handler\StreamHandler; use Symfony\Component\Process\Process; +use Log; class BaseCommand extends Command { /** - * Splice the logger and replace the handler to direct - * everything to stdout as well as the log + * Splice the logger and replace the active handlers with + * the handlers from the "cron" stack in config/logging.php + * + * Close out any of the existing handlers so we don't leave + * file descriptors leaking around + * + * @param string $channel_name Channel name to grab the handlers from */ - public function redirectLoggingToStdout() + public function redirectLoggingToStdout($channel_name) { $logger = app(\Illuminate\Log\Logger::class); - $handlers = $logger->getHandlers(); + // Close the existing loggers try { - $handlers[] = new StreamHandler('php://stdout'); - $logger->setHandlers($handlers); + $handlers = $logger->getHandlers(); + foreach ($handlers as $handler) { + $handler->close(); + } } catch (\Exception $e) { - $this->error('Couldn\'t splice the logger'); + $this->error('Error closing handlers: ' . $e->getMessage()); + } + + // Open the handlers for the channel name, + // and then set them to the main logger + try { + $logger->setHandlers( + Log::channel($channel_name)->getHandlers() + ); + } catch (\Exception $e) { + $this->error('Couldn\'t splice the logger: ' . $e->getMessage()); } } @@ -53,13 +70,13 @@ class BaseCommand extends Command * @throws \Symfony\Component\Process\Exception\RuntimeException * @throws \Symfony\Component\Process\Exception\LogicException */ - public function runCommand($cmd, $return=false, $verbose=true) + public function runCommand($cmd, $return = false, $verbose = true) { if (\is_array($cmd)) { $cmd = join(' ', $cmd); } - if($verbose) { + if ($verbose) { $this->info('Running "' . $cmd . '"'); } diff --git a/app/Console/Cron/Monthly.php b/app/Console/Cron/Monthly.php index e17b3deb..00208ab6 100644 --- a/app/Console/Cron/Monthly.php +++ b/app/Console/Cron/Monthly.php @@ -18,7 +18,7 @@ class Monthly extends BaseCommand public function handle(): void { - $this->redirectLoggingToStdout(); + $this->redirectLoggingToStdout('cron'); event(new CronMonthly()); } } diff --git a/app/Console/Cron/Nightly.php b/app/Console/Cron/Nightly.php index 4f4b6b53..2d81d323 100644 --- a/app/Console/Cron/Nightly.php +++ b/app/Console/Cron/Nightly.php @@ -18,7 +18,7 @@ class Nightly extends BaseCommand public function handle(): void { - $this->redirectLoggingToStdout(); + $this->redirectLoggingToStdout('cron'); event(new CronNightly()); } } diff --git a/app/Console/Cron/Weekly.php b/app/Console/Cron/Weekly.php index e14aeb08..4cb2b61e 100644 --- a/app/Console/Cron/Weekly.php +++ b/app/Console/Cron/Weekly.php @@ -18,7 +18,7 @@ class Weekly extends BaseCommand public function handle(): void { - $this->redirectLoggingToStdout(); + $this->redirectLoggingToStdout('cron'); event(new CronMonthly()); } } diff --git a/config/logging.php b/config/logging.php index f6fc76c3..288b2a00 100644 --- a/config/logging.php +++ b/config/logging.php @@ -33,9 +33,10 @@ return [ # PHP_SAPI === 'cli' ? 'console' : 'daily', ], ], - 'console' => [ + 'cron' => [ 'driver' => 'stack', 'channels' => [ + 'cron_rotating', 'stdout', ], ], @@ -50,6 +51,12 @@ return [ 'level' => 'debug', 'days' => 3, ], + 'cron_rotating' => [ + 'driver' => 'daily', + 'path' => storage_path('logs/cron.log'), + 'level' => 'debug', + 'days' => 3, + ], 'slack' => [ 'driver' => 'slack', 'url' => env('LOG_SLACK_WEBHOOK_URL'),