Splice the logger to use the cron stack for the console

This commit is contained in:
Nabeel Shahzad
2018-03-17 18:05:51 -05:00
parent 67ef6872af
commit 4b7cd6ce6e
5 changed files with 38 additions and 14 deletions

View File

@@ -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 . '"');
}

View File

@@ -18,7 +18,7 @@ class Monthly extends BaseCommand
public function handle(): void
{
$this->redirectLoggingToStdout();
$this->redirectLoggingToStdout('cron');
event(new CronMonthly());
}
}

View File

@@ -18,7 +18,7 @@ class Nightly extends BaseCommand
public function handle(): void
{
$this->redirectLoggingToStdout();
$this->redirectLoggingToStdout('cron');
event(new CronNightly());
}
}

View File

@@ -18,7 +18,7 @@ class Weekly extends BaseCommand
public function handle(): void
{
$this->redirectLoggingToStdout();
$this->redirectLoggingToStdout('cron');
event(new CronMonthly());
}
}

View File

@@ -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'),