Splice the logger to use the cron stack for the console
This commit is contained in:
@@ -3,25 +3,42 @@
|
|||||||
namespace App\Console;
|
namespace App\Console;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Monolog\Handler\StreamHandler;
|
|
||||||
use Symfony\Component\Process\Process;
|
use Symfony\Component\Process\Process;
|
||||||
|
use Log;
|
||||||
|
|
||||||
class BaseCommand extends Command
|
class BaseCommand extends Command
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Splice the logger and replace the handler to direct
|
* Splice the logger and replace the active handlers with
|
||||||
* everything to stdout as well as the log
|
* 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);
|
$logger = app(\Illuminate\Log\Logger::class);
|
||||||
$handlers = $logger->getHandlers();
|
|
||||||
|
|
||||||
|
// Close the existing loggers
|
||||||
try {
|
try {
|
||||||
$handlers[] = new StreamHandler('php://stdout');
|
$handlers = $logger->getHandlers();
|
||||||
$logger->setHandlers($handlers);
|
foreach ($handlers as $handler) {
|
||||||
|
$handler->close();
|
||||||
|
}
|
||||||
} catch (\Exception $e) {
|
} 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\RuntimeException
|
||||||
* @throws \Symfony\Component\Process\Exception\LogicException
|
* @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)) {
|
if (\is_array($cmd)) {
|
||||||
$cmd = join(' ', $cmd);
|
$cmd = join(' ', $cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($verbose) {
|
if ($verbose) {
|
||||||
$this->info('Running "' . $cmd . '"');
|
$this->info('Running "' . $cmd . '"');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class Monthly extends BaseCommand
|
|||||||
|
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
$this->redirectLoggingToStdout();
|
$this->redirectLoggingToStdout('cron');
|
||||||
event(new CronMonthly());
|
event(new CronMonthly());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class Nightly extends BaseCommand
|
|||||||
|
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
$this->redirectLoggingToStdout();
|
$this->redirectLoggingToStdout('cron');
|
||||||
event(new CronNightly());
|
event(new CronNightly());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class Weekly extends BaseCommand
|
|||||||
|
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
$this->redirectLoggingToStdout();
|
$this->redirectLoggingToStdout('cron');
|
||||||
event(new CronMonthly());
|
event(new CronMonthly());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,9 +33,10 @@ return [
|
|||||||
# PHP_SAPI === 'cli' ? 'console' : 'daily',
|
# PHP_SAPI === 'cli' ? 'console' : 'daily',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'console' => [
|
'cron' => [
|
||||||
'driver' => 'stack',
|
'driver' => 'stack',
|
||||||
'channels' => [
|
'channels' => [
|
||||||
|
'cron_rotating',
|
||||||
'stdout',
|
'stdout',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
@@ -50,6 +51,12 @@ return [
|
|||||||
'level' => 'debug',
|
'level' => 'debug',
|
||||||
'days' => 3,
|
'days' => 3,
|
||||||
],
|
],
|
||||||
|
'cron_rotating' => [
|
||||||
|
'driver' => 'daily',
|
||||||
|
'path' => storage_path('logs/cron.log'),
|
||||||
|
'level' => 'debug',
|
||||||
|
'days' => 3,
|
||||||
|
],
|
||||||
'slack' => [
|
'slack' => [
|
||||||
'driver' => 'slack',
|
'driver' => 'slack',
|
||||||
'url' => env('LOG_SLACK_WEBHOOK_URL'),
|
'url' => env('LOG_SLACK_WEBHOOK_URL'),
|
||||||
|
|||||||
Reference in New Issue
Block a user