diff --git a/composer.json b/composer.json index cb33768b..c64fea9e 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "symfony/finder": "^4.0", "zendframework/zend-db": "^2.8", "dragonmantank/cron-expression": "^2.0", - "doctrine/dbal": "^2.7" + "doctrine/dbal": "^2.7", + "monolog/monolog": "^1.24.0" }, "require-dev": { "phpunit/phpunit": "^7.0", diff --git a/src/Message/Broker/Redis.php b/src/Message/Broker/Redis.php index d255362f..c4e5681b 100644 --- a/src/Message/Broker/Redis.php +++ b/src/Message/Broker/Redis.php @@ -21,7 +21,7 @@ public function waitForNewMessage(): BrokerInterface 0 ); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -43,7 +43,7 @@ public function hasMessage(): bool $publishChannelLength = $this->getPublishChannelLength(); $subscriptionChannelLength = $this->getSubscriptionChannelLength(); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -58,7 +58,7 @@ public function getNextMessage(): string $message = $this->_getRedisClient()->rPop($this->_getPublishChannelName()); } } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -70,7 +70,7 @@ public function getPublishChannelLength(): int try { $publishChannelLength = $this->_getRedisClient()->lLen($this->_getPublishChannelName()); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -82,7 +82,7 @@ public function getSubscriptionChannelLength(): int try { $subscriptionChannelLength = $this->_getRedisClient()->lLen($this->_getSubscriptionChannelName()); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -94,7 +94,7 @@ public function publishMessage($message): BrokerInterface try { $this->_getRedisClient()->lPush($this->_getPublishChannelName(), $message); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } diff --git a/src/Process/Job.php b/src/Process/Job.php index c85734cb..aa77c3f6 100644 --- a/src/Process/Job.php +++ b/src/Process/Job.php @@ -28,7 +28,7 @@ protected function _run(): Forked $this->_getMaintainer()->deleteCompletedJobs(); $this->_getForeman()->workWorker(); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); $this->_setOrReplaceExitCode(255); } diff --git a/src/Process/Listener/Command.php b/src/Process/Listener/Command.php index c1cd9d99..078f13c5 100644 --- a/src/Process/Listener/Command.php +++ b/src/Process/Listener/Command.php @@ -41,7 +41,7 @@ public function addProcess(string $processTypeCode): Command try { $this->_getProcessPool()->addChildProcess($process); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } return $this; diff --git a/src/Process/Pool/Logger.php b/src/Process/Pool/Logger.php index 7d691773..b2c5f63b 100644 --- a/src/Process/Pool/Logger.php +++ b/src/Process/Pool/Logger.php @@ -3,6 +3,7 @@ namespace Neighborhoods\Kojo\Process\Pool; +use Monolog\Formatter\NormalizerFormatter; use Neighborhoods\Kojo\Process\Pool\Logger\FormatterInterface; use Neighborhoods\Kojo\ProcessInterface; use Neighborhoods\Pylon\Data\Property\Defensive; @@ -14,7 +15,11 @@ class Logger extends Log\AbstractLogger implements LoggerInterface use Time\AwareTrait; use Logger\Message\Factory\AwareTrait; use Defensive\AwareTrait; + public const PROP_IS_ENABLED = 'is_enabled'; + public const CONTEXT_KEY_EXCEPTION = 'exception'; + public const CONTEXT_KEY_EXCEPTION_STRING = 'exception_string'; + protected const LOG_DATE_TIME_FORMAT = 'D, d M y H:i:s.u T'; protected $log_formatter; @@ -49,6 +54,15 @@ public function log($level, $message, array $context = []) $logMessage->setProcessId($processId); $logMessage->setProcessPath($this->_getProcess()->getPath()); $logMessage->setMessage($message); + + if (array_key_exists(self::CONTEXT_KEY_EXCEPTION, $context) && $context[self::CONTEXT_KEY_EXCEPTION] + instanceof \Throwable){ + $context[self::CONTEXT_KEY_EXCEPTION_STRING] = (string)$context[self::CONTEXT_KEY_EXCEPTION]; + $normalizedException = (new NormalizerFormatter())->format([$context[self::CONTEXT_KEY_EXCEPTION]]); + unset($context[self::CONTEXT_KEY_EXCEPTION]); + $context[self::CONTEXT_KEY_EXCEPTION] = $normalizedException[0]; + } + if (json_encode($context) === false) { $logMessage->setContext([]); } else { diff --git a/src/Process/Pool/Strategy.php b/src/Process/Pool/Strategy.php index abd24e58..0b1d88cc 100644 --- a/src/Process/Pool/Strategy.php +++ b/src/Process/Pool/Strategy.php @@ -50,7 +50,7 @@ protected function _listenerProcessExited(ListenerInterface $listenerProcess): S $this->_getProcessPool()->addChildProcess($replacementListenerProcess); } catch (Exception $forkedException) { $this->_pauseListenerProcess($listenerProcess); - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } } @@ -85,7 +85,7 @@ protected function _jobProcessExited(JobInterface $jobProcess): Strategy try { $this->_getProcessPool()->addChildProcess($replacementProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } if (!$this->_getProcessPool()->hasAlarm()) { $this->_getProcessPool()->setAlarm($this->getMaxAlarmTime()); @@ -106,7 +106,7 @@ public function receivedAlarm(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($alarmProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } } @@ -125,7 +125,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($process); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); if ($process instanceof CommandInterface) { $this->_getProcessPool()->getProcess()->exit(); } @@ -138,7 +138,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($fillProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } } @@ -179,7 +179,7 @@ protected function _unPauseListenerProcesses(): Strategy $this->_getProcessPool()->addChildProcess($newListenerProcess); unset($this->_pausedListenerProcesses[$processId]); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } } else { diff --git a/src/Process/Pool/Strategy/Server.php b/src/Process/Pool/Strategy/Server.php index 9e998dae..40d4b7a9 100644 --- a/src/Process/Pool/Strategy/Server.php +++ b/src/Process/Pool/Strategy/Server.php @@ -21,7 +21,7 @@ public function childProcessExited(ProcessInterface $process): StrategyInterface try { $this->_getProcessPool()->addChildProcess($rootProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } else { $className = get_class($process); @@ -48,7 +48,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($process); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); $this->_getProcessPool()->getProcess()->exit(); } } @@ -59,7 +59,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($fillProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); $this->_getProcessPool()->getProcess()->exit(); } } diff --git a/src/Process/Pool/Strategy/Worker.php b/src/Process/Pool/Strategy/Worker.php index c6076a53..25e597b2 100644 --- a/src/Process/Pool/Strategy/Worker.php +++ b/src/Process/Pool/Strategy/Worker.php @@ -52,7 +52,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($process); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); $this->_getProcessPool()->getProcess()->exit(); } } @@ -63,7 +63,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($fillProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); $this->_getProcessPool()->getProcess()->exit(); } }